整理文檔,搜刮出一個(gè)H5調(diào)用相機(jī)拍照并壓縮圖片的實(shí)例代碼,稍微整理精簡(jiǎn)一下做下分享。
背景
最近要做一個(gè)h5的頁(yè)面,主要功能就是調(diào)用相機(jī)拍照或者是相冊(cè)選圖并且把照片壓縮轉(zhuǎn)base64之后上傳到后臺(tái)服務(wù)器,然后服務(wù)器返回識(shí)別結(jié)果。
前端的主要功能點(diǎn)有:
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è)
這個(gè)種方法兼容性還是有問(wèn)題的,在iphone手機(jī)上可以正常打開相機(jī),但是在安卓手機(jī)上點(diǎn)擊之后是相機(jī)、圖庫(kù)、文件管理器等混合選擇項(xiàng)。在網(wǎng)上搜了很多都沒(méi)有什么好的解決辦法,只能繼續(xù)往下寫了。。。
圖片壓縮
圖片壓縮就要用到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通過(guò)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é)果啦,然而事情并沒(méi)有那么順利。。。ios手機(jī)拍照壓縮之后圖片莫名的旋轉(zhuǎn)了,繼續(xù)解決問(wèn)題。
解決IOS圖片旋轉(zhuǎn)
首先引用exif.js,通過(guò)EXIF.getData和EXIF.getTag獲取拍照方向信息。
//file通過(guò)input標(biāo)簽獲取EXIF.getData(file,function(){ orientation=EXIF.getTag(file,'Orientation');});
下面給出每個(gè)orientation值對(duì)應(yīng)到iphone手機(jī)拍照的含義:
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í)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選