整理文檔,搜刮出一個H5調用相機拍照並壓縮圖片的實例代碼,稍微整理精簡一下做下分享。
背景最近要做一個h5的頁面,主要功能就是調用相機拍照或者是相冊選圖並且把照片壓縮轉base64之後上傳到後台服務器,然後服務器返回識別結果。
前端的主要功能點有:
調用相機最簡單的方法就是使用input file[camera]屬性:
<input type=file capture=camera accept=image/*>//相機<input type=file accept=image/*>//相冊
這個種方法兼容性還是有問題的,在iphone手機上可以正常打開相機,但是在安卓手機上點擊之後是相機、圖庫、文件管理器等混合選擇項。在網上搜了很多都沒有什麼好的解決辦法,只能繼續往下寫了。 。 。
圖片壓縮圖片壓縮就要用到FileReader
和<canvas>
了。
FileReader對象允許Web應用程序異步讀取存儲在計算機上的文件的內容,使用File或Blob對象指定要讀取的文件或數據。
<canvas>是一個可以使用腳本在其中繪製圖形的HTML元素,可以繪製圖形和簡單的動畫。
圖片壓縮要壓縮圖片的分辨率和質量,分辨率壓縮我這裡是設置了圖片的最大邊為800,另一邊按照圖片原有比例縮放,也可以設置圖片整體的縮放比例。
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獲取
然後就是壓縮圖片的質量了,這裡設置壓縮了80%,如果設置太小圖片就失真了。動態創建<canvas>標籤然後壓縮圖片:
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);
然後就是上傳到服務器並展示服務器的結果啦,然而事情並沒有那麼順利。 。 。 ios手機拍照壓縮之後圖片莫名的旋轉了,繼續解決問題。
解決IOS圖片旋轉首先引用exif.js,通過EXIF.getData和EXIF.getTag獲取拍照方向信息。
//file通過input標籤獲取EXIF.getData(file,function(){ orientation=EXIF.getTag(file,'Orientation');});
下面給出每個orientation值對應到iphone手機拍照的含義:
orientation | 描述 |
---|---|
3 | iphone橫屏拍攝,此時home鍵在左側,圖片相對於原始位置旋轉了180度 |
6 | iphone豎屏拍攝,此時home鍵在下方(正常拿手機的方向),圖片相對於原始位置逆時針旋轉可90度 |
8 | iphone豎屏拍攝,此時home鍵在上方,圖片相對於原始位置順時針旋轉了90度 |
switch (orientation) { case 6: case 8: cvs.width = height; cvs.height = width; break;}var context=cvs.getContext(2d);switch(orientation){ //iphone橫屏拍攝,此時home鍵在左側case 3: // 180度向左旋轉context.translate(width, height); context.rotate(Math.PI); break; //iphone豎屏拍攝,此時home鍵在下方(正常拿手機的方向) case 6: context.rotate(0.5 * Math.PI); context.translate(0, -height); break; //iphone豎屏拍攝,此時home鍵在上方case 8: // 逆時針旋轉90度context.rotate(-0.5 * Math.PI); context.translate(-width, 0); break;}
然後再上傳圖片,發現在IOS下圖片已經正常了。
下面給出完整代碼:
$('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圖片旋轉問題switch(orientation){ //iphone橫屏拍攝,此時home鍵在左側case 3: // 180度向左旋轉context.translate(width, height); context.rotate(Math.PI); break; //iphone豎屏拍攝,此時home鍵在下方(正常拿手機的方向) case 6: context.rotate(0.5 * Math.PI); context.translate(0, -height); break; //iphone豎屏拍攝,此時home鍵在上方case 8: // 逆時針旋轉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); //獲取識別結果... } image.src=dataURL; }; reader.readAsDataURL(file); }else{ alert(只能識別圖片! ) }});
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。