JS Client-Side Exif Orientation: Rotate and Mirror JPEG Images

前端 未结 12 1102
花落未央
花落未央 2020-11-22 11:59

Digital camera photos are often saved as JPEG with an EXIF \"orientation\" tag. To display correctly, images need to be rotated/mirrored depending on which orientation is se

12条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 12:24

    If

    width = img.width;
    height = img.height;
    var ctx = canvas.getContext('2d');
    

    Then you can use these transformations to turn the image to orientation 1

    From orientation:

    1. ctx.transform(1, 0, 0, 1, 0, 0);
    2. ctx.transform(-1, 0, 0, 1, width, 0);
    3. ctx.transform(-1, 0, 0, -1, width, height);
    4. ctx.transform(1, 0, 0, -1, 0, height);
    5. ctx.transform(0, 1, 1, 0, 0, 0);
    6. ctx.transform(0, 1, -1, 0, height, 0);
    7. ctx.transform(0, -1, -1, 0, height, width);
    8. ctx.transform(0, -1, 1, 0, 0, width);

    Before drawing the image on ctx

提交回复
热议问题