ImageIO 保存回原始大小 [英] ImageIO saves back to original size

查看:25
本文介绍了ImageIO 保存回原始大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在互联网上寻找一些解决方案,但仍然没有找到解决问题的方法.

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

我一直在工作或做一个程序,它可以从我的 PC 获取图像文件,然后使用 Java 图形 进行编辑以添加一些文本/对象/等.之后,Java ImageIO 将保存新修改的图像.

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.

原始图像是 2x3 英寸图像,而修改后的图像据说有 2x3 英寸太可悲了 8x14 英寸.所以,它比原来的要大得多.

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.

有什么解决方案/代码可以让我输出 2x3 英寸的图像并且仍然具有良好的质量"?

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

更新:

所以,这是我使用的代码.

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();
    }
}

第二次更新:

我现在知道新图像有什么问题了.当我从 Photoshop 中检查时,与原始图像分辨率相比,它具有不同的图像分辨率.原始图像的分辨率为 300 像素/英寸,而新图像的分辨率为 72 像素/英寸.

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.

如何使用 Java 更改分辨率?

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

推荐答案

要设置(JFIF 段的)图像分辨率,您可能可以使用 IIOMetatada for JPEG.

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

大致如下:

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);
    }
}

注意:不应逐字使用此代码,但添加迭代、错误处理、流关闭等会使示例过于混乱.

参见 JPEG 图像元数据DTD 有关元数据格式的文档,以及您可以控制的选项.

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

这篇关于ImageIO 保存回原始大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆