我使用JavaScript LoadImage。parseMetaData (https://github.com/blueimp/JavaScript-Load-Image)来尝试获取web上图像的方向,这样我就可以旋转它。
如果我硬编码的方向(见"定向:3"在我的第二个loadImage调用),我可以旋转它…但我正在尝试使用loadImage。parseMetaData来获取朝向。
我使用了基于web的EXIF解析器,方向信息在图像中。
当我调用loadImage。parseMetaData"数据。Exif"似乎为空。查看此页面:http://jsfiddle.net/aginsburg/GgrTM/13/
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://www.filepicker.io/api/file/U0D9Nb9gThy0fFbkrLJP', true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
if (this.status == 200) {
// Note: .response instead of .responseText
console.log ("got image");
var blob = new Blob([this.response], {type: 'image/png'});
console.log("about to parse blob:" + _.pairs(this.response));
loadImage.parseMetaData(blob, function (data) {
console.log("EXIF:" + _.pairs(data))
var ori ="initial";
if (data.exif) {
ori = data.exif.get('Orientation');
}
console.log("ori is:" + ori);
});
var loadingImage = loadImage(
blob,
function (img) {
console.log("in loadingImage");
document.body.appendChild(img);
},
{maxWidth: 600,
orientation: 3,
canvas: true,
crossOrigin:'anonymous'
}
);
if (!loadingImage) {
// Alternative code ...
}
}
};
xhr.send();
对loadImage的调用需要在调用parseMetaData的回调中。
原因是:你的代码包含了一个竞争条件。loadImage的调用很可能在调用parseMetaData完成并填充方向之前进行,因为它们都是异步调用。
为什么你要做一个新的blob,而你要求一个blob ?元数据随后丢失,这就是丢失元数据且exif为空的原因。只需替换:
var blob = new Blob([this.response], {type: 'image/png'});
由:var blob = this.response;
遇到同样的问题,我更改了'arrayBuffer'的响应类型,然后从响应
创建blobxhr.responseType = 'arraybuffer';
xhr.onload = function (e) {
if (this.status == 200) {
var arrayBufferView = new Uint8Array(this.response);
var blob = new Blob([arrayBufferView], { type: "image/jpeg" });
...