前端调用电脑摄像头
作者:小教学发布时间:2023-09-24分类:程序开发学习浏览:75
导读:项目中需要前端调用,所以做了如下操作先看一下效果吧主要是基于vue3,通过canvas把画面转成base64的形式,然后是把base64转成file文件,最后调用了一下上传接口...
项目中需要前端调用,所以做了如下操作
先看一下效果吧
主要是基于vue3,通过canvas把画面转成base64的形式,然后是把base64转成
file文件,最后调用了一下上传接口
但是这个得开启浏览器的摄像头权限,不然会报错
以下是代码
进入页面先调用一下摄像头
navigator.mediaDevices
.getUserMedia({ video: true })
.then((stream) => {
video.value.srcObject = stream
})
.catch((error) => {
console.error(error)
})
state.photoUrl = ''
state.photo = true
拍照
const canvas = document.createElement('canvas')
canvas.width = video.value.videoWidth
canvas.height = video.value.videoHeight
canvas.getContext('2d').drawImage(video.value, 0, 0, canvas.width, canvas.height)
state.photoUrl = canvas.toDataURL('image/png')
转base64
let arr = dataurl.split(',')
let mime = arr[0].match(/:(.*?);/)[1]
let suffix = mime.split('/')[1]
let bstr = atob(arr[1])
let n = bstr.length
let u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${filename}.${suffix}`, {
type: mime,
})
完整代码
<template>
<div>
<div>
<div>摄像头实时画面</div>
<div class="hm">
<video ref="video" v-if="state.photo" autoplay></video>
<img :src="state.photoUrl" v-else>
</div>
</div>
<div class="maT10">
<el-button @click="takePhoto">
拍照
</el-button>
<el-button @click="retake">
重拍
</el-button>
</div>
</div>
</template>
<script setup lang="ts" name="photo">
import axios from 'axios'
const state = reactive({
photo: true,
photoUrl: '',
})
const video = ref()
const takePhoto = () => {
const canvas = document.createElement('canvas')
canvas.width = video.value.videoWidth
canvas.height = video.value.videoHeight
canvas.getContext('2d').drawImage(video.value, 0, 0, canvas.width, canvas.height)
state.photoUrl = canvas.toDataURL('image/png')
clearVideo()
state.photo = false
//将图片发送到接口
let file = base64ImgtoFile(state.photoUrl)
let param = new FormData()
param.append('file', file, file.name)
param.append('fileReName', 'true')
let config = {
headers: {
'Content-Type': 'multipart/form-data',
Authorization: 'token', //此处是token
},
}
let url = import.meta.env.VITE_API_URL + '/api/admin/file/upload-file'
axios.post(url, param, config).then((response) => {})
}
const base64ImgtoFile = (dataurl, filename = 'file') => {
let arr = dataurl.split(',')
let mime = arr[0].match(/:(.*?);/)[1]
let suffix = mime.split('/')[1]
let bstr = atob(arr[1])
let n = bstr.length
let u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${filename}.${suffix}`, {
type: mime,
})
}
const clearVideo = () => {
const stream = video.value.srcObject
const tracks = stream.getTracks()
tracks.forEach((track) => {
track.stop()
})
video.value.srcObject = null
}
const retake = () => {
navigator.mediaDevices
.getUserMedia({ video: true })
.then((stream) => {
video.value.srcObject = stream
})
.catch((error) => {
console.error(error)
})
state.photoUrl = ''
state.photo = true
}
onMounted(() => {
retake()
})
//在离开当前页面的时候把摄像头关了,不然页面一直会显示摄像头的图标
onBeforeUnmount(() => {
video.value.srcObject = null
})
</script>
<style scoped lang="scss">
.hm {
width: 400px;
height: 300px;
video,
img {
width: 100%;
}
}
</style>
- 程序开发学习排行
-
- 1鸿蒙HarmonyOS:Web组件网页白屏检测
- 2HTTPS协议是安全传输,为啥还要再加密?
- 3HarmonyOS鸿蒙应用开发——数据持久化Preferences
- 4记解决MaterialButton背景颜色与设置值不同
- 5鸿蒙HarmonyOS实战-ArkUI组件(RelativeContainer)
- 6鸿蒙HarmonyOS实战-ArkUI组件(Stack)
- 7鸿蒙HarmonyOS实战-ArkUI组件(GridRow/GridCol)
- 8[Android][NDK][Cmake]一文搞懂Android项目中的Cmake
- 9鸿蒙HarmonyOS实战-ArkUI组件(mediaquery)
- 最近发表
-
- WooCommerce最好的WordPress常用插件下载博客插件模块的相关产品
- 羊驼机器人最好的WordPress常用插件下载博客插件模块
- IP信息记录器最好的WordPress常用插件下载博客插件模块
- Linkly for WooCommerce最好的WordPress常用插件下载博客插件模块
- 元素聚合器Forms最好的WordPress常用插件下载博客插件模块
- Promaker Chat 最好的WordPress通用插件下载 博客插件模块
- 自动更新发布日期最好的WordPress常用插件下载博客插件模块
- WordPress官方最好的获取回复WordPress常用插件下载博客插件模块
- Img to rss最好的wordpress常用插件下载博客插件模块
- WPMozo为Elementor最好的WordPress常用插件下载博客插件模块添加精简版