ImageIO.read -> Error reading PNG image data
问题:就是类似这样的的一张png图片,我想用java的imageIO去读取,然而运行的时候却报异常了?
javax.imageio.IIOException: Error reading PNG image data at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1271)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1478)
at javax.imageio.ImageIO.read(ImageIO.java:1422)
at javax.imageio.ImageIO.read(ImageIO.java:1326)
at imgIOTest.Test23.main(Test23.java:27)
Caused by: java.util.zip.ZipException: incorrect data check
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:147)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at java.io.DataInputStream.readFully(DataInputStream.java:178)
at com.sun.imageio.plugins.png.PNGImageReader.decodePass(PNGImageReader.java:1068)
at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(PNGImageReader.java:1172)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1264)
... 4 more
从异常情况上来看,图片本身可能被做过某些处理以致图片数据不完整了,然后imageio去读取的时候解析报错了,而imageio是javax的一部分,如果不采取这种方法去读取图片还有没其他的方法可以做;
我想问问有没方法可以让这样图片正常的读取?
其实我要实现的功能很简单就系对原图进行裁切处理得到一张处理过后的图片,以下是我的代码(没有带裁切不部分,因为报错是以为文件解析读取的问题而导致的):
package imgIOTest;import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Test23 {
public static void main(String[] args) {
String filePath = "F:\\图片压缩问题\\wKgBEFQ7VZqAcVYsAAfIvKuNK1s5607 - xx.png";
byte[] bytes = getBytes(filePath);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
bytes);
try {
BufferedImage image = ImageIO.read(byteArrayInputStream);// 问题代码段
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获得指定文件的byte数组
*/
public static byte[] getBytes(String filePath) {
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
}
求各路大神指点迷津,小弟临表涕零感谢不尽,谢谢!
回答:
thumbnailator 试一下用这个组件 https://code.google.com/p/thumbnailator/
切图片和格式转换 都比较给力,比jdk得图片包好用
你的异常应该就是图片有损坏。
以上是 ImageIO.read -> Error reading PNG image data 的全部内容, 来源链接: utcz.com/p/176756.html