2011-05-25 64 views
5

HI 我讀到POI項目,並試圖從中提取的Thumbs.db但代碼變得異常圖像..代碼操作系統如何從Java中的Thumbs.db中提取圖像?

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「

上述情況有什麼問題? 你可以建議一些其他方式做上述任務嗎?

回答

9

您需要先閱讀Thumbs.db文件的標題,然後才能開始提取圖像。嘗試下面我添加的更改,它應該刪除您獲得的ImageFormatException

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(); 

我希望有幫助!