节省的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.
我已经工作或者做一个程序,它会从我的电脑得到的图像文件,然后将使用 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.
原来是 2×3
英寸的图像,而经过修改的据称具有2x3inches太可悲了 8x14
英寸。所以,它比原来的走大。
The original is a 2x3
inches-image while the modified one which supposedly have 2x3inches too sadly got 8x14
inches. So, it has gone BIGGER than the original one.
解决方案是什么/ code,它会给我的2x3inches图像的输出,这将仍然有一个'好品质?
What is the solution/code that would give me an output of 2x3inches-image which will still have a 'nice quality'?
更新:
所以,这里的code我用。
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();
}
}
月2日更新:
我现在知道什么是新形象的问题。当我从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
为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);
}
}
注意:此code不应该完全使用,但加入迭代,错误处理,流闭等,杂波的例子太多
请参阅 JPEG图像元数据DTD ,获取有关的元数据格式的文档,你可以控制的选项。
See JPEG Image Metadata DTD for documentation on the metadata format, and what options you can control.
这篇关于节省的ImageIO回原来的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!