将上传的图像从各种格式转换为 JPEG
我正在开发一个允许用户上传图像的网站。我希望能够接受 BMP、GIF、JPEG 和 PNG(也许还有其他一两个,如 TIFF)格式。我正在使用 Apache Commons FileUpload 库来实现此目的。为了使所有图像最终采用相同的格式,我想将所有图像格式转换为 JPEG 格式。
对于上述所有图像格式都适用的最简单的方法是什么?
我尝试过:
- ImageIO.read(),它为某些 GIF 文件提供了
javax.imageio.IIOException: Unexpected block type 0!
和java.awt.color.CMMException: Invalid image format< /code> 用于 JPEG 文件。
- JAI,它为 GIF 文件提供了巨大的错误跟踪。
编辑:有关 JAI 方法的信息:
堆栈跟踪:
错误:一个工厂执行“gif”操作失败
发生在:javax.media.jai.ThreadSafeOperationRegistry
,后跟数百行跟踪。
代码:
public static void convertToJPG(String originalFile, String newFile) throws Exception {
RenderedImage image = JAI.create("fileload", originalFile);
JAI.create("filestore", image, newFile, "JPEG");
}
图片:
I am developing a website that allows the user to upload images. I would like to be able to accept BMP, GIF, JPEG and PNG (and maybe one or two others like TIFF) formats. I am using the Apache Commons FileUpload library to achieve this. In order that all the images end up in the same format, I would like to convert all image formats to JPEG format.
What is the easiest way to do this which will work will all of the above image formats?
I have tried:
- ImageIO.read(), which gave
javax.imageio.IIOException: Unexpected block type 0!
for some GIF files andjava.awt.color.CMMException: Invalid image format
for JPEG files. - JAI, which gave a humongous error trace for GIF files.
Edit: Information about the JAI method:
Stack trace:
Error: One factory fails for the operation "gif"
Occurs in: javax.media.jai.ThreadSafeOperationRegistry
followed by literally hundreds of lines of trace.
Code:
public static void convertToJPG(String originalFile, String newFile) throws Exception {
RenderedImage image = JAI.create("fileload", originalFile);
JAI.create("filestore", image, newFile, "JPEG");
}
Image:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请查看 ImageMagick,特别是转换命令。
Take a look at ImageMagick, and the convert command, in particular.