Access camera from a browser

前端 未结 6 2004
悲&欢浪女
悲&欢浪女 2020-11-30 02:38

Is it possible to access the camera (built-in on Apples) from a browser?

Optimal solution would be client-side javascript. Looking to avoid using Java or Flash.

6条回答
  •  Happy的楠姐
    2020-11-30 03:05

    As of 2017, WebKit announces support for WebRTC on Safari

    Now you can access them with video and standard javascript WebRTC

    E.g.

    var video = document.createElement('video');
    video.setAttribute('playsinline', '');
    video.setAttribute('autoplay', '');
    video.setAttribute('muted', '');
    video.style.width = '200px';
    video.style.height = '200px';
    
    /* Setting up the constraint */
    var facingMode = "user"; // Can be 'user' or 'environment' to access back or front camera (NEAT!)
    var constraints = {
      audio: false,
      video: {
       facingMode: facingMode
      }
    };
    
    /* Stream it to video element */
    navigator.mediaDevices.getUserMedia(constraints).then(function success(stream) {
      video.srcObject = stream;
    });
    

    Have a play with it.

提交回复
热议问题