SoFunction
Updated on 2025-04-07

How to call the device camera by js

This article has shared the specific code of js calling device camera for your reference. The specific content is as follows

Use getUserMedia API to get the permissions of the camera
Compatible with Chrome and Firefox, IOS is not compatible

The following is the source code:

<!DOCTYPE html>
<html lang="zh">
 <head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Document</title>
 </head>
 <body>
  <video src=""></video>
  <script type="text/javascript">
   var opt = {
    audio: true,
    video: {
     width: 375,
     height: 603
    }
   };
   (opt)
    .then(function(mediaStream) {
     var video = ('video');
      = mediaStream;
      = function(e) {
      ();
     };
    })
    .catch(function(err) {
     ( + ": " + );
    }); // always check for errors at the end.
  </script>
 </body>
</html>

Note that if you use Chrome to view the code to take effect under the https protocol, it is recommended to use Firefox to view it.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.