Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

PNG to JPEG  RSS feed

 
muhilan govin
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a requirement to convert a png into a jpeg maintaining the same dpi(600 dpi). This is the code i use

BufferedImage resizedImage=resize(image, width, height);
ImageIO.write(resizedImage,"jpeg",new File("c:\\Image\\resize.jpg");

private static BufferedImage resize(BufferedImage image, int width, int height)
{
BufferedImage resizedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return resizedImage;
}

The generated image is having a dpi of 96 by default which according to few sites is common in a windows machine. Any idea on how to fix this issue?

Thanks and regards,
Muhilan
SCJP,SCWCD,SCBCD,SCBCD5,SCDJWS,SCEA5,IBM certified Websphere Application server v6 Core Adminstrator,Oracle 9i SQL
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The resizing isn't the problem - the saving is. You'll probably need to use JPEGImageWriteParam or its super class ImageWriteParam. That at least allows you to tweak the quality. I don't know if it also allows you to change the DPI.

Instead of ImageIO.write you'll probably have to do a little of the hard work it does yourself. Well, not a lot, just get an ImageWriter. Which is what ImageIO.getImageWriters can help you with. And you'll need to create IIOImage and IIOMetaData instances. Well, not really. The IIOMetaData can remain null. The IIOImage can be easily constructed:
 
muhilan govin
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

We fixed it by trying to set the dpi in the metadata. The change of dpi in the metadata did not affect the quality as the info is for the printer.

Thanks and regards,
Muhilan
SCJP,SCWCD,SCBCD,SCBCD5,SCDJWS,SCEA5,IBM certified Websphere Application server v6 Core Adminstrator,Oracle 9i SQL
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!