Accessing JPEG EXIF rotation data in JavaScript on the client side

前端 未结 8 1457
臣服心动
臣服心动 2020-11-22 13:53

I\'d like to rotate photos based on their original rotation, as set by the camera in JPEG EXIF image data. The trick is that all this should happen in the browser, using Jav

8条回答
  •  心在旅途
    2020-11-22 14:22

    I upload expansion code to show photo by android camera on html as normal on some img tag with right rotaion, especially for img tag whose width is wider than height. I know this code is ugly but you don't need to install any other packages. (I used above code to obtain exif rotation value, Thank you.)

    function getOrientation(file, callback) {
      var reader = new FileReader();
      reader.onload = function(e) {
    
        var view = new DataView(e.target.result);
        if (view.getUint16(0, false) != 0xFFD8) return callback(-2);
        var length = view.byteLength, offset = 2;
        while (offset < length) {
          var marker = view.getUint16(offset, false);
          offset += 2;
          if (marker == 0xFFE1) {
            if (view.getUint32(offset += 2, false) != 0x45786966) return callback(-1);
            var little = view.getUint16(offset += 6, false) == 0x4949;
            offset += view.getUint32(offset + 4, little);
            var tags = view.getUint16(offset, little);
            offset += 2;
            for (var i = 0; i < tags; i++)
              if (view.getUint16(offset + (i * 12), little) == 0x0112)
                return callback(view.getUint16(offset + (i * 12) + 8, little));
          }
          else if ((marker & 0xFF00) != 0xFF00) break;
          else offset += view.getUint16(offset, false);
        }
        return callback(-1);
      };
      reader.readAsArrayBuffer(file);
    }
    
    var isChanged = false;
    function rotate(elem, orientation) {
        if (isIPhone()) return;
    
        var degree = 0;
        switch (orientation) {
            case 1:
                degree = 0;
                break;
            case 2:
                degree = 0;
                break;
            case 3:
                degree = 180;
                break;
            case 4:
                degree = 180;
                break;
            case 5:
                degree = 90;
                break;
            case 6:
                degree = 90;
                break;
            case 7:
                degree = 270;
                break;
            case 8:
                degree = 270;
                break;
        }
        $(elem).css('transform', 'rotate('+ degree +'deg)')
        if(degree == 90 || degree == 270) {
            if (!isChanged) {
                changeWidthAndHeight(elem)
                isChanged = true
            }
        } else if ($(elem).css('height') > $(elem).css('width')) {
            if (!isChanged) {
                changeWidthAndHeightWithOutMargin(elem)
                isChanged = true
            } else if(degree == 180 || degree == 0) {
                changeWidthAndHeightWithOutMargin(elem)
                if (!isChanged)
                    isChanged = true
                else
                    isChanged = false
            }
        }
    }
    
    
    function changeWidthAndHeight(elem){
        var e = $(elem)
        var width = e.css('width')
        var height = e.css('height')
        e.css('width', height)
        e.css('height', width)
        e.css('margin-top', ((getPxInt(height) - getPxInt(width))/2).toString() + 'px')
        e.css('margin-left', ((getPxInt(width) - getPxInt(height))/2).toString() + 'px')
    }
    
    function changeWidthAndHeightWithOutMargin(elem){
        var e = $(elem)
        var width = e.css('width')
        var height = e.css('height')
        e.css('width', height)
        e.css('height', width)
        e.css('margin-top', '0')
        e.css('margin-left', '0')
    }
    
    function getPxInt(pxValue) {
        return parseInt(pxValue.trim("px"))
    }
    
    function isIPhone(){
        return (
            (navigator.platform.indexOf("iPhone") != -1) ||
            (navigator.platform.indexOf("iPod") != -1)
        );
    }
    

    and then use such as

    $("#banner-img").change(function () {
        var reader = new FileReader();
        getOrientation(this.files[0], function(orientation) {
            rotate($('#banner-img-preview'), orientation, 1)
        });
    
        reader.onload = function (e) {
            $('#banner-img-preview').attr('src', e.target.result)
            $('#banner-img-preview').css('display', 'inherit')
    
        };
    
        // read the image file as a data URL.
        reader.readAsDataURL(this.files[0]);
    
    });
    

提交回复
热议问题