Skip to main content

How to create correct TIFF metadata

7 replies [Last post]
adulub
Offline
Joined: 2006-10-16

Hi, all

I need to convert multipage PDF file into multipage TIFF with next established parameters:
1) X-resolution
2) Y-resolution
3) Resiolution Unit
3) Photometric Interpretation

I have converted each PDF page into BufferedImage and then to BufferedImage.TYPE_BYTE_BINARY:

BufferedImage image = page.convertToImage();
BufferedImage outputImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_BYTE_BINARY);
outputImage.createGraphics().drawImage(image,0,0,null);

Now, I have List that contains buffered images and should be transformed into multipage TIFF image.

I have tried several methods to set needed parameters:
for example,
IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier((BufferedImage) images.get(0)), writeParam);
TIFFTagSet set = BaselineTIFFTagSet.getInstance();

Node root = metadata.getAsTree(metadata.getNativeMetadataFormatName());
Node ifd = root.getChildNodes().item(0);

TIFFTag tag = set.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION);
TIFFField field = new TIFFField(tag, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][] { { (long) 204, (long) 1 },{ (long) 0, (long) 0 } });
ifd.appendChild(field.getNativeNode());

tag = set.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION);
field = new TIFFField(tag, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][] { { (long) 196, (long) 1 }, { (long) 0, (long) 0 } });
ifd.appendChild(field.getNativeNode());

try {
metadata.mergeTree(metadata.getNativeMetadataFormatName(), root);
} catch (IIOInvalidTreeException e) {
e.printStackTrace();
}

writer.prepareWriteSequence(metadata);
for (Iterator iter = images.iterator(); iter.hasNext();) {
BufferedImage image = (BufferedImage) iter.next();
IIOImage iioImage = new IIOImage(image, null, metadata);
writer.writeToSequence(iioImage, writeParam);
}

Unfortunatelly, in the created TIFF image, all my settings are ignored. Resolution, for example, looks like
X - 1DPI, Y - 1DPI.

In order to experimet with TIFF, I read several TIFF files,get their metadata and compare with metadata of my files - there are a lot of differences.

Moreover, in the debug mode I have explored IIOMetadata object after my operations and found that my values of resolution and unit established correctly. May be its not enough and I need to set some extra fields ?

Thaks in advance, any advices will be appreciated

Best regards,
Andrey Dulub

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
adulub
Offline
Joined: 2006-10-16

Hmm...

Any suggestions ?

Best regards,
Andrey Dulub

Stefan Funk

Hi Andrey,

I added some excerpt from some of our code, hoping you will find it helpful.
We are using the com.sun.media.imageio.plugins.tiff.* classes to correct some
faulty TIFF EXIF headers. A documentation is located here:
http://download.java.net/media/jai-imageio/javadoc/1.1-latest/index.html...

A list of the TIFF tags and tag names you can fid here:
http://www.awaresystems.be/imaging/tiff/tifftags.html

Best regards,
Stefan.

------------------------------------------------------------------------------
// Set tag value and tag name.
String tagName = "Software";
int tagNumber = 305;

// Add a value to the tag.
String[] _value = { "ASCII value of the TIFF ASCII tag!" };

// Create a new TIFF field including a new TIFF ASCII TIFF tag.
TIFFTag newTag = new TIFFTag(tagName, tagNumber, (1 << TIFFTag.TIFF_ASCII));
newField = new TIFFField(newTag, TIFFTag.TIFF_ASCII, 1, _value);

// Put in the newly created one.
this.tiffDir.addTIFFField(newField);

// This TIFFField you can add to a TIFF directory and save the image.
// Create new metadata from the tiff dir.
IIOMetadata newMetadata = this.tiffDir.getAsMetadata();

// Write the image using an image writer.
IIOImage new_image = new IIOImage(this.image, null, newMetadata);
writer.write(null, new_image, null);
------------------------------------------------------------------------------

jai-imageio@javadesktop.org schrieb am 18.10.2006 13:52:
> Hmm...
>
> Any suggestions ?
>
> Best regards,
> Andrey Dulub
> [Message sent by forum member 'adulub' (adulub)]
>
> http://forums.java.net/jive/thread.jspa?messageID=164143

--
Stefan E. Funk
kopal - http://kopal.langzeitarchivierung.de
Goettingen State and University Library - The Historical Library Building
Papendiek 14
37073 Goettingen, Germany
phone : +49 551 397700
mailto: funk@sub.uni-goettingen.de

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

adulub
Offline
Joined: 2006-10-16

Hi Stefan,

Thank you very much. I have tried a way you proposed and it works.
One more question - I work with multipage tiff which created from collection of the buffered images.

writer.prepareWriteSequence(null);
for (Iterator iter = images.iterator(); iter.hasNext()) {
BufferedImage image = (BufferedImage) iter.next();
IIOImage iioImage = new IIOImage(image, null, [b]metadata[/b]);
writer.writeToSequence(iioImage, writeParam);
}

Do I have to create metadata object for each buffered image or creating of this object for first image from collection and then using it for other images will be enough ?

Best regards,
Andrey Dulub

Stefan Funk

Andrey,

I think both is possible, just as you like. What I do not know is, if there is
a standardized way to describe metadata for the whole image or for single
pages. It could just depend on your interpretation. We do not work with
multipage TIFFs.

Best regards.
Stefan.

jai-imageio@javadesktop.org schrieb am 18.10.2006 15:45:
> Hi Stefan,
>
> Thank you very much. I have tried a way you proposed and it works.
> One more question - I work with multipage tiff which created from collection of the buffered images.
>
> writer.prepareWriteSequence(null);
> for (Iterator iter = images.iterator(); iter.hasNext()) {
> BufferedImage image = (BufferedImage) iter.next();
> IIOImage iioImage = new IIOImage(image, null, [b]metadata[/b]);
> writer.writeToSequence(iioImage, writeParam);
> }
>
> Do I have to create metadata object for each buffered image or creating of this object for first image from collection and then using it for other images will be enough ?
>
> Best regards,
> Andrey Dulub
> [Message sent by forum member 'adulub' (adulub)]
>
> http://forums.java.net/jive/thread.jspa?messageID=164208

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

adulub
Offline
Joined: 2006-10-16

Thanks once again.
You have really helped me.

Best regards,
Andrey Dulub

lcfonseca
Offline
Joined: 2007-04-06

Andrey,

What PDF reader was used? JPedal?

Att.
Luiz Carlos Fonseca

vittalsl
Offline
Joined: 2007-10-08

Hi,

Could you please share the code for the same.

Thanks & Regards,
Vittal