vue調(diào)取電腦攝像頭實(shí)現(xiàn)拍照功能
本文實(shí)例為大家分享了vue調(diào)取電腦攝像頭實(shí)現(xiàn)拍照功能的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)效果圖:
拍照前&拍照后(我電腦攝像頭擋住的,所以圖片是灰色)
1.點(diǎn)擊拍照上傳功能調(diào)取電腦攝像頭權(quán)限
2.選擇允許使用攝像頭之后,頁面攝像頭區(qū)域開始顯示畫面
3.點(diǎn)擊拍照按鈕,右側(cè)顯示拍攝的圖片。點(diǎn)擊保存即可
完整代碼:
我這里寫的是一個(gè)組件,所以觸發(fā)調(diào)取攝像頭事件是從父組件傳過來的。也可以直接寫一個(gè)頁面上。
<template> <div class="camera-box" style="width: 900px;"> <el-row :gutter="20"> <el-col :span="12"> <div style="text-align: center;font-size: 14px;font-weight: bold;margin-bottom: 10px;">攝像頭</div> <!-- 這里就是攝像頭顯示的畫面 --> <video id="video" width="400" height="300"></video> <div class="iCenter" > <el-button type='primary' size='small' icon="el-icon-camera" @click="takePhone" style="margin-top: 10px;">拍照</el-button> </div> </el-col> <el-col :span="12"> <div style="text-align: center;font-size: 14px;font-weight: bold;margin-bottom: 10px;">拍攝效果</div> <!-- 這里是點(diǎn)擊拍照顯示的圖片畫面 --> <canvas id='canvas' width='400' height='300' style="display: block;"></canvas> <el-button :loading="loadingbut" icon="el-icon-check" type='primary' size='small' @click="takePhoneUpfile" style="margin-top: 10px;">保存</el-button> </el-col> </el-row> </div> </template> <script> import {putFileAttach} from "@/api/customer/animalinfo"; export default { props:{ tackPhoto:{//父組件傳過來的狀態(tài) type:Boolean, default:false } }, data() { return { loadingbut:false, preViewVisible: false, blobFile: null, canvas: null, video: null, mediaStreamTrack: '', } }, watch:{ tackPhoto:{ immediate: true, handler (newVal) {//監(jiān)聽接收到拍攝狀態(tài)后,開始調(diào)取攝像頭權(quán)限。如果功能是在一個(gè)組件里面,下面步驟可以直接寫在自己的觸發(fā)方法里面 if (newVal) { var video = document.querySelector('video'); var text = document.getElementById('text'); // var mediaStreamTrack; // 兼容代碼 window.URL = (window.URL || window.webkitURL || window.mozURL || window.msURL); if (navigator.mediaDevices === undefined) { navigator.mediaDevices = {}; } if (navigator.mediaDevices.getUserMedia === undefined) { navigator.mediaDevices.getUserMedia = function(constraints) { var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; if (!getUserMedia) { return Promise.reject(new Error('getUserMedia is not implemented in this browser')); } return new Promise(function(resolve, reject) { getUserMedia.call(navigator, constraints, resolve, reject); }); } } //攝像頭調(diào)用配置 var mediaOpts = { audio: false, video: true, video: { facingMode: "environment"} // 或者 "user" // video: { width: 1280, height: 720 } // video: { facingMode: { exact: "environment" } }// 或者 "user" } let that=this; navigator.mediaDevices.getUserMedia(mediaOpts).then(function(stream) { that.mediaStreamTrack = stream; video = document.querySelector('video'); if ("srcObject" in video) { video.srcObject = stream } else { video.src = window.URL && window.URL.createObjectURL(stream) || stream } video.play(); }).catch(function (err) { console.log(err) }); } }, deep:true }, }, mounted() { // 攝像頭 this.video = document.getElementById('video'); //畫布 this.canvas = document.getElementById('canvas') }, methods: { takePhone() {//點(diǎn)擊拍照截圖畫面 let that = this; that.canvas.getContext('2d').drawImage(this.video, 0, 0, 400, 300) ; let dataurl = that.canvas.toDataURL('image/jpeg') that.blobFile = that.dataURLtoFile(dataurl, 'camera.jpg'); that.preViewVisible = true }, takePhoneUpfile() {//保存圖片 this.loadingbut=true; let formData = new FormData() formData.append('file', this.blobFile);//圖片內(nèi)容 // 上傳圖片 putFileAttach(formData).then(res=>{//后臺(tái)接口 this.loadingbut=false; this.$emit('picture',res.data.data); this.tackPhoto=false; this.canvas.getContext('2d').clearRect(0, 0, 400, 300);//清除畫布 // 關(guān)閉攝像頭 this.mediaStreamTrack.getVideoTracks().forEach(function (track) { track.stop(); }); },error=>{ this.loadingbut=false; window.console.log(error); }); }, } } </script> <style> .camera-box #canvas{ border: 1px solid #DCDFE6; } </style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue3開啟攝像頭并進(jìn)行拍照的實(shí)現(xiàn)示例
- vue如何調(diào)用攝像頭實(shí)現(xiàn)拍照上傳圖片、本地上傳圖片
- vue調(diào)用攝像頭進(jìn)行拍照并能保存到本地的方法
- Vue實(shí)現(xiàn)調(diào)用PC端攝像頭實(shí)時(shí)拍照
- Vue調(diào)用PC攝像頭實(shí)現(xiàn)拍照功能
- vue調(diào)用本地?cái)z像頭實(shí)現(xiàn)拍照功能
- Vue2.0實(shí)現(xiàn)調(diào)用攝像頭進(jìn)行拍照功能 exif.js實(shí)現(xiàn)圖片上傳功能
- vue 獲取攝像頭拍照并旋轉(zhuǎn)、裁剪生成新的圖片功能實(shí)現(xiàn)
相關(guān)文章
vue中v-model、v-bind和v-on三大指令的區(qū)別詳解
v-model和v-bind都是數(shù)據(jù)綁定的方式,下面這篇文章主要給大家介紹了關(guān)于vue中v-model、v-bind和v-on三大指令的區(qū)別,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11vue element upload實(shí)現(xiàn)圖片本地預(yù)覽
這篇文章主要為大家詳細(xì)介紹了vue element upload實(shí)現(xiàn)圖片本地預(yù)覽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08Vue keep-alive實(shí)踐總結(jié)(推薦)
本篇文章主要介紹了Vue keep-alive實(shí)踐總結(jié)(推薦),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08vue如何實(shí)現(xiàn)關(guān)閉對話框后刷新列表
這篇文章主要介紹了vue如何實(shí)現(xiàn)關(guān)閉對話框后刷新列表,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04Vue3 composition API實(shí)現(xiàn)邏輯復(fù)用的方法
本文主要介紹了Vue3 composition API實(shí)現(xiàn)邏輯復(fù)用的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08