2

好的,这是我们的问题:

我们正在尝试使用 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);

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

4

2 回答 2

1

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

于 2013-08-05T19:10:57.470 回答
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();
}
于 2013-08-06T15:35:02.670 回答