Apache Commons Imaging – 将tiff转换为jpg

huangapple go评论62阅读模式
英文:

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: 无法写入此图像格式(Jpeg-Custom)。

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

英文:

I need to convert a tiff image into a jpg one using Apache Commons Imaging.
I tried to but I can't figure out how to do it using this library.

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

Where image is my tiff file to be converted, but I get
> org.apache.commons.imaging.ImageWriteException: This image format (Jpeg-Custom) cannot be written.

I don't understand what I'm doing wrong could someone help?

答案1

得分: 3

尝试使用 Java AWT:

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

和代码部分:

// 读取 TIFF 图像文件
BufferedImage tiffImage = ImageIO.read(new File("tiff-image.tiff"));
// 在写入之前准备图像 - 使用相同的尺寸
BufferedImage jpegImage = new BufferedImage(
        tiffImage.getWidth(),
        tiffImage.getHeight(),
        BufferedImage.TYPE_INT_RGB);
// 从原始 TIFF 图像绘制到新的 JPEG 图像
jpegImage.createGraphics().drawImage(tiffImage, 0, 0, Color.WHITE, null);
// 将图像以 JPEG 格式写入磁盘
ImageIO.write(jpegImage, "jpg", new File("jpeg-image.jpg"));
英文:

Try the use of java AWT:

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

And code:

  // TIFF image file read
  BufferedImage tiffImage = ImageIO.read(new File(&quot;tiff-image.tiff&quot;));
  // 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, &quot;jpg&quot;, new File(&quot;jpeg-image.jpg&quot;));

huangapple
  • 本文由 发表于 2020年9月25日 22:32:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64066151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定