Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract images from Thumbs.db in Java?

HI I read about POI project and tried to extract images from thumbs.db but getting exception in code .. Code 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();

Getting exception as "com.sun.image.codec.jpeg.ImageFormatException: Not a JPEG file: starts with 0x0c 0x00"

What is problem with above case ? Can you suggest some other way to do above task ?

like image 708
Sachin Doiphode Avatar asked May 25 '11 19:05

Sachin Doiphode


1 Answers

You need to read the header of the Thumbs.db file before you can start extracting the images. Try with the changes I added below, it should remove the ImageFormatException you are getting.

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

I hope that helps!

like image 90
bamana Avatar answered Oct 22 '22 08:10

bamana