| orientation | 描述 |
|---|---|
| 3 | iphone橫屏拍攝,此時(shí)home鍵在左側(cè),圖片相對(duì)于原始位置旋轉(zhuǎn)了180度 |
| 6 | iphone豎屏拍攝,此時(shí)home鍵在下方(正常拿手機(jī)的方向),圖片相對(duì)于原始位置逆時(shí)針旋轉(zhuǎn)可90度 |
| 8 | iphone豎屏拍攝,此時(shí)home鍵在上方,圖片相對(duì)于原始位置順時(shí)針旋轉(zhuǎn)了90度 |
switch (orientation) {
case 6:
case 8:
cvs.width = height;
cvs.height = width;
break;
}
var context=cvs.getContext("2d");
switch(orientation){
//iphone橫屏拍攝,此時(shí)home鍵在左側(cè)
case 3:
// 180度向左旋轉(zhuǎn)
context.translate(width, height);
context.rotate(Math.PI);
break;
//iphone豎屏拍攝,此時(shí)home鍵在下方(正常拿手機(jī)的方向)
case 6:
context.rotate(0.5 * Math.PI);
context.translate(0, -height);
break;
//iphone豎屏拍攝,此時(shí)home鍵在上方
case 8:
// 逆時(shí)針旋轉(zhuǎn)90度
context.rotate(-0.5 * Math.PI);
context.translate(-width, 0);
break;
}
然后再上傳圖片,發(fā)現(xiàn)在IOS下圖片已經(jīng)正常了。
下面給出完整代碼:
$('input[type=file]').change(function(e) {
var file = this.files[0];
var mime_type=file.type;
var orientation=0;
if (file && /^image\//i.test(file.type)) {
EXIF.getData(file,function(){
orientation=EXIF.getTag(file,'Orientation');
});
var reader = new FileReader();
reader.onloadend = function () {
var width,height;
var MAX_WH=800;
var image=new Image();
image.onload=function () {
if(image.height > MAX_WH) {
// 寬度等比例縮放 *=
image.width *= MAX_WH / image.height;
image.height = MAX_WH;
}
if(image.width > MAX_WH) {
// 寬度等比例縮放 *=
image.height *= MAX_WH / image.width;
image.width = MAX_WH;
}
//壓縮
var quality=80;
var cvs = document.createElement('canvas');
cvs.width = width = image.width;
cvs.height =height = image.height;
switch (orientation) {
case 6:
case 8:
cvs.width = height;
cvs.height = width;
break;
}
var context=cvs.getContext("2d");
//解決ios圖片旋轉(zhuǎn)問(wèn)題
switch(orientation){
//iphone橫屏拍攝,此時(shí)home鍵在左側(cè)
case 3:
// 180度向左旋轉(zhuǎn)
context.translate(width, height);
context.rotate(Math.PI);
break;
//iphone豎屏拍攝,此時(shí)home鍵在下方(正常拿手機(jī)的方向)
case 6:
context.rotate(0.5 * Math.PI);
context.translate(0, -height);
break;
//iphone豎屏拍攝,此時(shí)home鍵在上方
case 8:
// 逆時(shí)針旋轉(zhuǎn)90度
context.rotate(-0.5 * Math.PI);
context.translate(-width, 0);
break;
}
context.drawImage(image, 0, 0,image.width, image.height);
dataURL = cvs.toDataURL('image/jpeg', quality/100);
//獲取識(shí)別結(jié)果
...
}
image.src=dataURL;
};
reader.readAsDataURL(file);
}else{
alert("只能識(shí)別圖片!")
}
});
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:玉溪 安慶 邯鄲 內(nèi)蒙古 撫州 牡丹江 煙臺(tái) 晉中
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《H5調(diào)用相機(jī)拍照并壓縮圖片的實(shí)例代碼》,本文關(guān)鍵詞 調(diào)用,相機(jī),拍照,并,壓縮,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。