如何在Jframe上显示jpeg2000图像? [英] How to display a jpeg2000 image on a Jframe?

查看:179
本文介绍了如何在Jframe上显示jpeg2000图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中的文件和DataInputStream对象imgobj上有一个jpeg2000图像,img.jp2,并且想要在JFrame上显示该图像.

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.

旧版本 jai_imageio-1.1.jar 推荐在此处 jj2000 库都包含在内.

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

我尝试过:

    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:

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

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

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?

感谢您的帮助.

编辑

根据评论的要求,这就是我所做的:

AS requested in comments, this is what I did:

         //1   
         FaceImageInfo imgfn = fsinf.getFaceImageInfos().get(0);
         BufferedImage bf=ImageIO.read(imgfn.getImageInputStream());

         ImageIcon iconImg = new ImageIcon();             
         iconImg= new ImageIcon(bf);// if this fails try write it to a stream and read it back see //2
         JLabel(iconImg, JLabel.CENTER);


         //2
         ByteArrayOutputStream baos=null;
         try{
             baos=new ByteArrayOutputStream();
             ImageIO.write(bf, "jpg", baos);
         }
         finally{
             try{
                 baos.close();
             }
             catch(Exception ex)
             {
                 ex.printStackTrace();
             }
         }
         try {
             ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray());
             bf2=ImageIO.read(bais) ;
             iconImg= new ImageIcon(bf2);
             JLabel(iconImg, JLabel.CENTER);

        } catch (Exception e) {
            e.printStackTrace();
        }

推荐答案

假设代码以其他方式读取您想要的图像,您可以像这样从ImageReader轻松获得BufferedImage:

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

这篇关于如何在Jframe上显示jpeg2000图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆