uniapp微信小程序自定义相机 ,给相机添加辅助框,解决拒绝相机默认授权后无法再次拉起相机授权问题

news/2025/2/12 0:31:27/

微信小程序自定义相机


效果

在这里插入图片描述

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

文章目录

  • 微信小程序自定义相机
  • 效果
  • 需求
  • 一、小程序自定义相机
    • 1.使用uniapp的camera组件
  • 二、使用cover-view,cover-image加辅助边框
  • 三、相机拒绝授权后,无法再次拉起授权问题
  • 完整的代码


需求

uni.chooseMedia可以拉起手机自带相机,但是特殊情况下需要给相机加框和辅助线,比如相机身份证辅助框,这时就会用到uni-app的camera组件,使用 cover-view cover-image 覆盖在上面,实现相机辅助框的效果


一、小程序自定义相机

1.使用uniapp的camera组件

camera组件链接地址:https://uniapp.dcloud.net.cn/component/camera.html#camera

代码如下(示例):

<template><view><camera device-position="back" flash="off" @error="error" style="width: 100%; height: 300px;"></camera><button type="primary" @click="takePhoto">拍照</button><view>预览</view><image mode="widthFix" :src="src"></image></view>
</template>
export default {data() {return {src:""}},methods: {takePhoto() {const ctx = uni.createCameraContext();ctx.takePhoto({quality: 'high',success: (res) => {this.src = res.tempImagePath}});},error(e) {console.log(e.detail);}}
}

属性说明:
在这里插入图片描述

二、使用cover-view,cover-image加辅助边框

代码如下(示例):

<camera mode="normal" :device-position="devicePosition" :flash="flashStyle":style="{ height: cameraHeight + 'px' }" @error="errorCamera"><cover-view class="controls" style="width: 100%;height: 100%;"><cover-view class="controls1-bgcolor"></cover-view><cover-view class="controls2-bgcolor"><!-- 人像照 --><cover-image v-if="photoType=='idpositive'" class="w569-h828" :src="front" /><!-- 国徽照 --><cover-image v-else class="w569-h828" :src="side" /></cover-view><cover-view class="controls3-bgcolor"></cover-view></cover-view>
</camera>

三、相机拒绝授权后,无法再次拉起授权问题

相机初次进入会拉起授权,授权拒绝后无法再次拉起,针对这个问题,可以先通过uni.getSetting获取用户的当前设置;

代码如下(示例):

uni.openSetting({success(res) {console.log(res.authSetting)}
});

然后通过uni.openSetting调起客户端小程序设置界面,进行设置,并返回用户设置的操作结果。

uni.openSetting({success(res) {console.log(res.authSetting)}
});

解决相机无法拉起授权问题重点:什么时候获取用户的当前设置?什么时候拉起相机小程序相机设置界面?
1、获取用户当前设置只能在camera组件使用页面使用该方法
2、如果小程序初始化就获取当前设置 并拉起相机设置界面,会存在相机初始化时同时加载默认授权弹框和自定义弹框
3、由于相机授权决绝后即不允许使用摄像头,@error事件刚好是 用户不允许使用摄像头时触发,正确的使用是在@error事件中使用

errorCamera(e){const that = this;uni.getSetting({success(res) {if (!res.authSetting["scope.camera"]) {uni.showModal({title: '提示',content: '请开启摄像头权限,否则无法拍照',confirmText: '去开启',success(res) {if (res.confirm) {uni.openSetting({success(res) {if (res.authSetting["scope.camera"]) {uni.navigateBack({delta: 1})} else {uni.navigateBack({delta: 1})}}})} else if (res.cancel) {uni.navigateBack({delta: 1})}}})}}})},			

在这里插入图片描述


完整的代码

提示:相机框引导框可以让ui设计

代码如下(示例):

<template><view :style="{ height: windowHeight + 'px' }"><camera mode="normal" :device-position="devicePosition" :flash="flashStyle":style="{ height: cameraHeight + 'px' }" @error="errorCamera"><cover-view class="controls" style="width: 100%;height: 100%;"><cover-view class="controls1-bgcolor"></cover-view><cover-view class="controls2-bgcolor"><!-- 人像照 --><cover-image v-if="photoType=='idpositive'" class="w569-h828" :src="front" /><!-- 国徽照 --><cover-image v-else class="w569-h828" :src="side" /></cover-view><cover-view class="controls3-bgcolor"></cover-view></cover-view></camera><view class="bottom font-36-fff"><view class="wrap"><view class="back" @click="switchBtn"><image :src="flip" mode="" style="width: 60rpx; height: 60rpx;"></image></view><!-- <view class="back" @click="flashBtn">闪光灯</view> --><view @click="takePhoto"><image class="w131-h131" :src="icon"></image></view><view @click="chooseImage"><image :src="picture" mode="" style="width: 60rpx; height: 60rpx;"></image></view></view></view></view>
</template><script>export default {data() {return {front: '../../static/idcard/front.png', // 人像照side: '../../static/idcard/side.png', // 国徽照flip: '../../static/idcard/flip.png', // 反转icon: '../../static/idcard/icon.png', // 相机picture: '../../static/idcard/picture.png', // 照片cameraContext: {},windowHeight: '',cameraHeight: '',idcardFrontSide: true,photoType: '',devicePosition: 'back', // 摄像头默认后置flashStyle: 'off',};},onLoad(options) {if (uni.createCameraContext) {this.cameraContext = uni.createCameraContext()} else {// 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示uni.showModal({title: '提示',content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'})}this.photoType = options.photoType;this.devicePosition = 'back';},onShow() {const systemInfo = uni.getSystemInfoSync()this.windowHeight = systemInfo.windowHeightthis.cameraHeight = systemInfo.windowHeight - 80},methods: {errorCamera(e){const that = this;uni.getSetting({success(res) {if (!res.authSetting["scope.camera"]) {uni.showModal({title: '提示',content: '请开启摄像头权限,否则无法拍照',confirmText: '去开启',success(res) {if (res.confirm) {uni.openSetting({success(res) {if (res.authSetting["scope.camera"]) {uni.navigateBack({delta: 1})} else {uni.navigateBack({delta: 1})}}})} else if (res.cancel) {uni.navigateBack({delta: 1})}}})}}})},// 拍照takePhoto() {uni.showLoading({title: '拍摄中'})this.cameraContext.takePhoto({quality: 'normal',success: (res) => {console.log('拍摄照片', res)let idPhoto = res.tempImagePath;this.chosePhoto(idPhoto);uni.showToast({title: '拍照成功',icon: 'none',duration: 1200})},fail: (err) => {uni.showToast({title: '拍照失败,请检查系统是否授权',icon: 'none',duration: 1200})}})},// 从相册选取chooseImage() {uni.chooseImage({count: 1,sizeType: ['original', 'compressed'],sourceType: ['album'],success: (res) => {let idPhoto = res.tempFilePaths[0];this.chosePhoto(idPhoto);},fail: (err) => {}});},//反转switchBtn() {if (this.devicePosition == 'front') {this.devicePosition = 'back'} else {this.devicePosition = 'front'}},// flashBtn(){// 	if(this.flashStyle == 'on'){// 		this.flashStyle = 'off'// 	}else{// 		this.flashStyle = 'on'// 	}// },// 选择图片跳转chosePhoto(item) {if (this.photoType == 'idpositive') {let pages = getCurrentPages(); //获取所有页面栈实例列表let nowPage = pages[pages.length - 1]; //当前页页面实例let prevPage = pages[pages.length - 2]; //上一页页面实例prevPage.$vm.idpositive = item; //修改上一页data里面的参数值prevPage.$vm.checkoutface = true; //修改上一页data里面的参数值uni.navigateBack({ //uni.navigateTo跳转的返回,默认1为返回上一级delta: 1});} else if (this.photoType == 'idback') {let pages = getCurrentPages(); //获取所有页面栈实例列表let nowPage = pages[pages.length - 1]; //当前页页面实例let prevPage = pages[pages.length - 2]; //上一页页面实例prevPage.$vm.idback = item; //修改上一页data里面的参数值prevPage.$vm.checkoutId = true; //修改上一页data里面的参数值uni.navigateBack({ //uni.navigateTo跳转的返回,默认1为返回上一级delta: 1});}}},}
</script><style lang="less" scoped>.icon-w569-h828 {width: 569rpx;height: 828rpx;}.controls {display: flex;align-items: center;justify-content: center;flex-direction: column;}.controls1-bgcolor {list-style: none;padding: 0;margin: 0;width: 100%;height: 60%;background-color: rgba(248, 248, 248, 0.6);}.controls2-bgcolor {list-style: none;padding: 0;margin: 0;width: 100%;height: 110%;display: flex;align-items: center;justify-content: center;}.controls2-bgcolor1 {width: 7%;height: 150%;background-color: rgba(248, 248, 248, 0.6);}.controls3-bgcolor {list-style: none;padding: 0;margin: 0;width: 100%;height: 60%;background-color: rgba(248, 248, 248, 0.6);}.bottom {width: 100%;background-color: #000;.wrap {display: flex;align-items: center;justify-content: space-between;height: 80px;padding: 0 73rpx;}}.w569-h828 {width: 650rpx;height: 460rpx;background-color: rgba(0, 0, 0, 0) !important;}.w131-h131 {width: 131rpx;height: 131rpx;}.font-36-fff {font-size: 36rpx;color: #fff;}
</style>

http://www.ppmy.cn/news/696076.html

相关文章

小程序相机组件「camera」

组件的使用途径 使用camera组件后&#xff0c;只会触发摄像头&#xff0c;不会使用摄像头的默认样式&#xff0c;进入camera组件后&#xff0c;会全屏显示相机没有拍照按钮以及相机的各种样式。 1.将需要使用相机组件的wxml组件中输入camera标签 //属性方法看上图↑<came…

[安卓相机1]简单小Demo

安卓相机1-简单Demo 一、创建文件二、添加对应布局三、定义Preview类三、将Preview添加到FrameLayout四、在Manifest文件中声明相机权限最后 一、创建文件 1.创建一个新的工程, 这里的详细过程就略过了.具体请看参考中新建一个空的app部分 二、添加对应布局 修改布局, 使用F…

小程序开发之组件camera(系统相机)

camera 系统相机。该组件是原生组件&#xff0c;使用时请注意相关限制。 扫码二维码功能&#xff0c;需升级微信客户端至6.7.3。 需要用户授权 scope.camera 相关api&#xff1a;wx.createCameraContext 注&#xff1a; 请注意原生组件使用限制。同一页面只能插入一个 cam…

微信小程序相机功能

<form action"" bindsubmit"submit"> <input type"text" placeholder"账号"name"user"/> <input type"text"placeholder"密码"name"psd"/> <button form-type"sub…

小觅相机的相机标定全家桶(相机IMU,相机内参,相机外参)

性感帅哥博主在线标定小觅双目相机!!!(亲测有效系列!) 刚刚入手新小觅相机,结果飘出天际,很让人头疼!所以… 话不多说,开始骚操作! mkdir mynt_ws #创建文件夹 cd ~/mynt_ws #进入文件夹 mkdir src #创建文件夹 cd ~/mynt/src catkin_init_workspac…

小觅相机使用记录

安装环境 运行历程 ROS与深度相机入门教程-在ROS使用小觅深度相机 https://www.ncnynl.com/archives/201910/3435.html ros安装 rosdep init ROS安装问题解决方案 rosdep init ROS安装问题解决方案 - 古月居 官方SDK说明 SDK 样例 — MYNT EYE D SDK 1.8.0 文档 ubuntu…

小觅相机 相机以及IMU外参标定

最近在使用IMU和双目相机进行相关VIO算法的测试&#xff0c;首先要对IMU和相机的外参进行标定&#xff0c;本文主要是对标定过程做一个全面的记录&#xff0c;方便总结和讨论。测试中采用的是小觅双目模组标准版S1030-IR-120/Mono&#xff0c;整个标定过程都是依赖标定工具Kali…

开源一个水印相机小程序代码

文字很短&#xff0c;内容不多&#xff0c;就开源一个小程序。 一款智能水印相机&#xff0c;拍照自动添加时间、地点、经纬度等水印文字&#xff0c;可用于工作考勤、学习打卡、工作取证等&#xff0c;支持自定义内容以及给现有照片添加水印。无需安装&#xff0c;无需注册&am…