我需要一种快速的Java方法来检查JPEG文件是否有效,或者它是否是一个被截断/损坏的图像。
我尝试过几种方法:
using the javax.ImageIO library
public boolean check(File image) throws IOException { try { BufferedImage bi = ImageIO.read(image); bi.flush(); } catch (IIOException e) { return false; } return true; }
but it can detect only few corrupted files of the ones I have tested and it's very slow (on my PC around 1 image / second).
Apache Commons Imaging library
public boolean check(File image) throws IOException { JpegImageParser parser = new JpegImageParser(); ByteSourceFile bs = new ByteSourceFile(image); try { BufferedImage bi = parser.getBufferedImage(bs, null); bi.flush(); return true; } catch (ImageReadException e) { return false; } }
This code can detect all the corrupted images I've tested, but the performances are very poor (on my PC less than 1 image / second).
我正在寻找一个 Java 替代 UNIX 程序jpeginfo,其速度大约快了10倍(在我的电脑上大约每秒可处理10张图片)。