Loading...

Administrator
04-19 18:37
    preProcesser(/** @type {WechatMiniprogram.Canvas} */ canvas, /** @type { CanvasRenderingContext2D } */ ctx, img, /** @type {Number} */ index,) {
        switch (imgOrientation[index]) {
            case "up": {
                canvas.width = imgWidth[index];
                canvas.height = imgHeight[index];
                ctx.drawImage(img, 0, 0, imgWidth[index], imgHeight[index]);
                console.log(imgWidth[index], imgHeight[index]);
                break;
            }
            case "right": {
                // left和right方向需要交换width和height
                [imgWidth[index], imgHeight[index]] = [imgHeight[index], imgWidth[index]];
                canvas.width = imgWidth[index];
                canvas.height = imgHeight[index];
                ctx.translate(imgHeight[index] / 2, imgWidth[index] / 2)
                ctx.drawImage(img, -imgHeight[index] / 2, -imgWidth[index] / 2, imgWidth[index], imgHeight[index])
                ctx.rotate(90 * Math.PI / 180)
                break;
            }
            case "down": {
                canvas.width = imgWidth[index];
                canvas.height = imgHeight[index];
                ctx.translate(imgWidth[index] / 2, imgHeight[index] / 2)
                ctx.drawImage(img, -imgWidth[index] / 2, -imgHeight[index] / 2, imgWidth[index], imgHeight[index])
                ctx.rotate(180 * Math.PI / 180)
                break;
            }
            case "left": {
                // left和right方向需要交换width和height
                [imgWidth[index], imgHeight[index]] = [imgHeight[index], imgWidth[index]];
                canvas.width = imgWidth[index];
                canvas.height = imgHeight[index];
                ctx.translate(imgHeight[index] / 2, imgWidth[index] / 2)
                ctx.drawImage(img, -imgHeight[index] / 2, -imgWidth[index] / 2, imgWidth[index], imgHeight[index])
                ctx.rotate(270 * Math.PI / 180)
                break;
            }
        }
    },

小程序根据图片的orientation信息纠正图片方向
引用链接:ios手机竖屏拍照图片旋转90°问题解决方法

0