uni-app 多次扫码后会黑屏 安卓版本7.1.2

uni-app 多次扫码后会黑屏 安卓版本7.1.2

开发环境 版本号 项目创建方式
Windows 20H2 HBuilderX
产品分类:uniapp/App

PC开发环境操作系统:Windows

PC开发环境操作系统版本号:20H2

HBuilderX类型:正式

HBuilderX版本号:3.1.12

手机系统:Android

手机系统版本号:Android 7.1.1

手机厂商:手持机pda

手机机型:手持机pda

页面类型:vue

打包方式:云端

项目创建方式:HBuilderX

示例代码:
```cpp
// 开始装车扫码
startScan () {
	const _this = this
	uni.scanCode({
		onlyFromCamera:true,
		success: function(res) {
			uni.showLoading({
				title: '正在识别中'
			})
			console.log('条码类型:' + res.scanType)
			console.log('条码类容:' + res.result)
			_this.$request({
				url: '/ymt/road/getOrderAndCheckLoading/' + res.result + '/' + 101
			}).then(res1 => {
				console.log(res1)
				if (res1.statusCode === 200) {
					uni.hideLoading();
					uni.navigateTo({
						url: '/pages/startLoadCar/startLoadCar?obj=' + encodeURIComponent(JSON.stringify(res1.data.data))
					})
				} else if (res1.statusCode === 500) {
					console.log('状态码500')
					uni.hideLoading();
					uni.hideToast();
					uni.showModal({
						title: '提示',
						showCancel: false,
						content: res1.data.msg,
						success: function (res) {
							console.log('用户点击确定');
						}
					});
				} else {
					uni.hideLoading();
					uni.showToast({
						title: res1.data.msg,
						icon: "none",
						duration: 4000
					})
				}
			}).catch(err => {
				console.log(err)
				uni.hideLoading();
				uni.showToast({
					title: '扫码出错',
					icon: "none",
					duration: 4000
				})
			})
		}
	})
},

操作步骤:
- 多次点击扫码

预期结果:
- 点击多次后不该黑屏

实际结果:
- 多次扫码后会出现扫码黑屏

bug描述:
- 多次扫码后出现黑屏的情况该怎么处理呢,是否需要升级安卓版本?

更多关于uni-app 多次扫码后会黑屏 安卓版本7.1.2的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app 多次扫码后会黑屏 安卓版本7.1.2的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个典型的摄像头资源未正确释放问题。在Android 7.1.2系统上,多次调用uni.scanCode会导致摄像头资源累积,最终引发黑屏。

建议在每次扫码完成后强制关闭扫码界面:

startScan () {
    const _this = this
    uni.scanCode({
        onlyFromCamera: true,
        success: function(res) {
            // 立即关闭扫码界面
            plus.barcode.close();
            
            uni.showLoading({
                title: '正在识别中'
            });
            
            // 后续处理逻辑保持不变
            _this.$request({
                url: '/ymt/road/getOrderAndCheckLoading/' + res.result + '/' + 101
            }).then(res1 => {
                // ... 原有逻辑
            });
        },
        fail: function(err) {
            // 扫码失败时也要关闭
            plus.barcode.close();
            console.log('扫码失败:', err);
        }
    });
}

关键点:

  1. 在success和fail回调中都调用plus.barcode.close()强制释放摄像头资源
  2. 这能避免多次扫码导致的摄像头资源占用累积
  3. 不需要升级Android版本,这是代码层面的资源管理问题

另外建议添加防抖处理,避免用户快速连续点击:

let isScanning = false;

startScan () {
    if (isScanning) return;
    
    isScanning = true;
    const _this = this;
    
    uni.scanCode({
        onlyFromCamera: true,
        success: function(res) {
            plus.barcode.close();
            // ... 后续逻辑
        },
        complete: function() {
            isScanning = false;
        }
    });
}
回到顶部