使用ImageIO编写带有图层的JPEG 2000(即分解级别)

时间:2013-07-26 18:32:48

标签: java javax.imageio jpeg2000

好的,这是我们的问题:

我们正在尝试使用imageio将一系列黑白.tiff文件转换为jpeg2000 .jpf文件。我们总是获得可查看的.jpf文件,但它们通常没有指定数量的图层或分解级别进行缩放。

这是我们的代码:

 //Get the tiff reader
 Iterator<ImageReader> readerIterator = ImageIO.getImageReadersByFormatName("tiff");
 ImageReader tiffreader = readerIterator.next();

 //make an ImageInputStream from our tiff file and have the tiff reader read it
 ImageInputStream iis = ImageIO.createImageInputStream(itemFile);

 tiffreader.setInput(iis);

 //just pass empty params to the tiff reader
 ImageReadParam tparam;
 tparam = new TIFFImageReadParam();

 IIOImage img = tiffreader.readAll(0, tparam);


 //set up target file
 File f = new File(itemTargetDirectory.getAbsolutePath() + "/" + destFileName);

 //we have tried FILTER_97 as well as different ProgressionTypes and compression settings
 J2KImageWriteParam param;
 param = new J2KImageWriteParam();
 param.setProgressionType("layer");
 param.setFilter(J2KImageWriteParam.FILTER_53);
 //Our problem is that this param is not always respected in the resulting .jpf
 param.setNumDecompositionLevels(5);

 //get the JPEG 2000 writer                             
 Iterator<ImageWriter> writerIterator = ImageIO.getImageWritersByFormatName("JPEG 2000");
 J2KImageWriter jp2kwriter = null;
 jp2kwriter = (J2KImageWriter) writerIterator.next();

 //write the jpf file
 ImageOutputStream ios = ImageIO.createImageOutputStream(f);
 jp2kwriter.setOutput(ios);
 jp2kwriter.write(null, img, param);

这是一种奇怪的体验,因为相同的代码在后续运行中表现不同。 任何见解将不胜感激!

2 个答案:

答案 0 :(得分:1)

所有TIFF文件是否都具有相同的设置(颜色模型)? J2KImageWriter.java显示当索引颜色或多像素打包的源图像用作输入时,分解级别被设置(强制)为零。

答案 1 :(得分:0)

Drew走在正确的轨道上,这里的代码最终为我们排序:

 public void compressor(String inputFile, String outputFile) throws IOException {
    J2KImageWriteParam iwp = new J2KImageWriteParam();
    FileInputStream fis = new FileInputStream(new File(inputFile));
    BufferedImage image = ImageIO.read(fis);
    fis.close();

    if (image == null) 
    {
        System.out.println("If no registered ImageReader claims to be able to read the resulting stream");
    }

    Iterator writers = ImageIO.getImageWritersByFormatName("JPEG2000");
    String name = null;
    ImageWriter writer = null;
    while (name != "com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriter") {
        writer = (ImageWriter) writers.next();
        name = writer.getClass().getName();
        System.out.println(name);
    }
    File f = new File(outputFile);
    long s = System.currentTimeMillis();
    ImageOutputStream ios = ImageIO.createImageOutputStream(f);
    writer.setOutput(ios);
    J2KImageWriteParam param = (J2KImageWriteParam) writer.getDefaultWriteParam();
    IIOImage ioimage = new IIOImage(image, null, null);
    param.setSOP(true);
    param.setWriteCodeStreamOnly(true);
    param.setProgressionType("layer");
    param.setLossless(false);
    param.setCompressionMode(J2KImageWriteParam.MODE_EXPLICIT);
    param.setCompressionType("JPEG2000");
    param.setCompressionQuality(0.1f);
    param.setEncodingRate(1.01);
    param.setFilter(J2KImageWriteParam.FILTER_97);


    writer.write(null, ioimage, param);
    System.out.println(System.currentTimeMillis() - s);
    writer.dispose();
    ios.flush();
    ios.close();

    image.flush();
}