Apache Commons Imaging - 将 tiff 转换为 jpg

Apache Commons Imaging - convert tiff to jpg

我需要使用 Apache Commons Imaging 将 tiff 图像转换为 jpg 图像。 我试过了,但我不知道如何使用这个库来做到这一点。

final BufferedImage image = Imaging.getBufferedImage(new File(image));
final ImageFormat format = ImageFormats.JPEG;
final Map<String, Object> params = new HashMap<>();
return Imaging.writeImageToBytes(image, format, params);

其中 image 是我要转换的 tiff 文件,但我得到

org.apache.commons.imaging.ImageWriteException: This image format (Jpeg-Custom) cannot be written.

我不明白我做错了什么有人可以帮忙吗?

尝试使用 java AWT:

   import java.awt.Color;
   import java.awt.image.BufferedImage;
   import java.io.File;
   import java.io.IOException;
   import javax.imageio.ImageIO;

和代码:

  // TIFF image file read
  BufferedImage tiffImage = ImageIO.read(new File("tiff-image.tiff"));
  // Prepare the image before writing - with same dimensions
  BufferedImage jpegImage = new BufferedImage(
          tiffImage.getWidth(),
          tiffImage.getHeight(), 
          BufferedImage.TYPE_INT_RGB);
  // Draw image from original TIFF to the new JPEG image
  jpegImage.createGraphics().drawImage(tiffImage, 0, 0, Color.WHITE, null);
  // Write the image as JPEG to disk
  ImageIO.write(jpegImage, "jpg", new File("jpeg-image.jpg"));