Method of calling webcam by js

  • 2021-10-13 06:12:50
  • OfStack

IE browser is not supported (flash plug-in is required), mobile terminal is supported, and it has not been fully tested

When the PC terminal is used, the video tag and the canvas tag need to be reserved on the HTML page

When the mobile terminal uses it, the file tag, canvas tag and img tag need to be reserved on the HTML page


(function (window, document) {
  window.camera = {
    init: function (options) {
      /**
       * options  Attribute sample 
       * videoID: video Control ID
       * canvasID: canvas Control ID
       * fileID: type For file Adj. input Control's ID
       * imageID: img Control's ID
       * videoEnable:  Do you want to enable the camera 
       * audioEnable:  Do you want to enable the microphone 
       * videoWidth:  Video width 
       * videoHeight:  Video height 
       * photoWidth:  Photographic width 
       * photoHeight:  Photographic height 
       */

      _options = options;
      if (isMobileTerminal()) {
        initMobileTerminal();
      } else {
        initComputerTerminal();
      }
    },
    photo: function () {
      if (isMobileTerminal()) {
        photoMobileTerminal();
      } else {
        photoComputerTerminal();
      }
    }
  };

  let _options = null;

  function initComputerTerminal() {
    let videoDom = document.getElementById(_options.videoID);
    if (!videoDom) {
      alert('Video  Invalid control ');
      return;
    }

    let canvasDom = document.getElementById(_options.canvasID);
    if (!canvasDom) {
      alert('Canvas  Invalid control ');
      return;
    }
    canvasDom.setAttribute('width', _options.photoWidth + 'px');
    canvasDom.setAttribute('height', _options.photoHeight + 'px');

    let parameters = {
      video: _options.videoEnable ? {
        width: _options.videoWidth,
        height: _options.videoHeight
      } : false,
      audio: _options.audioEnable
    };

    navigator.mediaDevices.getUserMedia(parameters)
      .then(function (MediaStream) {
        video.srcObject = MediaStream;
        video.play();
      }).catch(function (reason) {
        console.log(reason);
        alert(reason);
      });
  }

  function photoComputerTerminal() {
    let videoDom = document.getElementById(_options.videoID);
    if (!videoDom) {
      alert('Video  Invalid control ');
      return;
    }


    let canvasDom = document.getElementById(_options.canvasID);
    if (!canvasDom) {
      alert('Canvas  Invalid control ');
      return;
    }

    let context = canvasDom.getContext('2d');
    context.drawImage(videoDom, 0, 0, _options.photoWidth, _options.photoHeight);
  }

  function initMobileTerminal() {
    let fileDom = document.getElementById(_options.fileID);
    if (!fileDom) {
      alert('File  Invalid control ');
      return;
    }

    fileDom.setAttribute('accept', 'image/*');
    fileDom.setAttribute('capture', 'camera');

    let canvasDom = document.getElementById(_options.canvasID);
    if (!canvasDom) {
      alert('Canvas  Invalid control ');
      return;
    }

    canvasDom.setAttribute('width', _options.photoWidth + 'px');
    canvasDom.setAttribute('height', _options.photoHeight + 'px');

    let imageDom = document.getElementById(_options.imageID);
    if (!imageDom) {
      alert('Image  Invalid control ');
      return;
    }

    fileDom.addEventListener('change', function () {
      let file = fileDom.files[0];
      let reader = new FileReader();
      reader.onloadend = function () {
        imageDom.setAttribute('src', reader.result);

        setTimeout(function () {
          let context = canvas.getContext("2d");
          context.drawImage(imageDom, 0, 0, _options.photoWidth, _options.photoHeight);
        }, 300);
      };
      reader.readAsDataURL(file);
    });
  }

  function photoMobileTerminal() {
    let fileDom = document.getElementById(_options.fileID);
    fileDom.click();
  }

  function isMobileTerminal() {
    if (/AppleWebKit.*Mobile/i.test(navigator.userAgent) || /Mobile/.test(navigator.userAgent) || /MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))
      return /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent);
    return false;
  }
})(window, document);

The above is js call webcam method details, more about js call webcam information please pay attention to this site other related articles!


Related articles: