在項目中饶套,這里用到了input type="file"方式上傳圖片
<input @change="upImg(6)" ref="foreheadCir" type="file" accept="image/*" mutiple="mutiple">
在項目測試中發(fā)現(xiàn)掌实,使用蘋果手機點擊input標(biāo)簽直接調(diào)用相機功能上傳照片(從相冊選擇圖片上傳則正常)硕并,卻發(fā)現(xiàn)后臺接收到的圖片被翻轉(zhuǎn)了90度,而在安卓機測試卻不會右莱,最終解決的方法是:使用npm庫的exif-js插件桃笙。
解決步驟如下:
step1安裝插件:
cnpm install exif-js --save
step2在當(dāng)前組件引用插件:
import Vue from "vue";
import EXIF from "exif-js";
Vue.use(EXIF); //全局申明
step3 獲取change事件傳遞過來的圖片(例如:this.$refs.foreheadCir.files[0])
var Orientation = null;
if (file) {
if (navigator.userAgent.match(/Android/i)) {
console.log("正在上傳,請稍后...");
var rFilter = /^(image\/jpeg|image\/png)$/i; // 檢查圖片格式
if (!rFilter.test(file.type)) {
//showMyTips("請選擇jpeg氏堤、png格式的圖片", false);
return;
}
console.log("2222"+Orientation);
if (/^image/.test(file.type)) {
// 創(chuàng)建一個reader
var reader = new FileReader();
// 將圖片將轉(zhuǎn)成 base64 格式
reader.readAsDataURL(file);
// 讀取成功后的回調(diào)
reader.onloadend = function (e) {
/*var obj={};
obj["src"]=this.result;
self.upPhoto(index,obj["src"],areaId);//傳給后臺*/
var base64S = null;
var img = new Image,
width = 2048, //image resize
quality = 0.8, //image quality
canvas = document.createElement("canvas"),
drawer = canvas.getContext("2d");
img.src = this.result;
img.onload = function() {
canvas.width = width;
canvas.height = width * (img.height / img.width);
drawer.drawImage(img, 0, 0, canvas.width, canvas.height);
base64S = canvas.toDataURL("image/jpeg", quality);
console.log(img.src);
self.upPhoto(index,base64S,areaId);
}
}
}
}else{
console.log("正在上傳,請稍后...");
var rFilter = /^(image\/jpeg|image\/png)$/i; // 檢查圖片格式
if (!rFilter.test(file.type)) {
//showMyTips("請選擇jpeg、png格式的圖片", false);
return;
}
// var URL = URL || webkitURL;
//獲取照片方向角屬性搏明,用戶旋轉(zhuǎn)控制
EXIF.getData(file, function() {
// alert(EXIF.pretty(this));
EXIF.getAllTags(this);
//alert(EXIF.getTag(this, 'Orientation'));
Orientation = EXIF.getTag(this, 'Orientation');
//return;
});
var oReader = new FileReader();
oReader.onload = function(e) {
//var blob = URL.createObjectURL(file);
//_compress(blob, file, basePath);
var image = new Image();
image.src = e.target.result;
image.onload = function() {
var expectWidth = this.naturalWidth;
var expectHeight = this.naturalHeight;
/* if (this.naturalWidth > this.naturalHeight && this.naturalWidth > 800) {
expectWidth = 800;
expectHeight = expectWidth * this.naturalHeight / this.naturalWidth;
} else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) {
expectHeight = 1200;
expectWidth = expectHeight * this.naturalWidth / this.naturalHeight;
}*/
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = expectWidth;
canvas.height = expectHeight;
ctx.drawImage(this, 0, 0, expectWidth, expectHeight);
var base64 = null;
//修復(fù)ios
if (navigator.userAgent.match(/iphone/i)) {
console.log('iphone');
// alert("蘋果");
//alert(expectWidth + ',' + expectHeight);
//如果方向角不為1鼠锈,都需要進行旋轉(zhuǎn) added by lzk
if(Orientation != "" && Orientation != 1){
// alert('旋轉(zhuǎn)處理');
switch(Orientation){
case 6://需要順時針(向左)90度旋轉(zhuǎn)
// alert('需要順時針(向左)90度旋轉(zhuǎn)');
self.rotateImg(this,'left',canvas);
break;
case 8://需要逆時針(向右)90度旋轉(zhuǎn)
// alert('需要順時針(向右)90度旋轉(zhuǎn)');
self.rotateImg(this,'right',canvas);
break;
case 3://需要180度旋轉(zhuǎn)
// alert('需要180度旋轉(zhuǎn)');
/*self.rotateImg(this,'right',canvas);//轉(zhuǎn)兩次
self.rotateImg(this,'right',canvas);*/
self.rotateImg(this,'turntwo',canvas);
break;
}
}
/*var mpImg = new MegaPixImage(image);
mpImg.render(canvas, {
maxWidth: 800,
maxHeight: 1200,
quality: 0.8,
orientation: 8
});*/
base64 = canvas.toDataURL("image/jpeg", 0.8);
}/*else if (navigator.userAgent.match(/Android/i)) {// 修復(fù)android
// var encoder = new JPEGEncoder();
// base64 = encoder.encode(ctx.getImageData(0, 0, expectWidth, expectHeight), 80);
alert("安卓");
base64 = ctx.getImageData(0, 0, expectWidth, expectHeight);
}*/else{
//alert(Orientation);
// alert("其它");
if(Orientation != "" && Orientation != 1){
//alert('旋轉(zhuǎn)處理');
switch(Orientation){
case 6://需要順時針(向左)90度旋轉(zhuǎn)
// alert('需要順時針(向左)90度旋轉(zhuǎn)');
self.rotateImg(this,'left',canvas);
break;
case 8://需要逆時針(向右)90度旋轉(zhuǎn)
// alert('需要順時針(向右)90度旋轉(zhuǎn)');
self.rotateImg(this,'right',canvas);
break;
case 3://需要180度旋轉(zhuǎn)
// alert('需要180度旋轉(zhuǎn)');
/*self.rotateImg(this,'right',canvas);//轉(zhuǎn)兩次
self.rotateImg(this,'right',canvas);*/
self.rotateImg(this,'turntwo',canvas);
break;
}
}
base64 = canvas.toDataURL("image/jpeg", 0.8);
}
self.upPhoto(index,base64,areaId);//傳給后臺
// $("#myImage").attr("src", base64);
};
};
oReader.readAsDataURL(file);
}
}
step4通過畫布,對圖片進行旋轉(zhuǎn)處理:
rotateImg:function(img, direction,canvas) {
console.log("拉拉");
//alert(img);
//最小與最大旋轉(zhuǎn)方向星著,圖片旋轉(zhuǎn)4次后回到原方向
var min_step = 0;
var max_step = 3;
//var img = document.getElementById(pid);
if (img == null)return;
//img的高度和寬度不能在img元素隱藏后獲取购笆,否則會出錯
var height = img.height;
var width = img.width;
//var step = img.getAttribute('step');
var step = 2;
if (step == null) {
step = min_step;
}
if (direction == 'right') {
step++;
//旋轉(zhuǎn)到原位置,即超過最大值
step > max_step && (step = min_step);
} else {
step--;
step < min_step && (step = max_step);
}
//img.setAttribute('step', step);
/*var canvas = document.getElementById('pic_' + pid);
if (canvas == null) {
img.style.display = 'none';
canvas = document.createElement('canvas');
canvas.setAttribute('id', 'pic_' + pid);
img.parentNode.appendChild(canvas);
} */
//旋轉(zhuǎn)角度以弧度值為參數(shù)
var degree = step * 90 * Math.PI / 180;
var ctx = canvas.getContext('2d');
switch (step) {
case 0:
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0);
break;
case 1:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, 0, -height);
break;
case 2:
canvas.width = width;
canvas.height = height;
ctx.rotate(degree);
ctx.drawImage(img, -width, -height);
break;
case 3:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, -width, 0);
break;
}
},
stpe5最后將生成的圖片轉(zhuǎn)換為base64格式虚循,通過ajax請求發(fā)送給后端同欠。