javascript - ZXing force rear camera on android device? - Stack Overflow

I am building a project that uses the ZXing library to scan barcodes.Currently the code I have is wor

I am building a project that uses the ZXing library to scan barcodes. Currently the code I have is working for iOS device opening up the rear camera but when testing using android device it opens up the front camera. Is there a way I can always force the rear camera to be used on any device? Please see working code below:

    <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>

I am building a project that uses the ZXing library to scan barcodes. Currently the code I have is working for iOS device opening up the rear camera but when testing using android device it opens up the front camera. Is there a way I can always force the rear camera to be used on any device? Please see working code below:

    <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>
Share Improve this question asked Aug 10, 2020 at 12:00 techietalktechietalk 1192 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

remove selectedDeviceId and use undefined

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

documentation says if you use undefined it will automatically choose the camera, preferring the main (environment facing) camera if more are available.

The error occurs because codeReader.getVideoInputDevices() returns different result

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745213221a4616966.html

相关推荐

  • javascript - ZXing force rear camera on android device? - Stack Overflow

    I am building a project that uses the ZXing library to scan barcodes.Currently the code I have is wor

    9小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信