gpt4 book ai didi

java - 如何在Jframe上显示jpeg2000图像?

转载 作者:行者123 更新时间:2023-12-01 11:12:22 24 4
gpt4 key购买 nike

我的项目中的文件和 DataInputStream 对象 imgobj 上有一个 jpeg2000 图像 img.jp2,并且希望在 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   
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,如下所示:

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

关于java - 如何在Jframe上显示jpeg2000图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32203794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com