uni-app 使用uni.createCameraContext()时调用对应方法列表失败 提示不能在扫码状态下调用拍照
uni-app 使用uni.createCameraContext()时调用对应方法列表失败 提示不能在扫码状态下调用拍照
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Windows | HBuilderX |
产品分类:uniapp/小程序/微信
PC开发环境操作系统:Windows
PC开发环境操作系统版本号:windows10企业版64位操作系统
HBuilderX类型:正式
HBuilderX版本号:3.1.12
第三方开发者工具版本号:1.05.2107221
基础库版本号:2.19.0
App下载地址或H5网址:[https://www.dcloud.io/hbuilderx.html](https://www.dcloud.io/hbuilderx.html)
### 示例代码:
```js
onLoad() {
this.ctx = uni.createCameraContext();
uni.authorize({
scope: 'scope.camera',
success() {
console.log('授权了摄像头')
}
})
},
onShow() {
this.animate()
this.startRecord()
},
// 保存拍照画面
takePhoto() {
this.ctx.takePhoto({
quality: 'high',
success: (res) => {
this.src = res.tempImagePath
},
fail: (err) => {
// not allow to invoke takephoto in 'scancode' mode
//不允许在“扫描代码”模式下调用takephoto
console.log('err1', err)
}
});
},
startRecord() {
this.ctx.startRecord({
success: (res) => {
console.log('startRecord', res)
},
fail: (err) => {
console.log('err2', err)
}
})
},
stopRecord() {
this.ctx.stopRecord({
success: (res) => {
console.log('暂停录制参数', res)
// this.setData({
// src: res.tempThumbPath,
// videoSrc: res.tempVideoPath
// })
},
fail: (err) => {
console.log('err3', err)
}
})
},
操作步骤:
会打印出err2 然后提示not allow to invoke takephoto in ‘scancode’ mode
预期结果:
打印出startRecord和回调参数
实际结果:
err1,{errMsg:'not allow to invoke takephoto in 'scancode' mode'}
err2,{errMsg:'not allow to invoke takephoto in 'scancode' mode'}
bug描述:
在进入页面中 马上调用 相机录制 然后提示not allow to invoke takephoto in ‘scancode’ mode 不能在扫码模式下 拍照 但是我是调用的录像啊,然后在扫码成功之后停止录制,获取到最后一针图像 显示在扫码区域 以便后续的页面停留操作。
更多关于uni-app 使用uni.createCameraContext()时调用对应方法列表失败 提示不能在扫码状态下调用拍照的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于uni-app 使用uni.createCameraContext()时调用对应方法列表失败 提示不能在扫码状态下调用拍照的实战教程也可以访问 https://www.itying.com/category-93-b0.html
这个错误提示表明当前相机处于扫码模式,而 takePhoto 和 startRecord 方法在扫码模式下无法调用。根据微信小程序官方文档,camera 组件的模式(mode)分为两种:normal(普通相机模式)和 scanCode(扫码模式)。在扫码模式下,只能调用扫码相关的方法,不能调用拍照或录像功能。
从你的代码来看,问题可能出现在以下几个方面:
-
未指定相机模式:如果
camera组件未明确设置mode属性,默认可能是scanCode模式。建议在camera组件中显式设置mode="normal",以确保相机处于普通模式。 -
时序问题:在
onShow中立即调用startRecord可能存在问题,因为相机可能尚未初始化完成。建议在camera组件的onReady事件触发后再调用录制或拍照方法。 -
权限问题:虽然你已授权摄像头权限,但扫码模式可能还需要额外的权限或配置。
修改建议:
- 在
camera组件中添加mode="normal"属性。 - 将
startRecord的调用移到camera组件的onReady回调中。 - 确保
takePhoto和startRecord只在普通模式下调用。
示例修改:
<template>
<camera mode="normal" [@ready](/user/ready)="onCameraReady"></camera>
</template>
<script>
export default {
methods: {
onCameraReady() {
this.startRecord();
}
}
}
</script>

