After a long time (almost after 2 year) I am writing on this topic and (to be honest) not in touch with the topic now, so if I miss something which is important for you or help something related to your task then you can ask me in the comment section or you can write me on my email address. I will definitely try (my best) to resolve your issue and get back to you with some solution. (I hope there are number of topics available for this on internet now and you can get the solution from some other places also.)
Here we START our work (with some basics):
As we already know that each page which is available inside the multi-page TIFF file contains its own property in it (as an image metadata). Here I am going to share with you a sample code for “How to read all the available properties of each page from its metadata?” The properties presence in metadata gives you the exact information about the image. Sometime when we work on some image part there we need to get this data and process our part of work accordingly. If we know this (metadata) information (input image) then we can easily maintain the quality of image (output) as per our original one.
:: List of Image Properties (just for your information):
- Image Width
- Image Length
- Bits Per Sample
- Compression
- Photometric Interpretation
- Image Description
- Strip Offsets
- Orientation
- Samples Per Pixel
- Rows Per Strip
- Strip Byte Counts
- X-Resolution
- Y-Resolution
- Planar Configuration
- Resolution Unit etc.
Process Description:
It’s a very simple code to get these properties from an individual page (or you can say “image”). Even if you don’t know or don’t want to remember all property names “Not an Issue”. By default you will get this list of information from image metadata. Its come in a set (from metadata of operational image). You just need to focus on your part i.e. which one (property) is important for you and how to fetch it from this bunch. Once you get this information you can able to set your output image property with the same values. (Anyway, this is another topic to consider and you can get it as per your requirements. If you want to get any particular field from property of image you can use the field tag number to get that information.).
Reading image Metadata (Properties):
(Need to) Import Packages:
java.io.File;
java.io.FileNotFoundException;
java.io.IOException;
java.util.Iterator;
javax.imageio.ImageIO;
javax.imageio.ImageReadParam;
javax.imageio.ImageReader;
javax.imageio.metadata.IIOMetadata;
javax.imageio.stream.FileImageInputStream;
javax.imageio.stream.ImageInputStream;
com.sun.media.imageio.plugins.tiff.TIFFDirectory;
com.sun.media.imageio.plugins.tiff.TIFFField;
com.sun.media.jai.codec.FileSeekableStream;
com.sun.media.jai.codec.ImageCodec;
com.sun.media.jai.codec.ImageDecoder;
(Use Google to search related API for detailed information. I know you can get these packages and APIs from internet. or simply click on the reference link below J )
Source Code:
Here only one method which I use for this and really nothing special in it. J You can get the idea about source code from comments.
Method:
public void readTiffImageProperties(String inputTifImagePath) {
Iterator readersIterator = ImageIO.getImageReadersByFormatName("tif");
ImageReader imageReader = (ImageReader)readersIterator.next();
ImageInputStream imageInputStream;
try {
imageInputStream = new FileImageInputStream(new File(inputTifImagePath));
imageReader.setInput(imageInputStream,false, true);
/* Take a input from a file */
FileSeekableStream fileSeekableStream;
fileSeekableStream = new FileSeekableStream(inputTifImagePath);
/* create ImageDecoder to count your pages from multi-page tiff */
ImageDecoder iDecoder = ImageCodec.createImageDecoder("tiff", fileSeekableStream, null);
/* count the number of pages inside the multi-page tiff */
int pageCount = iDecoder.getNumPages();
/* use first for loop to get pages one by one */
for(int page = 0; page < pageCount; page++){
/* get image metadata for each page */
IIOMetadata imageMetadata = imageReader.getImageMetadata(page);
/*
* The root of all the tags for this image is the IFD (Image File Directory).
* Get the IFD from where we can get all the tags for the image.
*/
TIFFDirectory ifd = TIFFDirectory.createFromMetadata(imageMetadata);
/* Create a Array of TIFFField*/
TIFFField[] allTiffFields = ifd.getTIFFFields();
/* use second for loop to get all field data */
for (int i = 0; i < allTiffFields.length; i++) {
TIFFField tiffField = allTiffFields[i];
/* name of property */
String nameOfField = tiffField.getTag().getName();
/* Tag no. of the property (optional) */
int numberOfField = tiffField.getTagNumber();
/* Type of property (optional) */
String typeOfField = TIFFField.getTypeName(tiffField.getType());
/* Value of Property*/
String valueOfField = tiffField.getValueAsString(0);
/* print it down as per your way */
System.out.println((i+1)+". " + nameOfField + ", " + numberOfField + ", " + typeOfField + ", " + valueOfField);
}
/* just for separate between two page (image) property */
System.out.println("======================================");
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
What more you need to do:
- Create a java file. (Let’s assume the name of class is “TIFFPageProperties” or you can go with your conventions)
- Copy and paste the methods inside (“TIFFPageProperties”/?) java class
- Write a main method which will pass a parameter to readTiffImageProperties method. Pass the Tiff image file path as a parameter to this method.
- Compile and Run the code
References:
Java SE Technical Documentation: Oracle Java Docs - javase
Note: For detailed information visit Oracle documentation. Any question or suggestions from your side are always welcome.