用 Java 读取 JPEG [英] Reading JPEGs in Java
问题描述
我正在尝试将 JPEG 转换为 BufferedImage
以显示在 JPanel
上.但是,javax.imageio.ImageIO.read()
似乎对它处理的 JPEG 非常挑剔,经常抛出 IIOException
.几乎所有您想命名的图像阅读器都可以正常打开相同的 JPEG.
I'm attempting to get a JPEG into a BufferedImage
to display on a JPanel
. However, javax.imageio.ImageIO.read()
seems to be very fussy about the JPEGs it processes, often throwing an IIOException
. The same JPEGs open fine in pretty much any image reader you'd care to name.
我看过 Apache 的 Sanselan 和 JAI.但是 Sanselan 无法处理 JPEG,并且 JAI 不适用于 64 位 Windows 平台并且似乎没有得到维护(上次更新是在 2006 年).之前的 StackOverflow 上的答案 建议 com.sun.image.codec.jpeg.JPEGCodec
,但这在 Java 5 中已被弃用,并在 7 中消失.
I've looked at Apache's Sanselan and JAI. But Sanselan can't process JPEGs and JAI isn't available for 64-bit Windows platforms and doesn't seem to be maintained (the last update was in 2006). A previous answer on StackOverflow suggested com.sun.image.codec.jpeg.JPEGCodec
, but this was deprecated in Java 5 and has disappeared in 7.
这些是我唯一的选择吗?真的没有 Java 库能够稳健地将 JPEG 读取到 BufferedImage
中吗?
Are these my only options? Are there really no Java libraries capable of robustly reading JPEGs into a BufferedImage
?
推荐答案
Legacy Toolkit 方法,例如 createImage 和 getImage 比 ImageIO
更宽松.
Legacy Toolkit methods such as createImage and getImage are known to be more lenient than ImageIO
.
Werner Randelshofer 还写了一个 服务提供商使用 ImageIO
读取 CMYK JPEG.
Werner Randelshofer also wrote a Service Provider to read CMYK JPEGs with ImageIO
.
通过结合这两种方法(尝试所有可能的ImageReader
,然后回退到Toolkit
),您将能够处理合理数量的 JPEG 图像.
By combining both approaches (try every possible ImageReader
and then fallback to Toolkit
) you will be able to handle a reasonable number of JPEG Images.
使用 CMYK 配置文件读取 JPEG可能是一个有趣的阅读.
Reading JPEGs with CMYK profile may be a interesting read.
这篇关于用 Java 读取 JPEG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!