Storing DPI and paper size information in JPEG using java

I have the following code:

ImageIO.write(originalImage,OUTPUT_TYPE,resultOutput);

This is for the following javax imageio. Call of imageio method:

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

This converts the original BMP image to JGP output Can DPI and paper size information be stored in JPEG to help print operations?

Solution

Fortunately, the java image I / O API allows you to do this It also allows image metadata to be set in a format independent manner For example, specify the required DPI, which is what I want to do

It turns out that setting up DPI is not as simple as expected

The java image I / O API is plug-in based. You must provide appropriate plug-ins for each file format you want to use API provides the necessary abstractions for writing plug-in neutral code, and has a repository of available implementations that can be queried at run time Starting with J2SE 6, each JRE must provide plug-ins in PNG, JPG and BMP file formats (as well as other plug-ins I haven't tried yet)

The specification for setting image metadata is the standard (plug-in neutral) metadata format specification Strangely, the specification is an XML schema This is correct. If you want to set up DPI, you will have to build a DOM tree by using iiometadatanodes and merge it! Sigh

Note that the plug-in's support for standard metadata may be different:

>Some people don't support changing the standard metadata > some people don't support the standard metadata format at all > some people don't support merging your DOM tree with the current metadata and will silently replace it

In any case, when you want to set DPI, the relevant tags 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,as it should be rendered on media -->
   <!ATTLIST "VerticalPixelSize" "value" #CDATA #required>
   <!-- Data type: Float -->

Please note that the specification clearly states that both must be expressed in millimeters per point How to ignore their own specifications, sun style

Sun has implemented this metadata specification for its PNG and JPG plug-ins and included it in the current JDK and JRE distributions Related courses are

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

You can go to www From the sun package, we can see that they are not part of J2SE API, but Sun specific implementation

Remember that the specification of each point requires mm? So look at the following table to see how sun actually implements the specification:

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

These errors have been known for a long time, and their repair is very simple Unfortunately, they were given low priority and were not even evaluated at the time of writing (July 2008) Big What should I do?

Well, because the solution is so trivial, I decided to stick to the image I / O API If you give these classes what they want, bitmaps will be good To ensure that your exported code is also suitable for platforms that correctly implement the specification, it must check the actual implementation classes being used and compensate for errors

If you find yourself in a similar situation, make sure your solution code can deal with it when you finally fix the error More about this article 'how bugs in the J2SE API can bite you twice'

Oh, if you use instanceof to check instances of error classes that cannot be guaranteed to exist on all platforms, be sure to capture NoClassDefFoundError;)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>