<template> <view> <!-- 使用 <camera> 组件来预览摄像头 --> <camera :device-position="cameraPosition" style="width: 100%; height: 400px;"></camera> <!-- 拍照按钮 --> <button @tap="takePhoto">拍照</button> <!-- 录像按钮 --> <button @tap="startRecording" v-if="!isRecording">开始录像</button> <button @tap="stopRecording" v-if="isRecording">停止录像</button> </view> </template> <script> export default { data() { return { cameraPosition: 'back', // 摄像头位置,'front'表示前置摄像头,'back'表示后置摄像头 isRecording: false, // 录像状态 cameraContext: null, // 摄像头上下文对象 }; }, mounted() { // 获取摄像头上下文对象 this.cameraContext = uni.createCameraContext(); }, methods: { // 拍照操作 takePhoto() { this.cameraContext.takePhoto({ success: (res) => { // res.tempImagePath 是拍照后的临时文件路径,可以在此处进行处理 console.log('拍照成功', res.tempImagePath); }, fail: (err) => { console.error('拍照失败', err); }, }); }, // 开始录像操作 startRecording() { this.cameraContext.startRecord({ success: () => { console.log('开始录像'); this.isRecording = true; }, fail: (err) => { console.error('开始录像失败', err); }, }); }, // 停止录像操作 stopRecording() { this.cameraContext.stopRecord({ success: (res) => { // res.tempVideoPath 是录像后的临时文件路径,可以在此处进行处理 console.log('停止录像', res.tempVideoPath); this.isRecording = false; }, fail: (err) => { console.error('停止录像失败', err); this.isRecording = false; }, }); }, }, }; </script>
网友回复
有没有开源的项目将图片视频声音文字转场特效编排自动生成剪映草稿json文件?
有没有摄像头捕获眼球转动操作鼠标的开源代码?
localstorage如何生成自增的键值对进行增删改查?
python有没有将python脚本与python运行环境一键打包成exe的代码?
nodejs如何执行浏览器中运行的js代码?
iframe中如何阻止其他域名网页的打开或跳转?
webrtc如何实现多人音频电话会议?
如何实现uni.connectSocket兼容web与小程序app端的websocket通讯?
webrtc如何浏览器中实现多人群音视频通话会议?
indexdb中的表结构与数据如何导出导入恢复?