簡體   English   中英

從HTML5網絡攝像頭獲取圖像的src

[英]Get the src of an image from HTML5 webcam

我有一個應用程序,用戶可以在其中單擊網絡攝像頭中的圖片並將其保存到數據庫中。 我已經從WebRTC添加了插件。

我的代碼如下:

<form action="process.php" method="post">
  <div class="camera">
    <video id="video">Video stream not available.</video>
    <button id="startbutton">Take photo</button>
  </div>
  <canvas id="canvas">
  </canvas>
  <div class="output">
    <img id="photo" alt="The screen capture will appear in this box.">
  </div>
  <script>
  var myImg = document.getElementById("photo").src;
  </script>
  <input type="submit" onclick="alert(myImg)">
  </form>

但是,當我單擊“提交”時,要檢查其通過的內容,它會發出空警報! 為什么不顯示src

:我正在幫助來自這個

首先定義一個默認的src。 然后用您拍攝的照片替換該src:

 <script> var myImg = document.getElementById("photo").src; </script> 

 <div class="output"> <img id="photo" src="" alt="The screen capture will appear in this box."> </div> 

htmljavascript有兩個問題

  1. 單擊input type="submit"時, img src仍設置為占位符圖像,刷新form提交window

  2. 在將img元素src設置為photo.setAttribute('src', data); data URI之前,提交form photo.setAttribute('src', data); takepicture

嘗試設置input type="submit" disabled屬性,以防止在takepicture之前提交form click事件附加到#startbutton #startbutton click處理程序安裝onload事件#photo ,應該alert data URI<img> src設定為后data URI的表示canvas從設置圖像videoonload處理程序; input type="submit" disabled屬性設置為false以允許提交form

還請注意,如果不使用https:協議,則可能會發生錯誤。

 getUserMedia() no longer works on insecure origins. 
 To use this feature, you should consider switching your 
 application to a secure origin, such as HTTPS. 
 See [url] for more details.

<form>
  <div class="camera">
    <video id="video">Video stream not available.</video>
    <button id="startbutton">Take photo</button> 
  </div>
  <canvas id="canvas">
  </canvas>
  <div class="output">
    <img id="photo" alt="The screen capture will appear in this box."> 
  </div>
    <script>
     document.getElementById("startbutton").addEventListener("click", function() {
       document.getElementById("photo").onload = function() {
           console.log(this.src);
           alert(this.src);
           document.querySelector("input[type=submit]").disabled = false;
       };          
     })
  </script>
  <input type="submit" disabled>
</form>

plnkr https://plnkr.co/edit/qzvEdtCnQnzXceIpDumA

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM