Can't Read and Write a Tiff Image File Using Java Imageio Standard Library

Java: Can't read Tiff image file

This ended up being a problem with loading .tiff files in pure Java. Installing TwelveMonkeys ImageIO plugin did the trick. Thanks again, especially to @IlarioPierbattista, who directed me to the solution!

Reading and Writing out TIFF image in Java

The easiest way to read in a TIFF and output a BMP would be to use the ImageIO class:

BufferedImage image = ImageIO.read(inputFile);
ImageIO.write(image, "bmp", new File(outputFile));

The only additional thing you would need to do to get this to work is make sure you've added the JAI ImageIO JARs to your classpath, since BMP and TIFF are not handled by the JRE without the plugins from this library.

If you can't use JAI ImageIO for some reason, you can get it to work with your existing code but you'll have to do some additional work. The color model that is being created for the TIFF that you are loading is probably an indexed color model which is not supported by a BMP. You can replace it with the JAI.create("format",...) operation by providing a rendering hint with a key of JAI.KEY_REPLACE_INDEX_COLOR_MODEL.

You may have some luck writing the image read from file into a temporary image and then writing out the temp image:

BufferedImage image = ImageIO.read(inputFile);
BufferedImage convertedImage = new BufferedImage(image.getWidth(),
image.getHeight(), BufferedImage.TYPE_INT_RGB);
convertedImage.createGraphics().drawRenderedImage(image, null);
ImageIO.write(convertedImage, "bmp", new File(outputFile));

I'm wondering if you're running into the same index color model issue as with the regular JAI. Ideally you should be using the ImageIO class to get ImageReader and ImageWriter instances for all but the simplest cases so that you can tweak the read and write params accordingly, but the ImageIO.read() and .write() can be finessed to give you what you want.

How can I process with a .tif image?

I guess you will need to use the JAI (Java advances imaging package), take a look at this, and see this example

Issue with reading Tiff image metadata with imageIO

For anyone that would like to know I ended up fixing my own issue. It seems the the image metadata was a bit screwed up. Since I was just doing a plain merge and since I knew each image was one page I was able to use a buffered image to read in the picture then make it a IIOImage with null metadata. I used the stream metadata (which worked) to merge the images. Here is my complete method I use to merge a list of images:

public static File mergeImages(List<File> files, String argID, String fileType, String compressionType) throws Exception{

//find the temp location of the image
String location = ConfigManager.getInstance().getTempFileDirectory();
logger_.debug("image file type [" + fileType + "]");
ImageReader reader = ImageIO.getImageReadersByFormatName(fileType).next();
ImageWriter writer = ImageIO.getImageWritersByFormatName(fileType).next();
//set up the new image name
String filePath = location + "\\" + argID +"." + fileType;
//keeps track of the images we copied from
StringBuilder builder = new StringBuilder();
List<IIOImage> bufferedImages = new ArrayList<IIOImage>();
IIOMetadata metaData = null;
for (File imageFile:files) {

//get the name for logging later
builder.append(imageFile.getCanonicalPath()).append("\n");
if (metaData == null){
reader.setInput(ImageIO.createImageInputStream(imageFile));
metaData = reader.getStreamMetadata();

}

BufferedImage image = ImageIO.read(imageFile);
bufferedImages.add(new IIOImage(image, null, null));
}

ImageWriteParam params = writer.getDefaultWriteParam();
if (compressionType != null){
params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
params.setCompressionType(compressionType);
}

ImageOutputStream outStream = null;

try{
outStream = ImageIO.createImageOutputStream(new File(filePath));
int numPages = 0;
writer.setOutput(outStream);
for(IIOImage image:bufferedImages){
if (numPages == 0){
writer.write(metaData, image, params);
}
else{
writer.writeInsert(numPages, image, params);
}
numPages++;
}
}
finally{
if (outStream != null){
outStream.close();
}

}

//set up the file for us to use later
File mergedFile = new File(filePath);

logger_.info("Merged image into [" + filePath + "]");
logger_.debug("Merged images [\n" + builder.toString() + "] into --> " + filePath);

return mergedFile;

}

I hope this help someone else because I know there isn't much on this issue that I could find.



Related Topics



Leave a reply



Submit