How to display a jpeg2000 image on a Jframe?

牧云@^-^@ 提交于 2019-12-13 04:09:15

问题


I have a jpeg2000 image, img.jp2 on a file, and on a DataInputStream object imgobj in my project, and want to show that image on a JFrame.

The old version jai_imageio-1.1.jar recommended here and the jj2000 library are both included.

I have tried:

    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.

And this:

    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

The option included in JMRTD is using two decoders, and no one of them accepts .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!.

So the question is: what is the proper use of jj2000 or jai_imageio to read a jpeg2000 image from a file or a DataInputStream, and if it is possible, to show it on a simple panel on a JFrame?

Thank you for help.


回答1:


Assuming the code otherwise reads the image like you want it, you can easily get a BufferedImage from the ImageReader like this:

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...
}


来源:https://stackoverflow.com/questions/32203794/how-to-display-a-jpeg2000-image-on-a-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!