web網頁調用攝像頭拍照

来源:https://www.cnblogs.com/jjlinsblog/archive/2019/04/28/10784154.html
-Advertisement-
Play Games

頁面內容 ...


頁面內容

<video id="video" class="video-bg"></video>
<canvas id="qrCanvas" class="qr-canvas"></canvas>
<div id="imageWarp" class="image-warp"></div>
<button type="button" onclick="window.MyCamera.openMedia()">開啟</button>
<button type="button" onclick="window.MyCamera.closeMedia()">關閉</button>
<button type="button" onclick="window.MyCamera.drawMediaAndToImg()">拍攝</button>

js內容
var CameraInit=(function(window,document,undefined){
    function MyCamera(videoDom,canvasDom) {
        this.mediaOpts = {
            audio: false,
            video: true,
            video: { facingMode: "environment"} // 或者 "user"
            // video: { width: 1280, height: 720 }
            // video: { facingMode: { exact: "environment" } }// 或者 "user"
        }
        this.video=videoDom;
        this.canvas1=canvasDom;
        this.context1 = this.canvas1.getContext('2d');
        this.mediaStreamTrack = null;
        this.checkEnvironment();
    }
    MyCamera.prototype = {
        checkEnvironment: function() {
            window.URL = (window.URL || window.webkitURL || window.mozURL || window.msURL);
            if (navigator.mediaDevices === undefined) {
                navigator.mediaDevices = {};
            }
            if (navigator.mediaDevices.getUserMedia === undefined) {
                navigator.mediaDevices.getUserMedia = function(constraints) {
                    var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
                    if (!getUserMedia) {
                        return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
                    }
                    return new Promise(function(resolve, reject) {
                        getUserMedia.call(navigator, constraints, resolve, reject);
                    });
                }
            }
        },
        // 回調
        successFunc: function(stream) {
            this.mediaStreamTrack = stream;
            this.video = document.querySelector('video');
            if ("srcObject" in this.video) {
                this.video.srcObject = stream
            } else {
                this.video.src = window.URL && window.URL.createObjectURL(stream) || stream
            }
            this.video.play();
        },
        errorFunc:function(err) {
            alert(err.name);
        },

        // 正式啟動攝像頭
        openMedia: function(){
            navigator.mediaDevices.getUserMedia(this.mediaOpts).then(this.successFunc.bind(this)).catch(this.errorFunc.bind(this));
        },

        //關閉攝像頭
        closeMedia: function(){
            var that=this;
            that.mediaStreamTrack.getVideoTracks().forEach(function (track) {
                track.stop();
                that.context1.clearRect(0, 0,that.context1.width, that.context1.height);//清除畫布
            });
        },

        //截取視頻並轉為圖片
        drawMediaAndToImg: function(){
            this.canvas1.setAttribute("width", this.video.videoWidth);
            this.canvas1.setAttribute("height", this.video.videoHeight);
            this.context1.drawImage(this.video, 0, 0, this.video.videoWidth, this.video.videoHeight);//顯示在canvas中
            var image = new Image();
            image.src = this.canvas1.toDataURL('image/png')
            var imageWarp = document.getElementById("imageWarp");
            if(imageWarp.hasChildNodes()) {
                document.getElementById("imageWarp").replaceChild(image, imageWarp.firstChild);
            }else{
                document.getElementById("imageWarp").appendChild(image);
            }
        },
    };
    return MyCamera;
})(window,document)

 



實例化
window.MyCamera=new CameraInit(document.getElementById("video"), document.getElementById("qrCanvas"));

 



參考內容:https://www.jb51.net/article/160015.htm

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 因為IT互聯網發展的非常迅速,而web前端這塊很火,目前工資水平給的很高,在市場上也是非常的稀缺人才,現在各個行業轉行做web前端的很多,今天給大家一些建議,希望新手少走點彎路吧! 建議一:有一個比較適合自己系統的學習方案,系統的學習教程,很多人在開始學習web前端的時候都不知道如何規劃,也不知道w ...
  • Node.js安裝和簡單使用 1. 安裝方法 簡單的安裝方式是直接官網下載,然後本地安裝即可。官網地址:nodejs.org Windows系統下,選擇和系統版本匹配的.msi尾碼的安裝文件。Mac OS X系統下,選擇.pkg尾碼的安裝文件。 2. 測試是否安裝成功 打開終端,鍵入命令 ,如果進入 ...
  • FullCalendar Timeline View(v4) The Scheduler add-on provides a new view called “timeline view” with a customizable horizontal time-axis and resources ...
  • canvas實現畫板 主要用到知識點: 滑鼠事件onmousedown() onmousemove() onmouseup() onmouseleave() 事件委托 canvas的一些方法 如:繪製線條moveTo() lineTo() stroke() 撤銷功能getImageData() pu ...
  • 微信分享時 分享鏈接攜帶參數可能不是固定的 需要在分享的前一刻才知道 這裡就是動態設置分享鏈接的基本寫法 代碼不是那麼詳盡 但大致流程如下 1.安裝引用jssdk 2.通過config介面註入配置信息 ...
  • 在學習node的過程中,對於流的概念一直不是很理解,通過查閱一些資料,現在將自己對流的一些理解進行總結一下。 一.流的理解 首先我們必須知道什麼是流,很多書中只是提到使用流讀寫文件怎麼怎麼方便,卻不提流到底是什麼?在說流之前我們先說一下 流數據 : 流數據:就是位元組數據, 在應用程式中各種對象之間交 ...
  • concat >>連接2個或更多數組,並返回結果 every >>對數組中的每一項運行給定函數,如果該函數對每一項都返回true,則返回true filter >>對數組中的每一項運行給定函數,返回該函數會返回true的項組成新的數組 forEach >>對數組中的每一項運行給定函數,這個方法沒有返 ...
  • 插槽分為預設插槽和具名插槽: 預設插槽: 具名插槽: 註意:具名插槽需要包裹在 template 標簽中,否則會報錯 子組件向父組件傳參: 結果: ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...