vue項目中實現拍照功能


vue文件中

 1 <!--開啟攝像頭的彈窗-->
 2 <div class="info2" @click="onTake">
 3   <el-image :src="url" style="width: 100%; height: 100%"></el-image>
 4 </div>
 5 
 6 <!--開啟攝像頭的拍照和-->
 7 <el-dialog title="拍照上傳" :visible.sync="visible" @close="onCancel1" width="1065px">
 8   <div class="box">
 9     <video id="videoCamera" class="canvas" :width="videoWidth" :height="videoHeight" autoPlay></video>
10     <canvas id="canvasCamera" class="canvas" :width="videoWidth" :height="videoHeight"></canvas>
11   </div>
12   <div slot="footer">
13     <el-button @click="drawImage" icon="el-icon-camera" size="small">
14       拍照
15     </el-button>
16     <el-button v-if="os" @click="getCompetence" icon="el-icon-video-camera" size="small">
17       打開攝像頭
18     </el-button>
19     <el-button v-else @click="stopNavigator" icon="el-icon-switch-button" size="small">
20       關閉攝像頭
21     </el-button>
22     <el-button @click="resetCanvas" icon="el-icon-refresh" size="small">
23       重置
24     </el-button>
25     <el-button @click="onCancel" icon="el-icon-circle-close" size="small">
26       完成
27     </el-button>
28   </div>
29 </el-dialog>

js

  1 //對參數進行設置
  2 data() {
  3   return {
  4     url:"",// 上傳的圖片的地址
  5     visible: false, //彈窗
  6     videoWidth: 500,// 繪畫布的寬高
  7     videoHeight: 400,
  8     os: false, //控制攝像頭開關
  9     thisCancas: null,
 10     thisContext: null,
 11     thisVideo: null,
 12     imgSrc: undefined,
 13     imgFile: null,
 14   }
 15 }
 16 /*調用攝像頭拍照開始*/
 17 onTake() {
 18   this.visible = true;
 19   this.getCompetence();
 20 },
 21 
 22 /*關閉彈窗,以及關閉攝像頭功能*/
 23 onCancel1() {
 24   this.visible = false;
 25   this.stopNavigator(); // 關閉攝像頭
 26 },
 27 
 28 // 調用攝像頭權限
 29 getCompetence() {
 30   //必須在model中render后才可獲取到dom節點,直接獲取無法獲取到model中的dom節點
 31   this.$nextTick(() => {
 32     const _this = this;
 33     this.os = false; //切換成關閉攝像頭
 34     // 獲取畫布節點
 35     this.thisCancas = document.getElementById("canvasCamera");
 36     // 為畫布指定繪畫為2d類型
 37     this.thisContext = this.thisCancas.getContext("2d");
 38     //獲取video節點
 39     this.thisVideo = document.getElementById("videoCamera");
 40     // 舊版本瀏覽器可能根本不支持mediaDevices,我們首先設置一個空對象
 41     if (navigator.mediaDevices === undefined) {
 42       navigator.menavigatordiaDevices = {};
 43     }
 44     // 一些瀏覽器實現了部分mediaDevices,我們不能只分配一個對象
 45     // 使用getUserMedia,因為它會覆蓋現有的屬性。
 46     // 這里,如果缺少getUserMedia屬性,就添加它。
 47     if (navigator.mediaDevices.getUserMedia === undefined) {
 48       navigator.mediaDevices.getUserMedia = function(constraints) {
 49         // 首先獲取現存的getUserMedia(如果存在)
 50         let getUserMedia =
 51           navigator.webkitGetUserMedia ||
 52           navigator.mozGetUserMedia ||
 53           navigator.getUserMedia;
 54         // 有些瀏覽器不支持,會返回錯誤信息
 55         // 保持接口一致
 56         if (!getUserMedia) {
 57           return Promise.reject(
 58             new Error("getUserMedia is not implemented in this browser")
 59           );
 60         }
 61         // 否則,使用Promise將調用包裝到舊的navigator.getUserMedia
 62         return new Promise(function(resolve, reject) {
 63           getUserMedia.call(navigator, constraints, resolve, reject);
 64         });
 65       };
 66     }
 67     const constraints = {
 68       audio: false,
 69       video: {
 70         width: _this.videoWidth,
 71         height: _this.videoHeight,
 72         transform: "scaleX(-1)"
 73       }
 74     };
 75     navigator.mediaDevices
 76       .getUserMedia(constraints)
 77       .then(function(stream) {
 78         // 舊的瀏覽器可能沒有srcObject
 79         if ("srcObject" in _this.thisVideo) {
 80           _this.thisVideo.srcObject = stream;
 81         } else {
 82           // 避免在新的瀏覽器中使用它,因為它正在被棄用。
 83           _this.thisVideo.src = window.URL.createObjectURL(stream);
 84         }
 85         _this.thisVideo.onloadedmetadata = function(e) {
 86           _this.thisVideo.play();
 87         };
 88       })
 89       .catch(err => {
 90         this.$notify({
 91           title: "警告",
 92           message: "沒有開啟攝像頭權限或瀏覽器版本不兼容.",
 93           type: "warning"
 94         });
 95       });
 96   });
 97 },
 98 
 99 //調用攝像頭 --- 進行繪制圖片
100 drawImage() {
101   // 點擊,canvas畫圖
102   this.thisContext.drawImage(
103     this.thisVideo,
104     0,
105     0,
106     this.videoWidth,
107     this.videoHeight
108   );
109   // 獲取圖片base64鏈接
110   this.imgSrc = this.thisCancas.toDataURL("image/png");
111 
112   /*const imgSrc=this.imgSrc;*/
113 },
114 //清空畫布
115 clearCanvas(id) {
116   let c = document.getElementById(id);
117   let cxt = c.getContext("2d");
118   cxt.clearRect(0, 0, c.width, c.height);
119 },
120 
121 //重置畫布
122 resetCanvas() {
123   // this.imgSrc = "";
124   this.clearCanvas("canvasCamera");
125 },
126 
127 //關閉攝像頭
128 stopNavigator() {
129   if (this.thisVideo && this.thisVideo !== null) {
130     this.thisVideo.srcObject.getTracks()[0].stop();
131     this.os = true; //切換成打開攝像頭
132   }
133 }
134 /*調用攝像頭拍照結束*/
135 
136 /*完成拍照並對其照片進行上傳*/
137 onCancel() {
138   this.visible = false;
139   /* this.resetCanvas();*/
140   // console.log(this.imgSrc);
141   this.imgFile = this.dataURLtoFile(this.imgSrc, new Date() + ".png");
142   console.log(this.imgFile);
143   this.stopNavigator();
144   // let par = {
145   //   photo: this.imgFile,
146   // };
147   let data = new FormData();
148   data.append("photo", this.imgFile); //1是圖片,2是視頻
149   // data.append("code", this.addForm.code);
150   console.log(data);
151   // checkbeforepersonalphoto上傳圖片的接口
152   checkbeforepersonalphoto(data).then(res => {
153     if (res.code == "1") {
154       this.$message({
155         message: "上傳成功",
156         type: "success"
157       });
158       this.url = res.data;
159     }
160   });
161 },
162 
163 dataURLtoFile(dataurl, filename) {
164   var arr = dataurl.split(",");
165   var mime = arr[0].match(/:(.*?);/)[1];
166   var bstr = atob(arr[1]);
167   var n = bstr.length;
168   var u8arr = new Uint8Array(n);
169   while (n--) {
170     u8arr[n] = bstr.charCodeAt(n);
171   }
172   return new File([u8arr], filename, { type: mime });
173 },

css

.info2 {
  width: 10%;
  height: 100px;
}
.box {
  display:flex;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM