在Asp中使用摄像头进行图像采集.净MVC

本文关键字:图像 MVC Asp 摄像头 | 更新日期: 2023-09-27 17:54:59

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

在Asp中使用摄像头进行图像采集.净MVC

您可以通过以下步骤轻松做到这一点

1步

从这里下载Javascript摄像头项目

第二步

提取解决方案并使用

将此完整解决方案添加到现有的asp.net mvc应用程序中

添加当前项目

第三步

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>

第四步

替换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。

webbrtc标准+使用WebSockets/AJAX。