一、问题描述

今天收到一个bug就是imageio读取图片会返回null,具体如下

但是其他的图片就没有问题

二、问题分析

结合百度发现这张图片原本的后缀并非是jpg,使用notpard++打开就可以发现

好家伙是webp格式的!!!!

WebP是google开发的一种旨在加快图片加载速度的图片格式。图片压缩体积大约只有JPEG的2/3,并能节省大量的服务器带宽资源和数据空间。是现代图像格式,提供了优越的无损和有损压缩的图片在网络上。使用WebP,网站管理员和web开发人员可以创建更小、更丰富的图像,使网页更快。

WebP无损的png图像小26%。WebP有损图像是25 - 34%小于等效SSIM质量指数可比JPEG图像

无损WebP支持透明(也称为alpha通道)的成本只有22%额外的字节。对有损压缩RGB压缩情况下是可以接受的,有损WebP还支持透明度,通常提供3×PNG相比较小的文件大小。

但是!!!由于Webp格式推出比较晚, Jdk 内置的图片编解码库对此并不支持。我只需要知道如何把webp格式转换成jpg或者其他格式就可以了!!!

三、解决问题

1、在百度找到一个api(地址如下)

我是地址

友情建议,最好用0.1.0哪个版本的其他版本试了都有问题~~~~~~

2、由于这个项目并未发布到maven中央仓库,所以需要手动导入本地jar包.

(1)、maven导包

<dependency>
<groupId>com.github.nintha</groupId>
<artifactId>webp-imageio-core</artifactId>
<version>{versoin}</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/webp-imageio-core-{version}.jar</systemPath>
</dependency>

(2)、gradle导包

dependencies {
compile fileTree(dir:'src/main/resources/libs',include:['*.jar'])
}

当然了,你想放在自己仓库中也是无可厚非的

3、上代码

(1)、Webp编码

public static void main(String args[]) throws IOException {
String inputPngPath = "test_pic/test.png";
String inputJpgPath = "test_pic/test.jpg";
String outputWebpPath = "test_pic/test_.webp"; // Obtain an image to encode from somewhere
BufferedImage image = ImageIO.read(new File(inputJpgPath)); // Obtain a WebP ImageWriter instance
ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next(); // Configure encoding parameters
WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
writeParam.setCompressionMode(WebPWriteParam.MODE_DEFAULT); // Configure the output on the ImageWriter
writer.setOutput(new FileImageOutputStream(new File(outputWebpPath))); // Encode
writer.write(null, new IIOImage(image, null, null), writeParam);
}

(2)、Webp解码

public static void main(String args[]) throws IOException {
ImageReader reader = ImageIO.getImageReadersByMIMEType("image/webp").next(); WebPReadParam readParam = new WebPReadParam();
readParam.setBypassFiltering(true); // Configure the input on the ImageReader
reader.setInput(new FileImageInputStream(new File(webp图片地址))); // Decode the image
BufferedImage image = reader.read(0, readParam); ImageIO.write(image, "jpg", new File(输出jpg的地址,png什么的也可以));
}

搞完收工!

ImageIo.read 返回null的更多相关文章

  1. json_decode返回NULL

    最近在调用某公司的API时,将对方返回的数据,使用PHP的json_decode函数解析,但是返回NULL,最终排查为对方传送来的json格式有误 打印$_REQUEST,数据结构大致如下: arra ...

  2. $.parseJson 在 firefox 下返回 null 的问题

    最近调查一个浏览器兼容性问题,在 IE, chrome下都运行正常,但是在 firefox 下运行时: $.parseJson(xxx) 返回 null,所以导致了 无法正常运行,调查的结果是因为 返 ...

  3. Type.GetType()反射另外项目中的类时返回null的解决方法

    项目1:ProjectA namespace ProjectA { public class paa { .... } } Type.GetType("paa")返回null Ty ...

  4. findViewById返回null

    Q:findViewById返回null? A: 代码逻辑错误: 最终,发现错误竟然是在layout文件中把android:id写成了android:name. android:name=" ...

  5. Xamarin +vs2015 Android 开发GPS loaction 返回 null 小结

    最近公司要开发android 所以研究了一下Xamarin  to android 中个GPS 废话不多说,说重点. 想获取手机上的gps信息必不可少的就是要使用 LocationManager Lo ...

  6. c#泛型方法返回null的问题

    c#的泛型方法实现和java实现有点不同,在java中,所有的泛型方法运行时类型必须是引用类型,所以和非泛型一样可以返回null. 但是c#中有点不同,可以同时是值类型和引用类型,而值类型不能赋值nu ...

  7. Flash的坑之ExternalInterface.call只返回null值的解决办法

    flash坑太多了,要确保能有效的使用ExternalInterface.call调用js的话,需要两个条件: 1.allowScriptAccess="always" 2.id= ...

  8. C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍

    一. SelectNodes,SelectSingleNode总是返回NULL 下面以一个简单的xml为例: <?xml version="1.0"?> <mes ...

  9. php怎么解析utf-8带BOM编码的json数据,php解析json数据返回NULL

    今天遇到一个问题,json_decode解析json数据返回null,试了各种方法都不行,最后发现,原来是json文件编码的问题. 当json_decode解析utf-8带BOM格式的json数据时, ...

随机推荐

  1. Python中并发、多线程等

    1.基本概念 并发和并行的区别: 1)并行,parallel 同时做某些事,可以互不干扰的同一时刻做几件事.(解决并发的一种方法) 高速公路多个车道,车辆都在跑.同一时刻. 2)并发 concurre ...

  2. Vite2+Electron仿抖音|vite2.x+electron12+vant3短视频|直播|聊天

    整合vite2+electron12跨平台仿抖音电脑版实战Vite2-ElectronDouYin. 基于vite2.0+electron12+vant3+swiper6+v3popup等技术跨端仿制 ...

  3. 全量同步Elasticsearch方案之Canal

    一.前言 Canal 是阿里的一款开源项目,纯 Java 开发.基于数据库增量日志解析,提供增量数据订阅&消费,目前主要支持了 MySQL(也支持 mariaDB). Canal 除了支持 b ...

  4. Android Studio 如何更新到新版本

    •How 打开 Android Studio 选择任务栏中的 File 点击 Setting: 来到如下界面: 依次点击  Appearance & Behavior -> System ...

  5. 强大的 Guava 工具类

    Java 开发的同学应该都使用或者听说过 Google 提供的 Guava 工具包.日常使用最多的肯定是集合相关的工具类,还有 Guava cache,除了这些之外 Guava 还提供了很多有用的功能 ...

  6. 【Prometheus学习笔记】主机监控 -node_exporter

    Exporter for machine metrics prometheus/node_exporter 安装 Prometheus sudo tar -zxvf prometheus-*.tar. ...

  7. 000 - 准备工作ADB wifi连接多台鸿蒙设备进行调试

    首先将两台鸿蒙设备插入电脑的usb上 查看两台鸿蒙设备的deviceid C:\Users\Administrator>adb devices * daemon not running; sta ...

  8. Spring Cloud Gateway 全局通用异常处理

    为什么需要全局异常处理 在传统 Spring Boot 应用中, 我们 @ControllerAdvice 来处理全局的异常,进行统一包装返回 // 摘至 spring cloud alibaba c ...

  9. day-5 xctf-when_did_you_born

    xctf-when_did_you_born 题目传送门:https://adworld.xctf.org.cn/task/answer?type=pwn&number=2&grade ...

  10. 《C++ primer》学习笔记整理

    简介 本笔记目前已包含<C++ Primer>中的绝大部分内容,但尚有部分小节有所缺漏,如 19.1.19.2 节的笔记尚未整理,会持续更新. 本项目中的学习笔记是在学完一章内容后,对其要 ...