java RenderedImage 到 BufferedImage 用于多页 tiff 读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7044521/
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-10-30 18:26:35  来源:igfitidea点击:

RenderedImage to BufferedImage for multipage-tiff reading

javajai

提问by Robert

I am using JAI to load in multipage TIFF images

我正在使用 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);

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.

我在尝试设置 BufferedImage 后清除了 RenderedImage,因此如果有另一种方法可以做到这一点,我并不完全“需要”RenderedImage。

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.

这给出了一个 ArrayIndexOutOfBoundsException。我不确定为什么 pg1 的宽度和高度完全由 op1 设置,但可能有一个非常有效的理由。

采纳答案by Jeffrey

Use op1.getAsBufferedImage()to create pg1.

使用op1。getAsBufferedImage()创建 pg1。

回答by Robert

I found a solution at http://www.jguru.com/faq/view.jsp?EID=114602

我在http://www.jguru.com/faq/view.jsp?EID=114602找到了解决方案

The first one didn't work, however, the convertRenderedImage function did work.

第一个不起作用,但是, convertRenderedImage 函数起作用了。

public BufferedImage convertRenderedImage(RenderedImage img) {
    if (img instanceof BufferedImage) {
        return (BufferedImage)img;  
    }   
    ColorModel cm = img.getColorModel();
    int width = img.getWidth();
    int height = img.getHeight();
    WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    Hashtable properties = new Hashtable();
    String[] keys = img.getPropertyNames();
    if (keys!=null) {
        for (int i = 0; i < keys.length; i++) {
            properties.put(keys[i], img.getProperty(keys[i]));
        }
    }
    BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties);
    img.copyData(raster);
    return result;
}

回答by heisbrandon

If you are stuck with a RenderedImage, you can use

如果您坚持使用 RenderedImage,则可以使用

PlanarImage.wrapRenderedImage(renderedImage).getAsBufferedImage() 

see herefor documentation

请参阅此处获取文档

回答by rogerdpack

JAI apparently has a "converter" class in there:

JAI 显然在那里有一个“转换器”类:

ImageDecoder dec = ImageCodec.createImageDecoder("PNM", new File(input), null);
return new RenderedImageAdapter(dec.decodeAsRenderedImage()).getAsBufferedImage()

ref: http://www.programcreek.com/java-api-examples/index.php?api=com.sun.media.jai.codec.ImageDecoder

参考:http: //www.programcreek.com/java-api-examples/index.php?api= com.sun.media.jai.codec.ImageDecoder

回答by Ankursonikajen

Try this :

试试这个 :

   RenderedImage im =  dec.decodeAsRenderedImage();
   BufferedImage bi = PlanarImage.wrapRenderedImage(im).getAsBufferedImage();