使用Java在JPEG中存储DPI和纸张尺寸信息 [英] Storing DPI and Paper Size information in a JPEG with Java

查看:301
本文介绍了使用Java在JPEG中存储DPI和纸张尺寸信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

ImageIO.write(originalImage, OUTPUT_TYPE, resultOutput);

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

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

这会将原始BMP图像转换为JGP输出。

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?

推荐答案

幸运的是,可以在JPEG中存储DPI和纸张大小信息以帮助进行打印操作吗?通过href = http://java.sun.com/javase/6/docs/technotes/guides/imageio/ rel = noreferrer> java映像I / O API ,您可以做到这一点。它还允许以格式独立的方式设置图像元数据。例如,指定所需的DPI,这就是我想要做的。

Luckily, the java image I/O API lets you do just that. It also allows for setting the image metadata in a format-independent way. For example to specify the desired DPI, which was what I wanted to do.

事实证明,设置dpi比预期的要简单得多……

As it turned out, setting the dpi was less straightforward than expected…

java映像I / O API 插件基于,对于您希望使用适当插件的每种文件格式,都必须可用。该API为以下内容提供了必要的抽象编写插件中性代码,并具有可用实现的存储库,您可以在运行时进行查询。从J2SE 6开始,每个JRE都必须提供PNG,JPG和BMP文件格式的插件(以及一些我尚未尝试过的插件)。

The java image I/O API is plug-in based, for every file format you wish to use an appropriate plug-in must be available. The API provides the necessary abstractions for writing plug-in neutral code and has a repository of available implementations that you can query at run-time. As of J2SE 6, every JRE has to provide plug-ins for PNG, JPG and BMP file formats (plus some others that I haven’t tried out yet).

设置图像元数据的规范为标准(非插入式)元数据格式规范。奇怪的是,规范是XML模式。没错,如果要设置DPI,则必须通过构建 DOM -树,使用 IIOMetadataNodes 将其合并!感叹..

The spec for setting image metadata is the standard (plug-in neutral) metadata format specification. Strangely enough, the spec is an XML schema. That’s right, if you want to set the DPI, you’ll have to do it by building a DOM-tree using IIOMetadataNodes and merge it in with the rest! Sigh..

请注意,插件对标准元数据的支持可能有所不同:

Beware that plug-ins can differ in their support for the standard metadata :

  • some don’t support changing the standard metadata
  • some don’t support the standard metadata format at all
  • some don’t support merging your DOM-tree with the current metadata and will silently replace it

无论如何,要设置DPI时,相关标签为Horizo​​ntalPixelSize和VerticalPixelSize:

Anyway, the relevant tags when you want to set the DPI are HorizontalPixelSize and VerticalPixelSize :

<!ELEMENT "HorizontalPixelSize" EMPTY>
   <!-- The width of a pixel, in millimeters,
             as it should be rendered on media -->
   <!ATTLIST "HorizontalPixelSize" "value" #CDATA #REQUIRED>
   <!-- Data type: Float -->
<!ELEMENT "VerticalPixelSize" EMPTY>
   <!-- The height of a pixel, in millimeters,
             as it should be rendered on media -->
   <!ATTLIST "VerticalPixelSize" "value" #CDATA #REQUIRED>
   <!-- Data type: Float -->

请注意,规范中明确指出,两者都必须以毫米/点表示。
如何无视您自己的规范,Sun风格

Note that the spec clearly states that both have to be expressed in millimeters per dot. How to disregard your own spec, Sun style

Sun已为其PNG和JPG插件实现了此元数据规范,并将其包含在当前的JDK和JRE发行版。相关类为

Sun has implemented this metadata spec for their PNG and JPG plug-ins and includes it in their current JDK and JRE distributions. The relevant classes are

com.sun.imageio.plugins.png.PNGImageWriter
com.sun.imageio.plugins.jpeg.JPEGImageWriter

您可以从com.sun软件包中看出它们不属于J2SE API,但特定于Sun的实现。

You can tell from the com.sun package that they’re not part of the J2SE API, but specific to Sun’s implementation.

还记得规范如何要求每点毫米吗?好吧,请查看下表以了解Sun实际如何执行该规范:

Remember how the spec required millimeters per dot? Well, have a look at the following table to see how Sun actually implemented the spec :

plug-in          unit                 bug report   date reported
PNGImageWriter   dots per millimeter  bug 5106305  23 sep 2004
JPEGImageWriter  decimeter per dot    bug 6359243  05 dec 2005

这些错误早已为人所知,其修复非常简单。不幸的是,在撰写本文时(2008年7月),它们的优先级较低,甚至没有得到评估。
太好了。现在怎么办?

These bugs have been known for a very long time and their fixes are really simple. Unfortunately, they were given a low priority and haven’t even been evaluated at the time of writing (July 2008). Great. Now what?

好吧,因为解决方法很简单,所以我决定使用图像I / O API。如果为这些类提供所需的内容,则位图会很好地显示出来。为了确保您的导出代码也能在正确实现该规范的平台上正常运行,它必须检查正在使用的实际实现类并补偿错误。

Well, because the workaround is so trivial I decided to stick with the image I/O API. If you give these classes what they want, the bitmaps will come out fine. To ensure that your export code also works on platforms that implement the spec correctly, it must check the actual implementation classes that are being used and compensate for the bugs.

如果如果发现自己处于类似情况,请确保最终解决错误后,您的解决方法代码将能够应对。有关更多信息,请参见 J2SE api中的错误如何叮咬您两次'。

If you find yourself in a similar situation, make sure your workaround code will be able to cope when the bugs are fixed eventually. More on this in the article 'How bugs in the J2SE api can bite you twice'.

哦,如果您使用instanceof检查错误类的实例,不能保证在所有平台上都存在,请确保捕获到NoClassDefFoundError;)

Oh, and if you use instanceof to check for instances of a buggy class that is not guaranteed to exist on all platforms, be sure to catch NoClassDefFoundError ;)

这篇关于使用Java在JPEG中存储DPI和纸张尺寸信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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