简体   繁体   English

Javascript后置摄像头

[英]Javascript Rear Camera

Hi there I'm trying to use this demo https://kdzwinel.github.io/JS-OCR-demo/ 嗨,我正在尝试使用此演示https://kdzwinel.github.io/JS-OCR-demo/

But I'm getting only the phone's front camera, it uses this code to enable the camera 但是我只得到手机的前置摄像头,它使用此代码启用摄像头

             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();
}

And I've tried to add the code to select the camera from https://simpl.info/getusermedia/sources/ 而且我尝试添加代码以从https://simpl.info/getusermedia/sources/选择摄像机

But without any success :( how can I get to use the rear camera in the first example without too much hassle, thanks to all 但是没有成功:(在所有的例子中,我如何在没有太多麻烦的情况下使用后置摄像头

The following does the trick in both Firefox and Chrome 以下在Fi​​refox和Chrome中都可以解决问题

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

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

Demo here : https://js-1lq5ue.stackblitz.io/ 演示在这里: https : //js-1lq5ue.stackblitz.io/

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

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