英文:
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<String, Object> params = new HashMap<>();
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("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"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论