解决某些手机系统正方向竖着拍方向错乱的问题。
npm install exif-js --save
import EXIF from "exif-js";
let reader = new FileReader();
reader.readAsDataURL(file);
if (file) {
EXIF.getData(file, function() {
let ori = EXIF.getTag(this, "Orientation");
// alert(ori, "手持方向");
reader.onload = ev => {
let image = new Image();
image.src = ev.target.result;
image.onload = () => {
let canvas = document.createElement("canvas"),
ctx = canvas.getContext("2d");
if (ori == 6 && image.width > image.height) {
//ios正方向竖着排方向错乱时,旋转90度
canvas.width = image.height;
canvas.height = image.width;
ctx.rotate(Math.PI / 2);
ctx.drawImage(image, 0, -image.height, image.width, image.height);
} else {
canvas.width = image.width / 2;
canvas.height = image.height / 2;
ctx.drawImage(image, 0, 0, image.width / 2, image.height / 2);
}
resolve(canvas.toDataURL("image/jpeg"));
};
};
});
}