简体   繁体   English

在JavaScript中确定照片的方向?

[英]Determine orientation of photos in JavaScript?

We're using FileReader to get an image, from a photo taken on an iPhone, into the browser. 我们正在使用FileReader从iPhone上拍摄的照片中获取图像到浏览器中。 We then use drawImage() to draw that image onto a canvas . 然后我们使用drawImage()将该图像绘制到canvas The problem is that photos taken on an iPhone display rotated on the page. 问题是在iPhone显示屏上拍摄的照片在页面上旋转。 We can't reproduce this on any Android devices. 我们无法在任何Android设备上重现这一点。

We can rotate the image on the canvas easily enough but how can we determine the required rotation? 我们可以很容易地在canvas上旋转图像,但我们如何确定所需的旋转? We tried some EXIF-reading libraries for JavaScript ( exif.js ) but were unable to successfully read the orientation. 我们尝试了一些用于JavaScript的EXIF读取库( exif.js )但无法成功读取方向。

Ok, so it looks like you in fact can read the exif data using exif.js. 好的,所以看起来你实际上可以使用exif.js读取exif数据。

$("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 . 此代码使用exif.jsbinaryajax.js

This works but only if you try it out with a photo taken on ios. 这可以工作,但只有你尝试用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. 我认为android只是旋转实际图像,方向总是1,所以他们甚至没有写出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): 对于具有方向的图像,该值是可读的,可以解释如下(那些是F的顺便说一句):

  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

Works great even without binaryajax.js 即使没有binaryajax.js也能很好地工作

Just use 只是用

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

Also here you could see another examples. 在这里你可以看到另一个例子。

Or if you just want the Orientation tag: 或者,如果您只想要Orientation标记:

EXIF.getData(file, function () {
    alert(this.exifdata.Orientation);
});

my two cents is that the exif-js framework does not work for me and is not required. 我的两分钱是exif-js框架对我不起作用而且不是必需的。

instead what you can do is 'onload' of your image src, create a canvas, then draw your canvas overtop of your image, what this accomplishes is getting rid of any iphone related 'exif' rotation of the image, because the canvas drawImage erases exif data. 相反,你可以做的是你的图像src的'onload',创建一个画布,然后在你的图像上方绘制你的画布,这实现了摆脱任何iphone相关的'exif'旋转的图像,因为画布drawImage擦除exif数据。

when the draw is complete, you will see your image 'rotate' back to how it would look on a PC browser 绘制完成后,您将看到图像“旋转”回到PC浏览器上的外观

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM