Apache Commons Imaging-将TIFF转换为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:无法写入此图像格式(Jpeg-Custom)。

我不明白自己在做错什么,有人可以帮忙吗?

解决方法

尝试使用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,Color.WHITE,null);
  // Write the image as JPEG to disk
  ImageIO.write(jpegImage,"jpg",new File("jpeg-image.jpg"));

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...