dragon66 / icafe

Java library for reading, writing, converting and manipulating images and metadata
Eclipse Public License 1.0
203 stars 58 forks source link

Return Thumbnails as byte array? #71

Closed ivanooi closed 6 years ago

ivanooi commented 6 years ago

Hi,

Is there anyway to return Thumbnails as byte array?

Thanks

dragon66 commented 6 years ago

ICAFE has a special data type called Thumbnail to handle different kinds of thumbnails. Internally, it is either in Tiff/Jpeg or BufferedImage which we call it raw format. I am not sure what exactly you mean by saying return thumbnail as byte array. The current implementation detail is: if it is in Tiff or Jpeg format, you can grab it as a byte array in that format otherwise you can grab it as BufferedImage. Please let me know more details about your use case and requirements.

Wen

ivanooi commented 6 years ago

This is the sample code. directly convert byte array into image and display it into SWT table row.

           inputStreamReader = new BufferedInputStream( new ByteArrayInputStream( lbyt_array ));

           image = new org.eclipse.swt.graphics.Image( Display, new ImageData( inputStreamReader ));  

           itblitm_row.setImage( 1, image ); //Photo

Thanks

dragon66 commented 6 years ago

Try this - first assume you have already got a Thumbnail instance called thumbnail: // To get a Thumbnail, you can either create one or extract it from a Metada.readMetadata. I assume you // got it from the later.

int dataType thumbnail.getDataType();
if(dataType == Thumbnail.DATA_TYPE_KJpegRGB || dataType == Thumbnail.DATA_TYPE_TIFF) {
    lbyt_array = thumbnail.getCompressedImage();
    inputStreamReader = new BufferedInputStream( new ByteArrayInputStream( lbyt_array ));
    image = new org.eclipse.swt.graphics.Image( Display, new ImageData( inputStreamReader ));  
    itblitm_row.setImage( 1, image ); //Photo
}

This will work almost all of the time (unless the thumbnail is in some raw data type). The compressed data byte array will be either in Jpeg or Tiff format.

Let me know if it works or if you have question about how to extract Thumbnail from reading metadata.

Wen

ivanooi commented 6 years ago

Hi,

I found this Metadata.extractThumbnails

but it return nothing.

I can't find this statement Thumbnail.getCompressedImage();

Thanks again

ivanooi commented 6 years ago

I believe should be like this :

     metadataMap.get(MetadataType.IMAGE );
     Exif exif = (JpegExif)metadataMap.get(MetadataType.EXIF );
     ExifThumbnail thumbnail = exif .getThumbnail();
     thumbnail.getCompressedImage();

It work! Thanks again! :-D

dragon66 commented 6 years ago

You are right but that's one specific way of get Exif thumbnail. A generic way to get Thumbnail which is the super class of all kinds of thumbnails is like this:

Map<MetadataType, Metadata> metadataMap = Metadata.readMetadata(InputStream is);
if(metadataMap.get(MetadataType.IMAGE) != null) {
    ImageMetadata im = (ImageMetadata)metadataMap.get(MetadataType.IMAGE);
    if(im.containsThumbnail()) {
        Map<String, Thumbnail> thumbnailMap = im.getThumbnails(); // Usually one image only has one thumbnail
        // Get the first entry that the iterator returns
        Map.Entry<String, Thumbnail> entry = thumbnailMap.entrySet().iterator().next(); // Need to do null check
        Thumbnail thumbnail = entry.getValue();
    }
}

Metadata.readMetadata() used to extract thumbnails from JPEG only, I checked in some code early today so it will extract thumbnail from TIFF too.

After you grab the Thumbnail, you can use the code I provided in the previous response to show it by Eclipse SWT.

ivanooi commented 6 years ago

ya, you are right! the above code better! Thanks again