欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js調(diào)用網(wǎng)絡(luò)攝像頭的方法

 更新時(shí)間:2020年12月05日 17:19:19   作者:Joseph_Bee  
這篇文章主要介紹了js調(diào)用網(wǎng)絡(luò)攝像頭的方法,幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下

不支持IE瀏覽器(需要使用flash插件), 支持移動(dòng)端, 未經(jīng)過(guò)完全測(cè)試

PC端使用的時(shí)候, HTML頁(yè)面需要預(yù)留video標(biāo)簽, canvas標(biāo)簽

移動(dòng)端使用的時(shí)候, HTML頁(yè)面需要預(yù)留file標(biāo)簽, canvas標(biāo)簽, img標(biāo)簽

(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: 是否啟用麥克風(fēng)
       * 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 控件無(wú)效');
      return;
    }

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


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

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

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

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

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

以上就是js調(diào)用網(wǎng)絡(luò)攝像頭的方法的詳細(xì)內(nèi)容,更多關(guān)于js調(diào)用網(wǎng)絡(luò)攝像頭的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Javascript 計(jì)算字符串在localStorage中所占字節(jié)數(shù)

    Javascript 計(jì)算字符串在localStorage中所占字節(jié)數(shù)

    本文給大家分享的是使用Javascript 計(jì)算字符串在localStorage中所占字節(jié)數(shù),分別對(duì)UTF-8和UTF-16兩種編碼進(jìn)行了詳細(xì)說(shuō)明,有需要的小伙伴可以參考下。
    2015-10-10
  • js判斷一個(gè)對(duì)象是數(shù)組(函數(shù))的方法實(shí)例

    js判斷一個(gè)對(duì)象是數(shù)組(函數(shù))的方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于利用js如何判斷一個(gè)對(duì)象是數(shù)組(函數(shù))的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用JS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • 淺析JavaScript中命名空間namespace模式

    淺析JavaScript中命名空間namespace模式

    namespace即“命名空間”,也稱“名稱空間” 、”名字空間”。接下來(lái)通過(guò)本文給大家介紹JavaScript中命名空間namespace模式的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧
    2016-06-06
  • 微信小程序分包加載的實(shí)現(xiàn)代碼

    微信小程序分包加載的實(shí)現(xiàn)代碼

    分包加載是一種小程序優(yōu)化技術(shù),將小程序不同功能的代碼,分別打包成不同的子包,在構(gòu)建時(shí)打包成不同的分包,用戶在使用時(shí)按需進(jìn)行加載,在構(gòu)建小程序分包項(xiàng)目時(shí),構(gòu)建會(huì)輸出一個(gè)或多個(gè)分包,這篇文章主要介紹了微信小程序---分包加載,需要的朋友可以參考下
    2024-07-07
  • 微信小程序?qū)崿F(xiàn)觸底加載

    微信小程序?qū)崿F(xiàn)觸底加載

    這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)觸底加載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 最新評(píng)論