如何从Java中的Thumbs.db中提取图像?

问题描述

| 喜 我阅读了有关POI项目的内容,并尝试从thumbs.db中提取图像,但是在代码中出现异常..代码os
InputStream stream = new FileInputStream(\"C:\\\\Thumbs.db\");
POIFSFileSystem fs = new POIFSFileSystem(stream);
DirectoryEntry root = fs.getRoot();
Entry entry = root.getEntry(\"2\");
DocumentInputStream is = fs.createDocumentInputStream(entry.getName());
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(is);
JPEGDecodeParam param = JPEGCodec.getDefaultJPEGEncodeParam(4,JPEGDecodeParam.COLOR_ID_RGBA);
decoder.setJPEGDecodeParam(param);
BufferedImage originalBufferedImage = decoder.decodeAsBufferedImage();
以\“ com.sun.image.codec.jpeg.ImageFormatException获取异常:不是JPEG文件:从0x0c 0x00 \开始” 以上情况有什么问题? 您能建议其他方法来完成上述任务吗?     

解决方法

您需要先读取Thumbs.db文件的标题,然后才能开始提取图像。尝试使用我在下面添加的更改,它应该删除您得到的ѭ1。
InputStream stream = new FileInputStream(\"C:\\\\Thumbs.db\");
POIFSFileSystem fs = new POIFSFileSystem(stream);
DirectoryEntry root = fs.getRoot();
Entry entry = root.getEntry(\"2\");
DocumentInputStream is = fs.createDocumentInputStream(entry.getName());

//Added to read the header lines and fix the ImageFormatException
int header_len = is.read();
for (int i = 1; i < header_len; i++) {
        is.read();
}

JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(is);
JPEGDecodeParam param = JPEGCodec.getDefaultJPEGEncodeParam(4,JPEGDecodeParam.COLOR_ID_RGBA);
decoder.setJPEGDecodeParam(param);
BufferedImage originalBufferedImage = decoder.decodeAsBufferedImage();
希望对您有所帮助!     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...