我正在使用JavaScript LoadImage.parseMetaData (https://github.com/blueimp/JavaScript-Load-Image)来尝试获取web上图像的方向,这样我就可以旋转它。
如果我硬编码方向(参见第二个loadImage调用中的" orientation : 3“),我可以旋转它……但我正在尝试使用loadImage.parseMetaData来获取方向。
我使用了基于web的EXIF解析器,方向信息在图像中。
当我调用loadImage.parseMetaData时,"data.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();欢迎任何正确定位图像的想法或替代方法。
发布于 2015-09-11 04:49:06
您对loadImage的调用需要位于对parseMetaData的调用的回调中。
原因是:您的代码包含竞态条件。由于它们都是异步调用,因此很可能在parseMetaData完成调用之前进行loadImage调用,并填充方向。
发布于 2014-08-05 03:03:26
你为什么要做一个新的斑点,而你要求的是一个斑点?然后元数据就会丢失,这就是为什么你丢失了它,exif为null。只需替换:
var blob = new Blob([this.response], {type: 'image/png'});出自:
var blob = this.response;应该能行得通。
发布于 2015-08-21 23:53:27
遇到同样的问题,我更改了'arrayBuffer‘的响应类型,然后根据响应创建blob
xhr.responseType = 'arraybuffer';
xhr.onload = function (e) {
if (this.status == 200) {
var arrayBufferView = new Uint8Array(this.response);
var blob = new Blob([arrayBufferView], { type: "image/jpeg" });
...https://stackoverflow.com/questions/24982771
复制相似问题