ImageIO处理图片时Unsupported Image Type

来源:互联网 发布:centos命令 desktop 编辑:程序博客网 时间:2024/05/02 02:49

异常代码

    BufferedImage bi = ImageIO.read(inputStream);

异常信息

javax.imageio.IIOException: Unsupported Image Type        at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1068)        at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1039)        at javax.imageio.ImageIO.read(ImageIO.java:1448)        at javax.imageio.ImageIO.read(ImageIO.java:1352)

异常原因

ps或其他软件处理过的图片保存为jpg格式时,默认的模式是CMYK模式(这是给印刷机用的)。

解决方案

方案一

从图片本身着手,使用相关软件将其渲染格式改为RGB格式。

方案二

从代码角度进行修改,对图片解码和解码工具进行修改,使用:twelvemonkeys。

在maven依赖中导入相应jar包即可,一般情况下不需要在代码上进行修改。

<dependency>    <groupId>com.twelvemonkeys.imageio</groupId>    <artifactId>imageio-jpeg</artifactId>    <version>3.3.2</version></dependency>

附一:twelvemonkeys简介

TwelveMonkeys ImageIO is a collection of plugins and extensions for Java’s ImageIO.

These plugins extends the number of image file formats supported in Java, using the javax.imageio.* package. The main purpose of this project is to provide support for formats not covered by the JRE itself.

Support for formats is important, both to be able to read data found “in the wild”, as well as to maintain access to data in legacy formats. Because there is lots of legacy data out there, we see the need for open implementations of readers for popular formats. The goal is to create a set of efficient and robust ImageIO plug-ins, that can be distributed independently.

TwelveMonkeys ImageIO 是 Java 的 ImageIO 的插件和扩展的集合。

这些插件使用 javax. imageio 包扩展了 Java 中支持的图像文件格式的数量。此项目的主要目的是为 JRE 本身不包括的格式提供支持。

对格式的支持非常重要, 既可以读取 “野外” 中的数据, 也可以保持对旧式格式数据的访问。由于存在大量的遗留数据, 我们认为需要对流行格式的读者开放实现。目标是创建一套高效、健壮的 ImageIO插件, 可以独立分布。

官网详情:https://haraldk.github.io/TwelveMonkeys/

附二:RGB色彩和CMYK色彩的区别和换算

  • RGB色彩

    自然界中绝大部分的可见光谱可以用红、绿和蓝三色光按不同比例和强度的混合来表示。RGB分别代表着3种颜色:R代表红色,G代表绿色、B代表蓝色。RGB模型也称为加色模型,通常用于光照、视频和屏幕图像编辑。RGB色彩模式使用RGB模型为图像中每一个像素的RGB分量分配一个0~255范围内的强度值。

  • CMYK色彩

    CMYK色彩模式以打印油墨在纸张上的光线吸收特性为基础,图像中每个像素都是由靛青(C)、品红(M)、黄(Y)和黑(K)色按照不同的比例合成。每个像素的每种印刷油墨会被分配一个百分比值,最亮(高光)的颜色分配较低的印刷油墨颜色百分比值,较暗(暗调)的颜色分配较高的百分比值。

  • CMYK转RGB的换算方法

R = 255*(100-C)*(100-K)/10000G = 255*(100-M)*(100-K)/10000B = 255*(100-Y)*(100-K)/10000
  • RGB转CMYK的换算方法
    这个转换理论上是不存在的,主要是因为K值的存在。当k=0时的公式为:
C = 100 - R * 100 / 255M = 100 - G * 100 / 255Y = 100 - B * 100 / 255