Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

java - ImageIO saves back to original size

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...