【问题标题】:Compressing and converting a jpg to tiff in Java在 Java 中将 jpg 压缩并转换为 tiff
【发布时间】:2018-11-19 21:41:45
【问题描述】:

我有一个 jpg 图像,我想将其转换为 tiff 文件,但是当我从 byteArrayOutputStream 创建输出文件时,输出文件的字节长度为 0。

public static void main(String[] args) throws Exception {
    String root = "E:\\Temp\\imaging\\test\\";

    File image = new File(root + "0riginalTif-convertedToJpg.JPG");

    byte[] bytes = compressJpgToTiff(image);
    File destination = new File(root + "OriginalJpg-compressedToTiff.tiff");
    FileOutputStream fileOutputStream = new FileOutputStream(destination);
    fileOutputStream.write(bytes);
}


public static byte[] compressJpgToTiff(File imageFile) throws Exception {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(255);
    ImageOutputStream imageOutputStream = null;
    try {
        File input = new File(imageFile.getAbsolutePath());

        Iterator<ImageWriter> imageWriterIterator = ImageIO.getImageWritersByFormatName("TIF");
        ImageWriter writer = imageWriterIterator.next();
        imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);
        writer.setOutput(imageOutputStream);

        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionType("JPEG");
        param.setCompressionQuality(0.1f);

        BufferedImage bufferedImage = ImageIO.read(input);
        writer.write(null, new IIOImage(bufferedImage, null, null), param);
        writer.dispose();
        return byteArrayOutputStream.toByteArray();

    } catch (Exception e) {

        throw new RuntimeException(e);
    } finally {
        if (imageOutputStream != null)
            imageOutputStream.close();
        byteArrayOutputStream.close();
    }
}

我想尽可能减小输出 tiff 的大小。有更好的方法吗?甚至可以减小 tiff 图像的大小吗?

【问题讨论】:

标签: java image jpeg tiff


【解决方案1】:

return byteArrayOutputStream.toByteArray(); 但您没有将数据写入byteArrayOutputStream。看,你刚刚将数据添加到writer。

关于tiff文件的压缩,你已经用了-param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

【讨论】:

  • 我做到了:imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream); writer.setOutput(imageOutputStream);
【解决方案2】:

在您使用byteArrayOutputStream.toByteArray() 将 byteArrayOutputStream 转换为 byteArray 之前,您的 byteArrayOutputStream 对象将在 finally 块中关闭,这就是为什么您将内容长度设为 0强>。所以修改你的代码一次如下:

public static byte[] compressJpgToTiff(File imageFile) throws Exception {
//Add rest of your method code here
writer.dispose();
byte[] bytesToReturn = byteArrayOutputStream.toByteArray();
return bytesToReturn;
} catch (Exception e) {
    throw new RuntimeException(e);
} finally {
    if (imageOutputStream != null)
        imageOutputStream.close();
    byteArrayOutputStream.close();
}
}

【讨论】:

  • 感谢您的回答,但显然这不是原因。
  • 在这种情况下,只需逐行调试自己,看看流在哪里丢失或到达 EOF。但只是看到你的代码首先想到的是finally block which runs before your return statement 关闭你的流对象而不是返回 toByte 部分,这显然没有任何东西可以转换为字节。所以再检查一遍。
  • 我已经这样做了,但我仍然无法弄清楚。 byteArrayOutputStream 永远不会填充数据。
猜你喜欢
  • 1970-01-01
  • 2012-12-29
  • 2010-11-15
  • 2012-08-13
  • 1970-01-01
  • 2012-03-21
  • 1970-01-01
  • 1970-01-01
  • 2022-12-14
相关资源
最近更新 更多