如何在Jframe上显示jpeg2000图像?

时间:2015-08-25 12:16:21

标签: java swing jpeg2000

我在一个文件上有一个jpeg2000图片,img.jp2,在我的项目中有一个DataInputStream对象imgobj,并希望在JFrame上显示该图像。

旧版jai_imageio-1.1.jar推荐herejj2000库都包含在内。

我试过了:

    j2kImageReader.setInput(new FileImageInputStream(new File(fileName)));
            ImageReadParam imageReadParam = j2kImageReader.getDefaultReadParam();
imageReadParam.setSourceRegion(new Rectangle(0, 0, 300, 300));
            IIOImage image = j2kImageReader.readAll(0, imageReadParam); 

       // This type of images is difficult to handle, 
       // I just don't know what to do with IIOImage, 
       // ImageIcon doesn't accept that type in its constructor.

而且:

    Image img = ImageIO.read(new File(fileName));
ImageIcon imgIcon = new ImageIcon(img);

JLabel label = new JLabel(imgIcon);
panel1.add(label);
panel1.repaint();

//Error: Can't read input file!. The panel is still empty

JMRTD中包含的选项使用两个解码器,其中没有一个接受.jp2

NistDecoder dec=new NistDecoder();
WsqDecoder wdec=new WsqDecoder();

//using the last one, I tried: bitmp= wdec.decode(myDataInputStream);
//but with Error, Invalid pointer : 0!.

所以问题是:jj2000或jai_imageio从文件或DataInputStream中读取jpeg2000图像的正确用法是什么,如果可能的话,在JFrame上的简单面板上显示它?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

假设代码以其他方式读取您想要的图像,您可以轻松地从BufferedImage获取ImageReader,如下所示:

try (ImageInputStream input = ImageIO.createImageInputStream(new File(fileName))) {
    j2kImageReader.setInput(input));

    // Not sure why/if you want to read only the upper left, but I'll leave it as is
    ImageReadParam imageReadParam = j2kImageReader.getDefaultReadParam();
    imageReadParam.setSourceRegion(new Rectangle(0, 0, 300, 300)); 

    // Use read instead of readAll
    BufferedImage image = j2kImageReader.read(0, imageReadParam); 

    // You can now create an icon and add to a component
    Icon icon = new ImageIcon(image);
    JLabel label = new JLabel(icon);

    // Etc...
}