Fixing orientation when resizing images on Android

On Android, EXIF orientation information is not taken into account when an image is resized or cropped using e.g. Ti.Blob.imageAsThumbnail(). Because of this bug, pictures appear rotated after resizing them.

There has been a ticket for this since June last year, but still no fix. Stefan Moonen soon decided he couldn’t wait no longer and created the fh.imagefactory module.

Here’s a simple CommonJS module to workaround the issue:

var ImageFactory = require('fh.imagefactory');

exports.imageAsThumbnail = function(size, quality) {

    if (Ti.Platform.name === 'android') {
        ImageFactory.rotateResizeImage(image.nativePath, size, quality || 100);
        return image;

    } else {
        return image.imageAsThumbnail(size);
    }
};

App imagineer: Imagining, Engineering & Speaking about Native mobile Apps with Appcelerator Titanium & Alloy • Meetup organizer • Certified Expert • Titan


Comments