提取 exif data/rotate 图片添加到 fabric.js canvas

Extract exif data/rotate image added to fabric.js canvas

在我的网络应用程序中,用户可以通过输入类型的文件在手机上拍照。

之前我使用 js load image 库来自动检测 EXIF 数据并在必要时旋转它。

现在我已经切换到 fabric.js 库,因为我想让用户在上传之前在 canvas 图像上绘图。

问题是我不知道如何获取图像exif数据,或者在使用时旋转图像fabric.js。

我的代码:

从索引数据库中获取图像

var image;
localforage.getItem('photo').then(function(value) {
            image = value;

            // THIS IS OLD CODE, BEFORE I USER js-load-image to automatically detect exif data
            // and rotate if neccessary, then I just appended the result to wrapper
            // $("#preview-wrapper").empty();
            // if (image) {
            //    loadImage(
            //      image,
            //      function(img) {
            //        $("#preview-wrapper").append(img);
            //      },
            //      {orientation: true} // Options
            //    );
            // }

        })
 loadFabricOnImageLoad(); // append image to fabric canvas

Fabric.js 函数

function loadFabricOnImageLoad(){

    if(typeof image !== "undefined" && image){

        let loaded = false;
        if (loaded) {return;}
        var c = document.getElementById('myCanvas');
        canvas = new fabric.Canvas(c);
        canvas.setWidth(width);
        canvas.setHeight(500);
        canvas.selection = false;

        var url = URL.createObjectURL(image);

        fabric.Image.fromURL(url, function(img) {
            img.set({
                left: 0,
                right: 0,
                top: 0,
            });

            img.scaleToWidth(width);

            canvas.add(img);

            canvas.setHeight(img.getScaledHeight());

            $("#canvas-loader").hide();

        });  

        fabric.Object.prototype.set({
            evented: false
        });

        canvas.freeDrawingBrush = new fabric['PencilBrush'](canvas);
        canvas.freeDrawingBrush.color = 'Red';
        canvas.freeDrawingBrush.width = 6;

        loaded = true;
    } else {
        setTimeout(loadFabricOnImageLoad, 250);
    }
}

毕竟我已经设法通过从图像文件中获取 exif 数据来完成它:

localforage.getItem('photo').then(function(value) {
        image = value;

        // extract the exif data from image file using exif.js
        EXIF.getData(image, function() {
          let obj = EXIF.getAllTags(this);
          imageOrientation = obj.Orientation;
          console.log( imageOrientation );
        });

    })

我必须进行大量缩放和调整大小才能获得旋转图像的大小,并且 canvas 正确:

function loadFabricOnImageLoad(){

if(typeof image !== "undefined" && image){

    let loaded = false;
    if (loaded) {return;}
    var c = document.getElementById('myCanvas');
    canvas = new fabric.Canvas(c);
    canvas.setWidth(width);
    canvas.setHeight(500);
    canvas.selection = false;

    var url = URL.createObjectURL(image);

    fabric.Image.fromURL(url, function(img) {

        img.scaleToWidth(canvas.width);

        if(imageOrientation == 3 || imageOrientation == 6 || imageOrientation == 8) {
            img.scaleToHeight(canvas.height);

            canvas.setHeight(img.getScaledHeight());
            canvas.add(img);

            img.scaleToWidth(canvas.height);
        img.scaleToHeight(canvas.width);
        img.rotate(getDegreesForOrientation(imageOrientation));
            img.center();
        } else {
            canvas.setHeight(img.getScaledHeight());

            canvas.add(img);
        }


        $("#canvas-loader").hide();

    });
    // console.log('slika '+JSON.stringify(fabric.Image));


    fabric.Object.prototype.set({
        evented: false
    });

    canvas.freeDrawingBrush = new fabric['PencilBrush'](canvas);
    canvas.freeDrawingBrush.color = 'Red';
    canvas.freeDrawingBrush.width = 6;

    loaded = true;
} else {
    setTimeout(loadFabricOnImageLoad, 250);
}
}

我知道这已经得到解答,但我想推荐不同的库。 exif-js 已死 - 错误且不再维护,有数百个未解决的问题。

我希望你尝试 exifr 我开始开发它是出于对 exif-js 的失望。