用Java读写TIFF图像 [英] Reading and Writing out TIFF image in Java

查看:3264
本文介绍了用Java读写TIFF图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下代码来完成读取和编写tiff图像的任务:

I tried the following code to accomplish the task of reading and writing tiff images:

 // Define the source and destination file names.
 String inputFile = /images/FarmHouse.tif
 String outputFile = /images/FarmHouse.bmp

 // Load the input image.
 RenderedOp src = JAI.create("fileload", inputFile);

 // Encode the file as a BMP image.
 FileOutputStream stream =
     new FileOutputStream(outputFile);
 JAI.create("encode", src, stream, BMP, null);

 // Store the image in the BMP format.
 JAI.create("filestore", src, outputFile, BMP, null);

然而,当我运行代码时,收到以下错误消息:

However, when I run the code, I get the following error message:

Caused by: java.lang.IllegalArgumentException: Only images with either 1 or 3 bands 
can be written out as BMP files.
 at com.sun.media.jai.codecimpl.BMPImageEncoder.encode(BMPImageEncoder.java:123)
 at com.sun.media.jai.opimage.EncodeRIF.create(EncodeRIF.java:79)

我知道如何解决这个问题吗?

Any idea how I could solve this issue?

推荐答案

读取TIFF并输出BMP的最简单方法是使用ImageIO类:

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));

要让它发挥作用,唯一需要做的就是确保你已添加一些JAI ImageIO JAR到你的类路径,因为没有来自这个库的插件,JRE不处理BMP和TIFF。

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.

如果你不能使用JAI ImageIO原因是,您可以使用现有代码,但您必须做一些额外的工作。为正在加载的TIFF创建的颜色模型可能是BMP不支持的索引颜色模型。您可以使用JAI.KEY_REPLACE_INDEX_COLOR_MODEL键提供渲染提示,将其替换为JAI.create(format,...)操作。

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));

我想知道你是否遇到了与常规JAI相同的索引颜色模型问题。理想情况下,除了最简单的情况之外,您应该使用ImageIO类来获取ImageReader和ImageWriter实例,以便您可以相应地调整读写参数,但ImageIO.read()和.write()可以很精细地为您提供你想要什么。

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.

这篇关于用Java读写TIFF图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆