RenderedImage to BufferedImage for multipage-tiff reading [英] RenderedImage to BufferedImage for multipage-tiff reading
问题描述
我正在使用JAI加载多页TIFF图像
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
我在尝试设置BufferedImage后清除了RenderedImage,所以我并不完全需要RenderedImage这是另一种方法。
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.
我尝试过:
pg1.setData(op1.getData());
,这给出了一个ArrayIndexOutOfBoundsException。我不确定为什么pg1的宽度和高度完全由op1设置,但可能有一个非常正确的原因。
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.
推荐答案
使用op1。 getAsBufferedImage()
创建pg1。
Use op1.getAsBufferedImage()
to create pg1.
这篇关于RenderedImage to BufferedImage for multipage-tiff reading的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!