简体   繁体   English

在具有多个摄像头的移动设备上选择默认的后置摄像头

[英]Selecting the default stock rear camera on mobile devices with multiple camera

I have implemented some JavaScript codes to allow user to scan QR code from a Progressive Web App (PWA).我已经实现了一些 JavaScript 代码,以允许用户从 Progressive Web App (PWA) 扫描二维码。

Used QRScanner library: https://github.com/mebjas/html5-qrcode使用的 QRScanner 库: https : //github.com/mebjas/html5-qrcode

    var html5QrCode = new Html5Qrcode("qrScanner");
    const config = { fps: 15, qrbox: 200 };
    
    function qrCodeSuccessCallback(successMessage) {
       console.log(successMessage)
    };
    function qrCodeFailedCallback(failedMessage) {
       return;
    }
    html5QrCode.start({ facingMode: "environment" }, config, qrCodeSuccessCallback, qrCodeFailedCallback)

The problem is on multiple rear camera devices like the Huawei P30 Pro, the JavaScript automatically selects the telephoto lens.问题是在华为 P30 Pro 等多个后置摄像头设备上,JavaScript 会自动选择长焦镜头。

Would like to know are there any solutions to make it automatically selects the device's default stock camera lens?想知道是否有任何解决方案可以使其自动选择设备的默认库存相机镜头?

Extra info额外信息

An alternative way could be having drop down list for user to select their desired camera lens (similar to this demo here ) But trying to avoid it as it will be a downside on the UI/UX for needing user to manually select it in a try and error basis.另一种方式也可以是具有下拉列表,让用户选择自己想要的相机镜头(类似这样的演示在这里),但设法避免它,因为它会在UI / UX不利的一面手动需要用户在尝试选择它和误差基础。

It is possible to get all camera lenses, but it doesn't have any fields to uniquely distinguish which camera is the normal default rear camera in order for me to filter the "devices" It only contains the cameraId and the label.可以获取所有相机镜头,但它没有任何字段来唯一区分哪个相机是正常的默认后置相机,以便我过滤“设备”它只包含cameraId和标签。

// This method will trigger user permissions
Html5Qrcode.getCameras().then(devices => {
  console.log(devices)  // list of devices, which the ID can be used to initialize the camera.
   // e.g. 0: {id: "ed3fb96551169649ccf26f4b7858515ea168a5060463e4e7780f2ade48d30150", label: "HP HD Camera (04ca:706d)"}

In case someone else have this problem, I implemented the following and it seems to work on phones with multiple cameras.如果其他人有这个问题,我实现了以下内容,它似乎适用于具有多个摄像头的手机。

HTML code using bootstrap 4使用引导程序 4 的 HTML 代码

<div class="form-row justify-content-md-center">
    <div class="form-group col-md-4 col-sm-4">
        <div class="justify-content-md-center" id="reader" width="300px" height="300px"></div>
    </div>
</div>

My javascript我的 JavaScript

$(document).ready(function () {

Html5Qrcode.getCameras().then(devices => {
        if (devices && devices.length) {
            var cameraId;
            var cameraLabel;
            if (devices.length === 1) {
                cameraId = devices[0].id;
            } else {
                cameraId = devices[1].id;
                //This is for cellphones with 4 cameras. Usually the first 2 detected are the front so in my case selecting the third in the list worked.
                if (cameraLabel.includes("front")) {
                    cameraId = devices[2].id;
                }
            }

            const html5QrCode = new Html5Qrcode("reader");
            html5QrCode.start(
                cameraId,
                {
                    fps: 10,    
                    qrbox: 250  
                },
                qrCodeMessage => {
                    //Things you want to do when you match a QR Code
                },
                errorMessage => {
                    // parse error, ignore it.
                })
                .catch(err => {
                    // Start failed, handle it.
                });

        }
    }).catch(err => {

    });

}) })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM