我的儿子正在研究Java项目,以显示文件中的JPEG2000图像。在此特定情况下,JPEG2000图像基于视频服务论坛TR-01标准中用于传输JPEG2000视频的广播配置文件。
他的代码基于此处的其他示例
How to display a jpeg2000 image on a Jframe?
,但是在显示此特定文件格式时遇到问题。其他示例图像文件(例如配置文件1或2)可以工作,但不能使用广播配置文件。他尝试显示的文件格式可以在Kakadu查看器中正常工作,因此似乎不支持广播配置文件。有没有人有从Java显示广播配置文件的经验。
编辑: 从那以后,他发现了另一种方法(从外部调用openjpeg“ opj_decompress”以将JPEG2000转换为bmp),但这基本上是他正在尝试的代码。使其比使用外部函数更整洁地工作仍然会很好:
package javaapplication1;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;
import javax.imageio.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import jj2000.j2k.*;
import com.sun.media.imageioimpl.plugins.jpeg2000.*;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.ImageInputStream;
import javax.swing.ImageIcon;
/**
*
*/
public class JavaApplication1 {
public static JFrame mainFrame;
public static JLabel headerLabel;
public static JLabel statusLabel;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
prepareGUI();
//JLabel hold = new JLabel("hold");
}
private static void prepareGUI() throws IOException{
J2KImageReaderSpi j2kspi = new J2KImageReaderSpi();
J2KImageReader j2kImageReader = new J2KImageReader(j2kspi);
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(1000,1000);
mainFrame.setLayout(new GridLayout(3, 8));
headerLabel = new JLabel("", JLabel.CENTER);
try (ImageInputStream input = ImageIO.createImageInputStream(new File("file1.j2k")))
{
j2kImageReader.setInput(input);
ImageReadParam imageReadParam = j2kImageReader.getDefaultReadParam();
BufferedImage image = j2kImageReader.read(0, imageReadParam);
ImageIcon imgIcon = new ImageIcon(image);
JLabel label = new JLabel(imgIcon);
mainFrame.add(label);
mainFrame.add(headerLabel);
//mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
}
}
引发的错误是:
Exception in thread "main" java.lang.RuntimeException: Codestream capabiities not JPEG 2000 - Part I compliant
at com.sun.media.imageioimpl.plugins.jpeg2000.J2KReadState.initializeRead(J2KReadState.java:696)
at com.sun.media.imageioimpl.plugins.jpeg2000.J2KReadState.<init>(J2KReadState.java:189)
at com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReader.read(J2KImageReader.java:443)
at javaapplication1.JavaApplication1.prepareGUI(JavaApplication1.java:60)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:37)
Caused by: java.lang.Error: Codestream capabiities not JPEG 2000 - Part I compliant
at jj2000.j2k.codestream.reader.HeaderDecoder.readSIZ(HeaderDecoder.java:527)
at jj2000.j2k.codestream.reader.HeaderDecoder.readFoundMainMarkSeg(HeaderDecoder.java:2164)
at jj2000.j2k.codestream.reader.HeaderDecoder.<init>(HeaderDecoder.java:2368)
at com.sun.media.imageioimpl.plugins.jpeg2000.J2KReadState.initializeRead(J2KReadState.java:423)
... 4 more