溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

H5調(diào)用相機(jī)拍照并壓縮圖片的案例

發(fā)布時(shí)間:2020-10-19 15:35:46 來源:億速云 閱讀:129 作者:小新 欄目:web開發(fā)

H5調(diào)用相機(jī)拍照并壓縮圖片的案例?這個(gè)問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過這個(gè)問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純?nèi)容,讓我們一起來看看吧!

整理文檔,搜刮出一個(gè)H5調(diào)用相機(jī)拍照并壓縮圖片的實(shí)例代碼,稍微整理精簡(jiǎn)一下做下分享。

背景

最近要做一個(gè)h6的頁面,主要功能就是調(diào)用相機(jī)拍照或者是相冊(cè)選圖并且把照片壓縮轉(zhuǎn)base64之后上傳到后臺(tái)服務(wù)器,然后服務(wù)器返回識(shí)別結(jié)果。

前端的主要功能點(diǎn)有:

  1. H5如何調(diào)用相機(jī)

  2. 圖片如何壓縮

  3. 圖片轉(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è)

這個(gè)種方法兼容性還是有問題的,在iphone手機(jī)上可以正常打開相機(jī),但是在安卓手機(jī)上點(diǎn)擊之后是相機(jī)、圖庫(kù)、文件管理器等混合選擇項(xiàng)。在網(wǎng)上搜了很多都沒有什么好的解決辦法,只能繼續(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通過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ī)拍照的含義:

orientation描述
3iphone橫屏拍攝,此時(shí)home鍵在左側(cè),圖片相對(duì)于原始位置旋轉(zhuǎn)了180度
6iphone豎屏拍攝,此時(shí)home鍵在下方(正常拿手機(jī)的方向),圖片相對(duì)于原始位置逆時(shí)針旋轉(zhuǎn)可90度
8iphone豎屏拍攝,此時(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í)別圖片!")
  }
});

感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)H5調(diào)用相機(jī)拍照并壓縮圖片的案例大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI