javascript - Android 设备上的 ZXing force 后置摄像头?

标签 javascript android android-camera zxing

我正在构建一个使用 ZXing 库扫描条形码的项目。目前,我拥有的代码适用于打开后置摄像头的 iOS 设备,但在使用 Android 设备进行测试时,它会打开前置摄像头。有没有一种方法可以让我始终强制在任何设备上使用后置摄像头?请参阅下面的工作代码:

    <script type="text/javascript">
        window.addEventListener('load', function () {
            let selectedDeviceId;
            const codeReader = new ZXing.BrowserMultiFormatReader();
            console.log('ZXing code reader initialized');
            codeReader.getVideoInputDevices()
                .then((videoInputDevices) => {
                    if (videoInputDevices.length < 1) {
                        console.log('No video devices found');
                        return;
                    }

                    selectedDeviceId = videoInputDevices[0].deviceId;

                    codeReader.decodeFromVideoDevice(selectedDeviceId, 'video', (result, err) => {
                        if (result) {
                            console.log(result);
                            var barcode = result;
                            //this.window.alert(barcode);
                            if (String(barcode).charAt(0) == 'L') {
                                document.getElementById('result').textContent = result.text;
                                document.getElementById('result').style.color = "green";
                                var previousurl = document.referrer;
                                window.location.href = previousurl + "&BarCode=" + result.text;
                            }
                            else {

                                document.getElementById('result').textContent = result.text;
                                document.getElementById('result').style.color = "red";
                                window.alert("Incorrect Barcode scan value.  Please try again.")
                            }

                        }
                        if (err && !(err instanceof ZXing.NotFoundException)) {
                            console.error(err);
                            document.getElementById('result').textContent = err;
                        }
                    })
                    console.log(`Started continous decode from camera with id ${selectedDeviceId}`)
                })
                .catch((err) => {
                    console.error(err)
                })
        })

    </script>

最佳答案

删除 selectedDeviceId 并使用 undefined

codeReader.decodeFromVideoDevice(undefined, 'video', (result, err) => {
                        if (result) {

文档说如果你使用 undefined 它会自动选择相机,如果有更多可用的相机,它会优先选择主(面向环境)相机。

错误发生是因为codeReader.getVideoInputDevices()返回不同的结果

关于javascript - Android 设备上的 ZXing force 后置摄像头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63339772/

相关文章:

java - Android,如何一次定义多个变量

Android 传感器旋转角度

java - 内部应用程序相机照片不正确地保存到图库

Android M Camera Intent + 权限错误?

Android TextureView OpenGLRenderer: GL_INVALID_OPERATION

javascript - ViewData 未通过 Jquery 显示

javascript regexp 在字符串末尾找到第三个字符

javascript - 页面刷新后自动调用Ajax

javascript - jquery扩展和调整大小问题

java - Android 文本中的笔划问题