【问题标题】:How to display a jpeg2000 image on a Jframe?如何在 Jframe 上显示 jpeg2000 图像?
【发布时间】:2015-08-25 12:16:20
【问题描述】:

我有一个 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 的简单面板上?

感谢您的帮助。

编辑

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

         //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();
        }

【问题讨论】:

  • ...当您尝试上述方法时,究竟发生了什么?
  • 代码里已经解释过了,谢谢@haraldK。
  • 嗨。请与我分享您的解决方案。你用的是什么 maven 或 jar 文件?
  • @AliHoseinpour 查看编辑,希望对您有所帮助,谢谢。

标签: java swing jpeg2000


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多