【问题标题】:How to get images from client web cam如何从客户端网络摄像头获取图像
【发布时间】:2014-01-19 06:44:32
【问题描述】:

我希望创建一个 ASP.Net 网站,用户可以在其中从网络摄像头捕获图像并将其保存到我服务器上的数据库中。我曾尝试使用 TWAIN,但似乎找不到任何支持此功能的新相机。尝试使用 Silverlight 和 WIA 时也是如此。有没有人在这方面取得任何成功?

客户端计算机将配备我推荐的任何网络摄像头,因此如果您知道可行的型号,请分享。我已经尝试过 Microsoft LifeCam 和 Logitech,但都没有成功。

如果你已经这样做了,或者现在我真的很感激一些帮助。谢谢你的时间。

【问题讨论】:

    标签: c# asp.net twain


    【解决方案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/

    还有库本身:

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

    希望它对你有用!

    【讨论】:

    • 这太棒了,这正是我正在寻找的,但是......我需要支持 Internet Explorer。你知道有什么方法可以诱使 Internet Explorer 排除这种情况吗?
    • 嗯...在这种情况下,我会推荐 Silverlight;这是一篇关于如何做到这一点的好帖子:codeproject.com/Articles/81582/Silverlight-4-Webcam-Support
    【解决方案2】:

    asp.net 是一种服务器端技术,无法连接到客户端网络摄像头。您的浏览器高度沙盒化,不太可能允许访问用户的网络摄像头。

    考虑构建一个 Flash 或 Silverlight 组件来访问网络摄像头。

    对此的例外是在许多移动平台上,浏览器可以通过<input type="file" /> 标签访问相机和图片存储。这在 Android 上已经有一段时间了,现在是 Safari v6 的一个选项。我不知道有哪些桌面浏览器允许直接访问附加的网络摄像头。

    奖励解决方法是让用户拍照,然后通过文件上传访问它们。

    【讨论】:

      【解决方案3】:

      我可以通过让用户在计算机上安装 Google Chrome Frame 然后使用 canvas 标签来获取图像来完成我想要的。运行良好,这里有一些示例代码:

          <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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-09
        • 1970-01-01
        相关资源
        最近更新 更多