簡體   English   中英

Javascript后置攝像頭

[英]Javascript Rear Camera

嗨,我正在嘗試使用此演示https://kdzwinel.github.io/JS-OCR-demo/

但是我只得到手機的前置攝像頭,它使用此代碼啟用攝像頭

             function setupVideo(rearCameraId) {
    var deferred = new $.Deferred();
    var getUserMedia = Modernizr.prefixed('getUserMedia', navigator);
    var videoSettings = {
        video: {
            optional: [
                {
                    width: {min: pictureWidth}
                },
                {
                    height: {min: pictureHeight}
                }
            ]
        }
    };

    //if rear camera is available - use it
    if (rearCameraId) {
        videoSettings.video.optional.push({
            sourceId: rearCameraId
        });
    }

    getUserMedia(videoSettings, function (stream) {
        //Setup the video stream
        video.src = window.URL.createObjectURL(stream);

        window.stream = stream;

        video.addEventListener("loadedmetadata", function (e) {
            //get video width and height as it might be different than we requested
            pictureWidth = this.videoWidth;
            pictureHeight = this.videoHeight;

            if (!pictureWidth && !pictureHeight) {
                //firefox fails to deliver info about video size on time (issue #926753), we have to wait
                var waitingForSize = setInterval(function () {
                    if (video.videoWidth && video.videoHeight) {
                        pictureWidth = video.videoWidth;
                        pictureHeight = video.videoHeight;

                        clearInterval(waitingForSize);
                        deferred.resolve();
                    }
                }, 100);
            } else {
                deferred.resolve();
            }
        }, false);
    }, function () {
        deferred.reject('There is no access to your camera, have you denied it?');
    });

    return deferred.promise();
}

而且我嘗試添加代碼以從https://simpl.info/getusermedia/sources/選擇攝像機

但是沒有成功:(在所有的例子中,我如何在沒有太多麻煩的情況下使用后置攝像頭

以下在Fi​​refox和Chrome中都可以解決問題

const constraints = {
 "video": {
   "facingMode": 
      { "ideal": "environment" }
  }
};

const stream = await navigator.mediaDevices.getUserMedia(constraints);

演示在這里: https : //js-1lq5ue.stackblitz.io/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM