简体   繁体   English

ImageIO恢复为原始大小

[英]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. 我一直在工作或正在执行一个程序,该程序将从PC上获取图像文件,然后将使用Java Graphics对其进行编辑以添加一些文本/对象/等。 After that, Java ImageIO will save the newly modified image. 之后, Java ImageIO将保存新修改的图像。

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 2x3 inches-image while the modified one which supposedly have 2x3inches too sadly got 8x14 inches. 原始图像为2x3英寸,而修改后的图像原本为2x3英寸,可悲的是8x148x14英寸。 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'? 什么解决方案/代码可以使我输出2x3英寸的图像,并且仍然具有“不错的质量”?

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. 当我从Photoshop检查它时,它与原始图像的图像分辨率不同。 The original has a 300 pixels/inch while the new image has a 72 pixels/inch resolution. 原始图像的分辨率为300像素/英寸,而新图像的分辨率为72像素/英寸。

How will I be able to change the resolution using Java? 如何使用Java更改分辨率?

To set the image resolution (of the JFIF segment), you can probably use the IIOMetatada for JPEG. 要设置(JFIF段的)图像分辨率,可以将IIOMetatada用于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. 有关数据格式以及可以控制哪些选项的文档,请参见JPEG图像元数据DTD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM