2012-12-27 133 views
0

我期待創建一個ASP.Net網站,用戶可以從網絡攝像頭捕獲圖像並將其保存到我的服務器上的數據庫。我曾嘗試使用TWAIN,但似乎無法找到任何支持此功能的新相機。嘗試使用Silverlight和WIA時也是如此。這樣做有沒有人取得過成功?如何從客戶端網絡攝像頭獲取圖像

客戶端計算機將擁有我推薦的任何網絡攝像頭,因此如果您知道可以使用的模型,請分享。我已經嘗試了微軟LifeCam和羅技沒有運氣。

如果你已經完成了這個或現在我將非常感謝一些幫助。謝謝你的時間。

回答

0

我能夠通過讓用戶在那臺計算機上安裝Google Chrome Frame然後使用畫布標籤來獲取圖像,從而完成了我想要的操作。很好地工作在這裏的一些示例代碼:

<div> 

     <p id="status" style="color:red"> 
      <noscript>JavaScript is disabled.</noscript> 
     </p> 


     <table> 
      <tr> 
       <td> 
        <input id="btnTakePicture" type="button" value="Take Picture"; /> 
       </td> 
       <td> 
        <input id="btnSave" type="button" value="Save Picture"; /> 
       </td> 
      </tr> 
     </table> 


     <asp:Button ID="btnSavePicture" runat="server" Text="HiddenSave" OnClick="btnSavePicture_Click" CssClass="hiddencol" /> 

     <asp:Panel ID="pnlHide" runat="server" style="display:none" >  
      <textarea id="eventdata" rows="18" cols="1" style="width: 100%" runat="server"></textarea> 
     </asp:Panel> 

     <script type="text/javascript"> 

       navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || 
       navigator.mozGetUserMedia || navigator.msGetUserMedia; 

       var webcam = (function() { 


        var video = document.createElement('video'), 
        photo = document.createElement('canvas'); 

        function play() { 

         if (navigator.getUserMedia) { 

          navigator.getUserMedia({ video: true, audio: true, toString: function() { return "video,audio"; } }, onSuccess, onError); 

         } else { 

          changeStatus('getUserMedia is not supported in this browser.', true); 
         } 

        } 

        function onSuccess(stream) { 

         var source; 

         if (window.webkitURL) { 

          source = window.webkitURL.createObjectURL(stream); 

         } else { 

          source = stream; // Opera and Firefox 
         } 


         video.autoplay = true; 

         video.src = source; 

         changeStatus('Connected.', false); 

        } 

        function onError() { 

         changeStatus('Please accept the getUserMedia permissions! Refresh to try again.', true); 

        } 

        function changeStatus(msg, error) { 
         var status = document.getElementById('status'); 
         status.innerHTML = msg; 
         status.style.color = (error) ? 'red' : 'green'; 
        } 


        // allow the user to take a screenshot 
        function setupPhotoBooth() { 
         //var takeButton = document.createElement('button'); 
         var takeButton = document.getElementById('btnTakePicture'); 
         //takeButton.innerText = 'Take Picture'; 
         takeButton.addEventListener('click', takePhoto, true); 
         //document.body.appendChild(takeButton); 

         //var saveButton = document.createElement('button'); 
         var saveButton = document.getElementById('btnSave'); 
         //saveButton.id = 'btnSave'; 
         //saveButton.innerText = 'Save Picture'; 
         saveButton.disabled = true; 
         saveButton.addEventListener('click', savePhoto, true); 
         //document.body.appendChild(saveButton); 

        } 

        function takePhoto() { 

         // set our canvas to the same size as our video 
         photo.width = video.width; 
         photo.height = video.height; 

         var context = photo.getContext('2d'); 
         context.drawImage(video, 0, 0, photo.width, photo.height); 

         // allow us to save 
         var saveButton = document.getElementById('btnSave'); 
         saveButton.disabled = false; 

         var data = photo.toDataURL("image/png"); 

         data = data.replace("image/png", ""); 

         document.getElementById("<%= eventdata.ClientID %>").value = data; 

        } 

        function savePhoto() { 
         //      var data = photo.toDataURL("image/png"); 

         //      data = data.replace("image/png", "image/octet-stream"); 

         //      document.getElementById("<%= eventdata.ClientID %>").value = data; 
         //      document.location.href = data; 

         SendBack(); 
        } 

        return { 
         init: function() { 

          changeStatus('Please accept the permissions dialog.', true); 

          video.width = 320; 
          video.height = 240; 

          document.body.appendChild(video); 
          document.body.appendChild(photo); 

          navigator.getUserMedia || (navigator.getUserMedia = navigator.mozGetUserMedia || navigator.webkitGetUserMedia || navigator.msGetUserMedia); 

          play(); 
          setupPhotoBooth(); 

         }() 

        } 


       })(); 

      function SendBack() { 
       var btn = document.getElementById("<%= btnSavePicture.ClientID %>"); 
       btn.click(); 
      } 

    </script> 

    </div> 
0

asp.net是一種服務器端技術,無法連接到客戶端網絡攝像頭。您的瀏覽器是高度防砂的,不太可能允許訪問用戶的網絡攝像頭。

考慮構建一個Flash或Silverlight組件來訪問攝像頭。

此例外是,在許多移動平臺上,瀏覽器可以通過<input type="file" />標籤訪問攝像頭和圖片存儲區。這在Android上已經有一段時間了,現在是Safari v6的一個選項。我不知道任何允許直接訪問連接的網絡攝像頭的桌面瀏覽器。

一個獎項的解決方法是讓用戶拍照,然後通過文件上傳訪問它們。

1

如果您針對的是較新的瀏覽器(支持WebRTC getUserMedia方法的瀏覽器),那麼Photobooth.js可能適合您。

引用自WebMonkey:'最引人注目的WebRTC熱點是開發人員Wolfram Hempel的Photobooth.js,一個用於與設備相機一起工作的JavaScript庫。

http://www.webmonkey.com/2012/12/add-an-html5-webcam-to-your-site-with-photobooth-js/

而且LIB本身:

http://wolframhempel.com/2012/12/02/photobooth-js/

希望工程爲您服務!

+0

這是真棒,正是我所期待的,但是...我需要支持Internet Explorer。你知道一種欺騙Internet Explorer的方法嗎? – user229133

+0

嗯......在這種情況下,我會推薦Silverlight;這裏有一篇關於如何做的好貼子:http://www.codeproject.com/Articles/81582/Silverlight-4-Webcam-Support – OnoSendai

相關問題