javax.imageio.IIOException: Unsupported Image Type异常
可以采用如下方案:

public class ImagesUtils {public static byte[] compressImage(File file, int ppi) {byte[] smallImage = null;try {BufferedImage bi = ImagesUtils.readImage(file);ByteArrayOutputStream out = new ByteArrayOutputStream();Thumbnails.of(bi).size(ppi, ppi).outputFormat("png").toOutputStream(out);smallImage = out.toByteArray();} catch (IOException e) {e.printStackTrace();}return smallImage;}public static BufferedImage readImage(File file) throws IOException {return readImage(ImageIO.createImageInputStream(file));}public static BufferedImage readImage(InputStream stream) throws IOException {return readImage(ImageIO.createImageInputStream(stream));}public static BufferedImage readImage(ImageInputStream input) throws IOException {Iterator<?> readers = ImageIO.getImageReaders(input);if (readers == null || !readers.hasNext()) {return null;}ImageReader reader = (ImageReader) readers.next();reader.setInput(input);BufferedImage image;try {// 尝试读取图片 (包括颜色的转换).image = reader.read(0); // RGB} catch (IIOException e) {// 读取Raster (没有颜色的转换).Raster raster = reader.readRaster(0, null);// CMYKimage = createJPEG4(raster);}return image;}private static BufferedImage createJPEG4(Raster raster) {int w = raster.getWidth();int h = raster.getHeight();byte[] rgb = new byte[w * h * 3];// 彩色空间转换float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null);float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null);float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null);float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null);for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) {float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i];double val = y + 1.402 * (cr - 128) - k;val = (val - 128) * .65f + 128;rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k;val = (val - 128) * .65f + 128;rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);val = y + 1.772 * (cb - 128) - k;val = (val - 128) * .65f + 128;rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);}raster = Raster.createInterleavedRaster(new DataBufferByte(rgb, rgb.length), w, h, w * 3, 3,new int[] { 0, 1, 2 }, null);ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);ColorModel cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);return new BufferedImage(cm, (WritableRaster) raster, true, null);}
}

javax.imageio.IIOException: Unsupported Image Type异常,解决方法相关推荐

  1. 图片处理 javax.imageio.IIOException: Unsupported Image Type

    2019独角兽企业重金招聘Python工程师标准>>> 图片处理 javax.imageio.IIOException: Unsupported Image Type 博客分类: j ...

  2. Thumbnails.of Could not obtain image from URL(javax.imageio.IIOException: Unsupported Image Type)

    Thumbnails在下载个别图片的时候报上面的错误,根据提示猜测是图片格式问题.经查看网上相关资料,发现是因为个别图片格式是CMYK导致的.java的api无法解析cmyk格式的图片,只能解析rgb ...

  3. javax.imageio.IIOException: Unsupported Image Type

    把图片的模式用ps从CMYK转为RGB就ok了. CMYK也称作印刷色彩模式,是一种依靠反光的色彩模式,和RGB类似,CMY是3种印刷油墨名称的首字母:青色Cyan.品红色Magenta.黄色Yell ...

  4. 图片压缩Unsupported Image Type异常,解决方法

    问题 需要对上传的大图片,在存储原文件后,另对它进行压缩存储作为缩略图.部分jpg图片在压缩时报错,导致没有压缩直接保存.网站页面展示这些5M左右的图片,打开变慢,影响使用 javax.imageio ...

  5. javax异常: javax.imageio.IIOException: Can't create output stream解决方法

    验证码工具类: 1 package com.newcapec.utils; 2 3 import com.sun.image.codec.jpeg.JPEGCodec; 4 import com.su ...

  6. 路径名导致的异常:javax.imageio.IIOException: Can‘t read input file!

    路径名导致的异常:javax.imageio.IIOException: Can't read input file! 参考文章: (1)路径名导致的异常:javax.imageio.IIOExcep ...

  7. javax.imageio.IIOException: Bogus marker length

    读取图片时抛异常,解决方法:删掉本地图片,重新跑测试,正常: 异常信息: javax.imageio.IIOException: Bogus marker lengthat com.sun.image ...

  8. base64转图片问题javax.imageio.IIOException: Invalid JPEG file structure: two SOI markers

    报错信息: javax.imageio.IIOException: Invalid JPEG file structure: two SOI markersat com.sun.imageio.plu ...

  9. variable ‘‘ of type ‘‘ referenced from scope ‘‘, but it is not defined 异常解决方法

    variable '' of type '' referenced from scope '', but it is not defined 异常解决方法 参考文章: (1)variable '' o ...

最新文章

  1. 爆气球这道题目,展开了新的思路
  2. php 微信第三方登录demo,第三方登录 - 快速接入微信、QQ、微博等第三方登录方式 – 基于ThinkPHP和Bootstrap的极速后台开发框架...
  3. 怎么学python-结合学习经历,谈一谈如何学习Python
  4. lstm 根据前文预测词_干货 | Pytorch实现基于LSTM的单词检测器
  5. linux 基本指令
  6. SqlServer和MySQL中存储过程out返回值处理C#代码
  7. MySQL字符集的基本类型与统一字符集
  8. 数据库常见面试题总结
  9. TensorFlow HOWTO 5.1 循环神经网络(时间序列)
  10. 第一次使用Pocket-PowerBuilder和开发DLL的经历
  11. HTML多选框,全选,多选
  12. 揭秘小红书的种草套路
  13. 【神经网络解耦】基于matlab的神经网络解耦系统仿真
  14. Beyong Compare3,4使用
  15. 计算机基础应用本终结性考试,计算机应用基础
  16. 如何利用路由器连接wifi并将wifi网络分发出去
  17. 【文献阅读1】Comparative cytological and transcriptomic analysis of pollen development in autotetraploid a
  18. Android studio app启动时默认启动页面为自定义界面
  19. intel 服务器芯片组 c6,华擎推出C621A WS工作站主板 支持志强W-3300处理器
  20. 浅析私有化即时通讯软件的用处有哪些

热门文章

  1. 02 HTML标签学习1
  2. python列表添加字符串_python字符串,列表常用操作
  3. RadioButton 和 CheckBox
  4. 上证50指数成分股列表
  5. H5练手项目-写一个菜鸟裹裹小程序
  6. 三维模型:3D互联网时代产品展示新趋势
  7. 什么是Xshell? 干啥用的?
  8. 3.4nbsp;企业家——3.4.1nbsp;史蒂…
  9. Windows小技巧
  10. python爬虫爬取古诗词内容,并存入mysql