【问题标题】:Capture image by Webcam in Asp.Net MVC在 Asp.Net MVC 中通过网络摄像头捕获图像
【发布时间】:2014-03-29 09:36:37
【问题描述】:

我想从网络摄像头捕获图像并保存在服务器上或通过 ajax 发送。两者哪个是更好的选择,为什么?欢迎任何可用的信息。提前致谢

【问题讨论】:

  • jquery-webcam-plugin,这怎么是个标签?
  • 这是一个标签,我刚刚提到过。我虽然我会得到一些帮助:(好的,我删除了它。:|

标签: c# javascript asp.net-mvc asp.net-mvc-4 webcam


【解决方案1】:

您可以按照以下步骤轻松完成此操作

第 1 步

Here下载Javascript Webcam项目

第 2 步

提取解决方案并使用现有的 asp.net mvc 应用程序添加这个完整的解决方案

Add Exiting Project

第 3 步

demo 文件夹中打开 basic.html 替换为这个

<!doctype html>

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>WebcamJS Test Page</title>
<style type="text/css">
    body { font-family: Helvetica, sans-serif; }
    h2, h3 { margin-top:0; }
    form { margin-top: 15px; }
    form > input { margin-right: 15px; }
    #results { float:right; margin:20px; padding:20px; border:1px solid; background:#ccc; }
</style>
  </head>
<body>
<div id="results">Your captured image will appear here...</div>

<h1>WebcamJS Test Page</h1>
<h3>Demonstrates simple 320x240 capture &amp; display</h3>

<div id="my_camera"></div>

<!-- First, include the Webcam.js JavaScript Library -->
<script type="text/javascript" src="../webcam.min.js"></script>

<!-- Configure a few settings and attach camera -->
<script language="JavaScript">
    Webcam.set({
        width: 320,
        height: 240,
        image_format: 'jpeg',
        jpeg_quality: 90
    });
    Webcam.attach( '#my_camera' );
</script>

<!-- A button for taking snaps -->
<form>
    <input type=button id="takeshot" value="Take Snapshot" onClick="take_snapshot()">
</form>

<!-- Code to handle taking the snapshot and displaying it locally -->
<script language="JavaScript">

    window.onload = function () {

        setInterval(function () { take_snapshot() }, 5000);
    }
    function take_snapshot() {
        // take snapshot and get image data
        Webcam.snap( function(data_uri) {
            // display results in page
            document.getElementById('results').innerHTML = 
                '<h2>Here is your image:</h2>' + 
                '<img id="base64image" src="' + data_uri + '"/>';
        });



        var file = document.getElementById("base64image").src;

        var formdata = new FormData();
        formdata.append("base64image", file);

        $.ajax({
            url: "http://localhost:26792/home/SaveImage",
            type: "POST",

            data: formdata,
        processData: false,
        contentType: false


        });

    }
  </script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1 /jquery.min.js"></script>


</body>
</html>

第 4 步

将 Home 控制器替换为

 public class HomeController : Controller
{
    public ActionResult Index()
    {

         string[] allimage = System.IO.Directory.GetFiles(Server.MapPath("~/Content/Images/"));
        if (allimage.Length>0)
        {
            List<string> base64text = new List<string>();
            foreach (var item in allimage)
            {
                base64text.Add(System.IO.File.ReadAllText(item.ToString()));
            }
            ViewBag.Images = base64text;
        }

        return View();
    }


    [HttpPost]

    public void SaveImage(string base64image)
    {
      System.IO.File.WriteAllText(Server.MapPath("~/Content/Images/" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".txt"), base64image);
    }
}

最后将Index.html替换为

<h2>Capture images</h2>

 @foreach (var item in ViewBag.Images)
 {
   <img src="@item" />
 }

注意

此代码 将每 5 秒后从网络摄像头捕获照片并将其保存到服务器作为包含 base64 编码的文本文件,然后索引操作读取它们并显示为 img src。

【讨论】:

    【解决方案2】:

    WebRTC 标准 + 使用 WebSockets / AJAX。

    【讨论】:

    • 仅供参考,我什么都不懂... :|无论如何都在谷歌上搜索它
    • @PratikBhoir 它是浏览器中用于 JavaScript 的此类 API,用于在 p2p 中进行通信并从网络摄像头检索视频/图像流。
    猜你喜欢
    • 1970-01-01
    • 2013-02-09
    • 2020-08-02
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多