RenderedImage 到 BufferedImage 以进行多页 tiff 读取
我正在使用 JAI 加载多页 TIFF 图像
File file = workArea[0];
SeekableStream s = new FileSeekableStream(file);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
//first page
RenderedImage op1 =
new NullOpImage(dec.decodeAsRenderedImage(0),
null,
OpImage.OP_IO_BOUND,
null);
BufferedImage pg1 = new BufferedImage(op1.getWidth(), op1.getHeight(),
BufferedImage.TYPE_INT_RGB);
pg1.getGraphics().drawImage((Image) op1, 0, 0, null);
但是,在最后一行中我收到运行时错误:
Exception in thread "main" java.lang.ClassCastException:
javax.media.jai.MullOpImage cannot be cast to java.awt.Image
我在尝试设置 BufferedImage 后清除了 RenderedImage,因此如果有另一种方法,我并不完全“需要”RenderedImage做这个。
我尝试过:
pg1.setData(op1.getData());
这给出了 ArrayIndexOutOfBoundsException。我不确定为什么 pg1 的宽度和高度是由 op1 设置的,但可能有一个非常有效的原因。
I am using JAI to load in multipage TIFF images
File file = workArea[0];
SeekableStream s = new FileSeekableStream(file);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
//first page
RenderedImage op1 =
new NullOpImage(dec.decodeAsRenderedImage(0),
null,
OpImage.OP_IO_BOUND,
null);
BufferedImage pg1 = new BufferedImage(op1.getWidth(), op1.getHeight(),
BufferedImage.TYPE_INT_RGB);
pg1.getGraphics().drawImage((Image) op1, 0, 0, null);
However, in the last line I get a runtime error of:
Exception in thread "main" java.lang.ClassCastException:
javax.media.jai.MullOpImage cannot be cast to java.awt.Image
I clear the RenderedImage after attempting to set the BufferedImage so I don't exactly "need" the RenderedImage if there is another method of doing this.
I attempted:
pg1.setData(op1.getData());
and that gives an ArrayIndexOutOfBoundsException. I'm not sure why exactly as pg1's width and height are set by op1's, but there is probably a very valid reason.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我在 http://www.jguru.com/faq/view.jsp?EID=114602 找到了解决方案
第一个不起作用,但是,convertRenderedImage 函数确实起作用了。
I found a solution at http://www.jguru.com/faq/view.jsp?EID=114602
The first one didn't work, however, the convertRenderedImage function did work.
使用 op1。
getAsBufferedImage()
创建 pg1。Use op1.
getAsBufferedImage()
to create pg1.如果您遇到 RenderedImage 问题,可以使用
此处 查看文档
If you are stuck with a RenderedImage, you can use
see here for documentation
JAI 显然有一个“转换器”类:
ref: http://www.programcreek.com/java-api-examples/index.php?api=com.sun.media.jai.codec.ImageDecoder
JAI apparently has a "converter" class in there:
ref: http://www.programcreek.com/java-api-examples/index.php?api=com.sun.media.jai.codec.ImageDecoder
试试这个:
Try this :
加载 TIFF 最简单的方法是使用 Twelve Monkey 并提供插件来支持 TIFF 格式到标准 Java ImageIO。
只需添加以下 Maven 依赖项,
现在,您将能够使用 ImageIO 直接加载 TIFF 文件。
它非常简单且可靠,因为它使用标准 Java ImageIO API 进行所有处理。
只有十二猴子的插件提供了SPI插件来提供TIFF的支持。
此处添加一个正在运行的示例程序是 Java 8,它读取 TIFF 文件并创建一个多页 TIFF 文件:
它正在使用 Java 8,如果有人想要添加压缩,只需注释该行并添加适当的压缩名称即可。
The easiest way to load TIFF is using Twelve Monkey with provide plugin to support TIFF format into Standard Java ImageIO.
Just add below Maven dependency,
Now, you will be able to load TIFF file directly Using ImageIO.
It is very easy and reliable because it uses standard Java ImageIO API for all processing.
Only the Plugin from Twelve Monkey provide the SPI plugin to provide support of TIFF.
Adding here one example program which is running is Java 8 and it reads TIFF files and create a single multi-page TIFF file:
It is working with Java 8, if someone wants to add compression just comment the line and add appropriate compression name.