ImageIO压缩图像的Java问题

时间:2013-10-28 12:55:38

标签: java javax.imageio

我正在完成一个可以压缩多页tiff图像的java应用程序。问题是,如果我将它作为.jar文件运行,这在我的IDE内以及同一台计算机上的本地环境中工作得非常好,但是如果我尝试在另外安装了java的计算机上运行它,它会压缩tiff文件,但这些图像不再可见。点击它们会显示Windows图像查看器的“无法预览”。可能是什么导致了这个?我用来压缩多页tiff的具体方法如下:

public synchronized void compress() throws IOException
{
    System.out.println("NUMpAGES: " + numPages);
    if(this.getSrcImageFile().length()<SIZE_THRESHOLD){
        this.closeAllStreams();
        return;
    }
    this.initOutStreams();
    this.setImageEncoder(ImageCodec.createImageEncoder("tiff", this.getOutputStream(), null));
    int compressionAlgorithm;
    if(bitDepth == 1 || bitDepth == 8)
    {
        /*Cant use JPEG_TTN2 with images that have less than 8-bit samples/only have a bit-depth of 1.*/   
        compressionAlgorithm = TIFFEncodeParam.COMPRESSION_DEFLATE;
        encodeParams.setCompression(compressionAlgorithm); //redundant with line above
    }
    else
    {

        System.out.println("Attempting to compress using JPEG_TTN2 with bit depth: " + bitDepth);
        compressionAlgorithm = TIFFEncodeParam.COMPRESSION_JPEG_TTN2;
        encodeParams.setCompression(compressionAlgorithm); //redundant with line above
    }
    this.setImageEncoder(ImageCodec.createImageEncoder("tiff", this.getOutputStream(), encodeParams));
    Vector vector = new Vector();
    if(numPages == 1){
         for(int i = 0; i < numPages - 1; i ++) //i < bufferedImages.length OLD
         {
              System.out.println(i);
              vector.add((bufferedImages[i]));
         } 
    }else{
         System.out.println("Using second case");
         for(int i = 0; i < numPages; i ++)
         {
              System.out.println("Adding to vector image for file " + this.getSrcImagePath() + " " + i);
              vector.add((bufferedImages[i]));
         }            
    }

    System.out.println("Buffered Images size: " + bufferedImages.length);

    Iterator vecIter = vector.iterator();
    if(numPages > 1){
        vecIter.next();
    }

    encodeParams.setExtraImages(vecIter);
    this.getImageEncoder().encode(bufferedImages[0]);
    closeAllStreams();
}

1 个答案:

答案 0 :(得分:0)

我会重复@mabi在第一篇(也是迄今为止唯一的好)评论中所说的话

  

JAI不附带JDK AFAIK。

我可以将AFAIK升级为“否”。 JAI是随JRE提供的

此外,用于处理TIFF的SPI(服务提供商接口)附带JAI。 ImageIO的基于JSE的部分使用SPI来识别编码器和编码器。解码器的文件类型。我怀疑是因为JAI没有安装在第二台计算机上(或以其他方式添加到应用程序的运行时类路径中),这就是它失败的原因。

至于为什么它在你的代码中没有明显失败,这可能是由于它抛出的异常是如何处理的。我怀疑他们只是被忽视了。

相关问题