使用 Java 将 DPI 和纸张尺寸信息存储在 JPEG 中

发布于 2024-12-22 22:15:20 字数 406 浏览 4 评论 0原文

我有以下代码:

ImageIO.write(originalImage, OUTPUT_TYPE, resultOutput);

这是对以下 javax.imageio.ImageIO 方法的调用:

public static boolean write(RenderedImage im,
                            String formatName,
                            File output)
                     throws IOException

这会将原始 BMP 图像转换为 JGP 输出。是否可以在 JPEG 中存储 DPI 和纸张尺寸信息以帮助打印操作?

I have the following code:

ImageIO.write(originalImage, OUTPUT_TYPE, resultOutput);

This is an invocation of the following javax.imageio.ImageIO method:

public static boolean write(RenderedImage im,
                            String formatName,
                            File output)
                     throws IOException

This turns an original BMP image into a JGP output. Is it possible to also store DPI and Paper Size information in the JPEG to aid in printing operations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈独秀 2024-12-29 22:15:20

我找到了这篇关于在 PNG 文件上设置 DPI 的帖子。它指出您应该使用“metadata.mergeTree”来正确保存元数据。

考虑到这一点,这里有一些工作常规代码,它采用 BMP 文件并以任意 DPI 创建 JPG 文件:

import java.awt.image.BufferedImage
import java.io.File
import java.util.Hashtable
import java.util.Map
import javax.imageio.*
import javax.imageio.stream.*
import javax.imageio.metadata.*
import javax.imageio.plugins.jpeg.*
import org.w3c.dom.*

File sourceFile = new File("sample.bmp")
File destinationFile = new File("sample.jpg")

dpi = 100

BufferedImage sourceImage = ImageIO.read(sourceFile)
ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpeg").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(destinationFile);
imageWriter.setOutput(ios);
def jpegParams = imageWriter.getDefaultWriteParam();

IIOMetadata data = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(sourceImage), jpegParams);
Element tree = (Element)data.getAsTree("javax_imageio_jpeg_image_1.0");
Element jfif = (Element)tree.getElementsByTagName("app0JFIF").item(0);
jfif.setAttribute("Xdensity", Integer.toString(dpi));
jfif.setAttribute("Ydensity", Integer.toString(dpi));
jfif.setAttribute("resUnits", "1"); // density is dots per inch                 
data.mergeTree("javax_imageio_jpeg_image_1.0",tree)

// Write and clean up
imageWriter.write(data, new IIOImage(sourceImage, null, data), jpegParams);
ios.close();
imageWriter.dispose();

在 OSX 的预览应用程序中对我来说工作得很好,Gimp 都报告生成的图像为 100 DPI。至于纸张大小...我想这是直接由 DPI 决定的?我找不到任何可以设置该特定值的 JPEG 属性。

I found this post for setting DPI on PNG Files. It pointed out that you should use 'metadata.mergeTree' to properly save your metadata.

With that in mind, here is some working groovy code that takes a BMP file and creates a JPG file at arbitrary DPI:

import java.awt.image.BufferedImage
import java.io.File
import java.util.Hashtable
import java.util.Map
import javax.imageio.*
import javax.imageio.stream.*
import javax.imageio.metadata.*
import javax.imageio.plugins.jpeg.*
import org.w3c.dom.*

File sourceFile = new File("sample.bmp")
File destinationFile = new File("sample.jpg")

dpi = 100

BufferedImage sourceImage = ImageIO.read(sourceFile)
ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpeg").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(destinationFile);
imageWriter.setOutput(ios);
def jpegParams = imageWriter.getDefaultWriteParam();

IIOMetadata data = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(sourceImage), jpegParams);
Element tree = (Element)data.getAsTree("javax_imageio_jpeg_image_1.0");
Element jfif = (Element)tree.getElementsByTagName("app0JFIF").item(0);
jfif.setAttribute("Xdensity", Integer.toString(dpi));
jfif.setAttribute("Ydensity", Integer.toString(dpi));
jfif.setAttribute("resUnits", "1"); // density is dots per inch                 
data.mergeTree("javax_imageio_jpeg_image_1.0",tree)

// Write and clean up
imageWriter.write(data, new IIOImage(sourceImage, null, data), jpegParams);
ios.close();
imageWriter.dispose();

Worked fine for me in that OSX's Preview app and Gimp both reported that the resulting image was 100 DPI. As to Paper Size...I imagine this is directly determined by DPI? I couldn't find any JPEG property that would set that particular value.

命硬 2024-12-29 22:15:20

您可以考虑使用 Commons Sanselan 而不是 ImageIO 来完成此任务。

有关详细信息,请参阅 http://commons.apache.org/sanselan/whysanselan.html

You may consider using Commons Sanselan, instead of ImageIO for this task.

See http://commons.apache.org/sanselan/whysanselan.html for more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文