Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine orientation of photos in JavaScript?

We're using FileReader to get an image, from a photo taken on an iPhone, into the browser. We then use drawImage() to draw that image onto a canvas. The problem is that photos taken on an iPhone display rotated on the page. We can't reproduce this on any Android devices.

We can rotate the image on the canvas easily enough but how can we determine the required rotation? We tried some EXIF-reading libraries for JavaScript (exif.js) but were unable to successfully read the orientation.

like image 980
eagspoo Avatar asked Apr 25 '13 17:04

eagspoo


People also ask

How do I know the orientation of an image?

The length of the longest side determines the orientation. For example, if the height of the image is longer than the width, it is a “portrait” format. Images where the width is longer are called “landscape.”

How do you tell if an image is landscape or portrait Javascript?

You can simply compare width and height of the image. var someImg = $("#someId"); if (someImg. width() > someImg. height()){ //it's a landscape } else if (someImg.

What is Auto Photo Orientation?

DSLR cameras are equipped with a built-in sensor that detects camera orientation. Information from this sensor is embedded in photographs as they are taken, allowing portrait (tall) orientation photographs to be rotated automatically when displayed in Nikon View, PictureProject or Nikon Capture 4.


3 Answers

Ok, so it looks like you in fact can read the exif data using exif.js.

$("input").change(function() {
    var file = this.files[0];
    fr   = new FileReader;

    fr.onloadend = function() {
        var exif = EXIF.readFromBinaryFile(new BinaryFile(this.result));
        alert(exif.Orientation);
    };

    fr.readAsBinaryString(file);
});

This code is using exif.js and binaryajax.js.

This works but only if you try it out with a photo taken on ios. I think android just rotates the actual image and orientation is always 1 so they don't even write out the orientation to exif. Hence we were fooled into thinking it wasn't working.

For images that do have orientation, the value is readable and can be interpreted as below (those are F's btw):

  1        2       3      4         5            6           7          8

888888  888888      88  88      8888888888  88                  88  8888888888
88          88      88  88      88  88      88  88          88  88      88  88
8888      8888    8888  8888    88          8888888888  8888888888          88
88          88      88  88
88          88  888888  888888
like image 196
eagspoo Avatar answered Oct 06 '22 01:10

eagspoo


Works great even without binaryajax.js

Just use

EXIF.getData(changeEvent.target.files[0], function () {
    alert(EXIF.pretty(this));                            
});

Also here you could see another examples.

like image 22
aesir Avatar answered Oct 05 '22 23:10

aesir


Or if you just want the Orientation tag:

EXIF.getData(file, function () {
    alert(this.exifdata.Orientation);
});
like image 40
Felix Turner Avatar answered Oct 06 '22 01:10

Felix Turner