2

I've been searching for some solutions from the internet yet I still haven't found an answer to my problem.

I've been working or doing a program that would get an image file from my PC then will be edited using Java Graphics to add some text/object/etc. After that, Java ImageIO will save the newly modified image.

So far, I was able to do it nicely but I got a problem about the size of the image. The original image and the modified image didn't have the same size.

The original is a 2x3inches-image while the modified one which supposedly have 2x3inches too sadly got 8x14inches. So, it has gone BIGGER than the original one.

What is the solution/code that would give me an output of 2x3inches-image which will still have a 'nice quality'?

UPDATE:

So, here's the code I used.

public Picture(String filename) {
    try {
        File file = new File("originalpic.jpg");
        image = ImageIO.read(file);
        width  = image.getWidth();
    }
    catch (IOException e) {
        throw new RuntimeException("Could not open file: " + filename);
    }
}

private void write(int id) {
    try {
        ImageIO.write(image, "jpg", new File("newpic.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

2nd UPDATE:

I now know what's the problem of the new image. As I check it from Photoshop, It has a different image resolution compared to the original one. The original has a 300 pixels/inch while the new image has a 72 pixels/inch resolution.

How will I be able to change the resolution using Java?

GM-Xile GM-Xile
  • 321
  • 1
  • 8
  • 21
  • The question is a bit general, but my guess is that you are saving in a format with a different DPI setting. Post some code and we can try to help. – Jeff Foster Jul 12 '13 at 13:53
  • 1
    Please post an [SSCCE](http://sscce.org/) of your problem. – mattbdean Jul 12 '13 at 13:53
  • Strange. Every transformation on the Graphics2D should be mirrored with an inverse transformation. _I do not know, but that seems the only explanation._ Clipping needed? – Joop Eggen Jul 12 '13 at 14:00
  • I edited your question to add a ..question. If that is **not** the question, please [edit](http://stackoverflow.com/posts/17616528/edit) accordingly with your own. – Andrew Thompson Jul 12 '13 at 14:10
  • I updated my post above and added the code I used. – GM-Xile GM-Xile Jul 14 '13 at 04:01

1 Answers1

4

To set the image resolution (of the JFIF segment), you can probably use the IIOMetatada for JPEG.

Something along the lines of:

public class MetadataTest {
    public static void main(String[] args) throws IOException {
        BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_3BYTE_BGR);

        ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
        writer.setOutput(ImageIO.createImageOutputStream(new File("foo.jpg")));
        ImageWriteParam param = writer.getDefaultWriteParam();

        IIOMetadata metadata = writer.getDefaultImageMetadata(ImageTypeSpecifier.createFromRenderedImage(image), param);
        IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(metadata.getNativeMetadataFormatName());
        IIOMetadataNode jfif = (IIOMetadataNode) root.getElementsByTagName("app0JFIF").item(0);

        jfif.setAttribute("resUnits", "1");
        jfif.setAttribute("Xdensity", "300");
        jfif.setAttribute("Ydensity", "300");

        metadata.mergeTree(metadata.getNativeMetadataFormatName(), root);

        writer.write(null, new IIOImage(image, null, metadata), param);
    }
}

Note: this code should not be used verbatim, but adding iteration, error handling, stream closing etc, clutters the example too much.

See JPEG Image Metadata DTD for documentation on the metadata format, and what options you can control.

Harald K
  • 26,314
  • 7
  • 65
  • 111