无法使用 loadImage.parseMetaData 获取 EXIF 信息

agi*_*urg 5 javascript exif loadimage

我正在使用 JavaScript LoadImage.parseMetaData ( https://github.com/blueimp/JavaScript-Load-Image ) 尝试获取网络上图像的方向,以便我可以旋转它。

如果我对方向进行硬编码(请参阅第二个 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();
Run Code Online (Sandbox Code Playgroud)

欢迎任何正确定位图像的想法或替代方法。

小智 1

为什么你在要求一个 Blob 的同时却创建了一个新的 Blob?然后元数据就会丢失,这就是你丢失它并且 exif 为空的原因。只需替换:

var blob = new Blob([this.response], {type: 'image/png'});
Run Code Online (Sandbox Code Playgroud)

经过:

var blob = this.response;
Run Code Online (Sandbox Code Playgroud)

应该做到这一点。