在 Java 中读取和写入 TIFF 图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2898311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:15:58  来源:igfitidea点击:

Reading and Writing out TIFF image in Java

javatiff

提问by user224270

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

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

 // 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?

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

采纳答案by Jeff

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

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

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.

要使其工作,您唯一需要做的额外事情是确保您已将 JAI ImageIO JAR 添加到您的类路径中,因为如果没有来自该库的插件,JRE 不会处理 BMP 和 TIFF。

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.

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

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.

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

回答by SamVish

FileInputStream in = new FileInputStream(imgFullPath);
FileChannel channel = in.getChannel();
ByteBuffer buffer = ByteBuffer.allocate((int)channel.size());
channel.read(buffer);
tiffEncodedImg = Base64.encode(buffer.array()); 

Use this contents(i.e value of "tiffEncodedImg") as src value of img tag in HTML

将此内容(即“tiffEncodedImg”的值)用作 HTML 中 img 标签的 src 值