Skip to main content

[JAI-IMAGEIO] Converting TIFF to 300 DPI

5 replies [Last post]
Anonymous

I'm reading multi-page TIFF images using Image I/O and writing each
image to a separate file. In the process of doing this I need to
convert each image to have an XResolution and YResolution of 300\1.
I've already done this by iterating over the metadata tree and changing
the tag after having scaled the image to maintain the same size with the
new DPI. Is this the only way to do this or is there something easier?

Regards,
Ken Blair

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
philipc2000
Offline
Joined: 2007-03-01

>I've already done this by iterating over the metadata tree and changing
>the tag after having scaled the image to maintain the same size with the
>new DPI

Can a JAI Guru please show me some sample code on how to modify the XResolution, YResolution, and ResolutionUnit tags? The code below correctly sets up LITTLE_ENDIAN, but the above 3 tags are reset to 1, and I need to set them to "2" for inches and 200 DPI.

Thanks!
Philip

// Get Writer for TIFF
Iterator writers = ImageIO.getImageWritersByFormatName("TIFF");
ImageWriter writer = (ImageWriter)writers.next();

// Set WriteParam's
TIFFImageWriteParam writeParam = (TIFFImageWriteParam)writer.getDefaultWriteParam();
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType("CCITT T.6");

// Create File to save the image
File f = new File(outFilename);
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
writer.setOutput(ios);

// get metadata from input image
IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(null);
String tiffMetadataFormatName =
streamMetadata.getNativeMetadataFormatName();

// Create the new stream metadata object for little endian byte order.
IIOMetadataNode newTree = new IIOMetadataNode(tiffMetadataFormatName);
IIOMetadataNode byteOrderNode = new IIOMetadataNode("ByteOrder");
byteOrderNode.setAttribute("value", "LITTLE_ENDIAN");
newTree.appendChild(byteOrderNode);

// Set the value of the stream metadata object.
streamMetadata.setFromTree(tiffMetadataFormatName, newTree);

// Create IIOImage
IIOImage iioImage = new IIOImage( targetImage
,null
,null);

// Save the image
writer.write(streamMetadata, iioImage, writeParam);
ios.close();

Brian Burkhalter

Please see the attached sample code which shows two different approaches for
setting the {X,Y}Resolution fields of a TIFF file.

While this example shows only how to set the resolution fields it should apply
to any given TIFF field.

Brian

On Tue, 20 Mar 2007, jai-imageio@javadesktop.org wrote:

> Can a JAI Guru please show me some sample code on how to modify the XResolution, YResolution, and ResolutionUnit tags? The code below correctly sets up LITTLE_ENDIAN, but the above 3 tags are reset to 1, and I need to set them to "2" for inches and 200 DPI.

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[TIFFWriteDPI.java]
---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

dcallaway
Offline
Joined: 2003-06-19

I don't see "attached sample code" anywhere. Where would I find that?

Brian Burkhalter

On Tue, 1 Aug 2006, Ken Blair wrote:

> I'm reading multi-page TIFF images using Image I/O and writing each image to
> a separate file. In the process of doing this I need to convert each image
> to have an XResolution and YResolution of 300\1. I've already done this by
> iterating over the metadata tree and changing the tag after having scaled the
> image to maintain the same size with the new DPI. Is this the only way to do
> this or is there something easier?

The only thing easier which comes to mind is the case where the image is
already at 300 dpi. ;-)

Brian

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

Ken Blair

Well, I'll just keep doing that then. There can't be an exceedingly
fast and easy to use getRational(TAG_X_RESOLUTION) type solution for
everything, I was hoping I just missed it in the TIFF API somewhere but
I see now why that would be silly. The real fun is going to be if they
ever start sending me TIFF files with a resolution unit that's not in
inches. Thanks Brian.

Brian Burkhalter wrote:
> The only thing easier which comes to mind is the case where the image
> is already at 300 dpi. ;-)
>
> Brian

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net