溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

網絡攝像頭怎么利用JavaScript進行調用

發(fā)布時間:2020-12-07 15:11:05 來源:億速云 閱讀:128 作者:Leah 欄目:開發(fā)技術

本篇文章給大家分享的是有關網絡攝像頭怎么利用JavaScript進行調用,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

不支持IE瀏覽器(需要使用flash插件), 支持移動端, 未經過完全測試

PC端使用的時候, HTML頁面需要預留video標簽, canvas標簽

移動端使用的時候, HTML頁面需要預留file標簽, canvas標簽, img標簽

(function (window, document) {
  window.camera = {
    init: function (options) {
      /**
       * options 屬性示例
       * videoID: video控件ID
       * canvasID: canvas控件ID
       * fileID: type為file的input控件的ID
       * imageID: img控件的ID
       * videoEnable: 是否啟用攝像頭
       * audioEnable: 是否啟用麥克風
       * videoWidth: 視頻寬度
       * videoHeight: 視頻高度
       * photoWidth: 拍照寬度
       * photoHeight: 拍照高度
       */

      _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 控件無效');
      return;
    }

    let canvasDom = document.getElementById(_options.canvasID);
    if (!canvasDom) {
      alert('Canvas 控件無效');
      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 控件無效');
      return;
    }


    let canvasDom = document.getElementById(_options.canvasID);
    if (!canvasDom) {
      alert('Canvas 控件無效');
      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 控件無效');
      return;
    }

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

    let canvasDom = document.getElementById(_options.canvasID);
    if (!canvasDom) {
      alert('Canvas 控件無效');
      return;
    }

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

    let imageDom = document.getElementById(_options.imageID);
    if (!imageDom) {
      alert('Image 控件無效');
      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);

以上就是網絡攝像頭怎么利用JavaScript進行調用,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI