背景
最近要做一個(gè)h5的頁面,主要功能是調(diào)用相機(jī)拍照或者是相冊(cè)選圖并且把照片壓縮轉(zhuǎn)base64之后上傳到后臺(tái)服務(wù)器,然后服務(wù)器返回識(shí)別結(jié)果。
前端的主要功能點(diǎn)有:
H5如何調(diào)用相機(jī)
圖片如何壓縮
圖片轉(zhuǎn)base64
H5調(diào)用相機(jī)/相冊(cè)
調(diào)用相機(jī)最簡(jiǎn)單的方法就是使用input file[camera]屬性:
<input type="file" capture=camera accept="image/*">//相機(jī)
<input type="file" accept="image/*">//相冊(cè)
這種方法兼容性還是有一定問題的,在iphone手機(jī)上可以正常打開相機(jī),但是在安卓手機(jī)上點(diǎn)擊之后顯示的是相機(jī)、圖庫、文件管理器等混合選擇項(xiàng)。在網(wǎng)上搜了很多都沒有什么好的解決辦法,只能繼續(xù)往下寫了/(ㄒoㄒ)/~~
圖片壓縮
圖片壓縮就要用到FileReader和<canvas>了。
FileReader對(duì)象允許Web應(yīng)用程序異步讀取存儲(chǔ)在計(jì)算機(jī)上的文件的內(nèi)容,使用File或Blob對(duì)象指定要讀取的文件或數(shù)據(jù)。
<canvas>是一個(gè)可以使用腳本在其中繪制圖形的HTML元素,可以繪制圖形和簡(jiǎn)單的動(dòng)畫。
我們要壓縮一張圖片,就要壓縮圖片的分辨率和質(zhì)量,分辨率壓縮我這里是設(shè)置了圖片的最大邊為800,另一邊按照?qǐng)D片原有比例縮放,也可以設(shè)置圖片整體的縮放比例。
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;
}
}
image.src=dataURL;//dataURL通過FileReader獲取
然后就是壓縮圖片的質(zhì)量了,這里設(shè)置的壓縮比為80%,如果設(shè)置太小圖片就失真了。動(dòng)態(tài)創(chuàng)建<canvas>標(biāo)簽然后壓縮圖片:
var quality=80;
var cvs = document.createElement('canvas');
cvs.width = image.width;
cvs.heigh = image.height;
var context=cvs.getContext("2d");
context.drawImage(image, 0, 0,image.width, image.height);
dataURL = cvs.toDataURL('image/jpeg', quality/100);
然后就是上傳到服務(wù)器并展示服務(wù)器的結(jié)果啦,然而事情并沒有那么順利。。。ios手機(jī)拍照壓縮之后圖片就莫名其妙的旋轉(zhuǎn)了,繼續(xù)解決問題。
解決IOS圖片旋轉(zhuǎn)
首先引用exif.js,通過EXIF.getData和EXIF.getTag獲取拍照方向信息。
//file通過input標(biāo)簽獲取
EXIF.getData(file,function(){
orientation=EXIF.getTag(file,'Orientation');
});
下面給出每個(gè)orientation值對(duì)應(yīng)到iphone手機(jī)拍照的含義:
| 描述 | |
|---|---|
| 3 | 橫屏拍攝,此時(shí)home鍵在左側(cè),圖片相對(duì)于原始位置旋轉(zhuǎn)了180度 |
| 6 | 豎屏拍攝,此時(shí)home鍵在下方(正常拿手機(jī)的方向),圖片相對(duì)于原始位置逆時(shí)針旋轉(zhuǎn)可90度 |
| 8 | 豎屏拍攝,此時(shí)home鍵在上方,圖片相對(duì)于原始位置順時(shí)針旋轉(zhuǎn)了90度 |
獲取圖片的方向信息之后,根據(jù)獲取到的值作相應(yīng)的旋轉(zhuǎn)操作。
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)問題
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í)別圖片!")
}
});