【问题标题】:ZXing force rear camera on android device?ZXing force 安卓设备上的后置摄像头?
【发布时间】:2020-08-10 12:00:40
【问题描述】:

我正在构建一个使用 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>

【问题讨论】:

    标签: javascript android android-camera zxing zxing-js


    【解决方案1】:

    删除 selectedDeviceId 并使用 undefined

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

    文档说,如果您使用 undefined,它将自动选择摄像头,如果有更多可用的摄像头,则首选主(面向环境的)摄像头。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      相关资源
      最近更新 更多