Skip to main content

Read embedded thumbnails in JPEG images

4 replies [Last post]
buzz2000
Offline
Joined: 2007-07-20

I'm trying to write some code to read the embedded thumbnails present in
JPEG images (from digital cameras for example).

This is the code:

---------------------------------------------------
ImageInputStream iis = ImageIO.createImageInputStream(new
FileInputStream(filename));

Iterator readers = ImageIO.getImageReadersByFormatName("jpg");
ImageReader reader = null;
while (readers.hasNext()) {
reader = (ImageReader)readers.next();
if (reader.readerSupportsThumbnails()) {
reader.setInput(iis, true);
System.out.println("NumThumbnails:" + reader.getNumThumbnails(0));
}

}
----------------------------------------------

It always prints "0", so it seems the library can't find any Thumbnail
in the JPEG file, although I'm sure it is there.

Here are the questions:
* May be the embedded thumbnail is in the EXIF information, so ImageIO
does not support it?
* Is there another ImageReader available that fully supports reading
thumbnails?
* Any hint on how to do this?

Thanks in advance for your help.

yan

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
igorsecansky
Offline
Joined: 2009-11-27

getThumbnails method usually failed for me...
I tried to create thumbnails using resizing in JAI. It works good and with different types (jpg, png, ...) but is not as quick as extracting embedded exif thumbnail that is included in the jpeg. http://commons.apache.org/sanselan/ is able to do this:

IImageMetadata metadata = Sanselan.getMetadata(sourceFile);
if (metadata instanceof JpegImageMetadata) {
JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
byte[] data = null;
ArrayList dirs = jpegMetadata.getExif().getDirectories();
for (int i = 0; i < dirs.size(); i++) {
TiffImageMetadata.Directory tiffdir = (TiffImageMetadata.Directory) dirs.get(i);
data = null;
if( tiffdir.getJpegImageData() != null ){
data = tiffdir.getJpegImageData().data;
if( data != null ){
// now data contains the exif thumbnail image which is embedded in the original jpg
// FileHandler.buildImageFromByteArray(data, thumbnail_filename);
// return true;
}
}
}
}

....

bpb
Offline
Joined: 2004-06-23

I should have remarked that the JAI Image I/O Tools JPEG reader supports via the thumbnail method calls all thumbnails embedded in the JFIF APP0, JFXX APP0, and EXIF APP1 marker segments. Please see this javadoc for more information:

http://download.java.net/media/jai-imageio/javadoc/1.1/overview-summary....

I think that the only thumbnails supported by the Java SE Image I/O JPEG reader via the thumbnail method calls are those in the JFIF and JFXX marker segments. If you are unable to use JAI Image I/O Tools for some reason you could however derive the EXIF thumbnail by parsing the contents of the "unknown" node in the image metadata corresponding to the EXIF APP1 marker segment.

Brian Burkhalter

Please provide some more details.

Are you using the JPEG ImageReader provided by the Java SE Image I/O Framework
or the one provided by JAI Image I/O Tools
(https://jai-imageio-code.dev.java.net)? If the latter package is not
installed then it will obviously be the former plugin. If they are both
installed you can verify the class name by printing
reader.getClass().getName(). If that starts with "com.sun.media" then it is
from JAI Image I/O Tools, otherwise not. (By the way, using the class name is
not a great way to generate a programmatic switch.)

On Fri, 20 Jul 2007, jai-imageio@javadesktop.org wrote:

> I'm trying to write some code to read the embedded thumbnails present in
> JPEG images (from digital cameras for example).
>
> This is the code:
>
> ---------------------------------------------------
> ImageInputStream iis = ImageIO.createImageInputStream(new
> FileInputStream(filename));
>
> Iterator readers = ImageIO.getImageReadersByFormatName("jpg");
> ImageReader reader = null;
> while (readers.hasNext()) {
> reader = (ImageReader)readers.next();
> if (reader.readerSupportsThumbnails()) {
> reader.setInput(iis, true);
> System.out.println("NumThumbnails:" + reader.getNumThumbnails(0));
> }
>
> }
> ----------------------------------------------
>
> It always prints "0", so it seems the library can't find any Thumbnail
> in the JPEG file, although I'm sure it is there.
>
>
> Here are the questions:
> * May be the embedded thumbnail is in the EXIF information, so ImageIO
> does not support it?
> * Is there another ImageReader available that fully supports reading
> thumbnails?
> * Any hint on how to do this?
>
>
> Thanks in advance for your help.
>
> yan
> [Message sent by forum member 'buzz2000' (buzz2000)]
>
> http://forums.java.net/jive/thread.jspa?messageID=227431
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

buzz2000
Offline
Joined: 2007-07-20

You were right, i'm not using the right reader :

com.sun.imageio.plugins.jpeg.JPEGImageReader

How can i use the good one properly ?