我想从网络摄像头捕获图像并保存在服务器上或通过ajax发送。从哪来两个更好的选择,为什么?欢迎提供任何可用信息。提前致谢
答案 0 :(得分:1)
您可以通过以下步骤轻松完成此操作
第1步
从Here
下载Javascript网络摄像头项目第2步
使用
提取解决方案并使用现有的asp.net mvc应用程序添加此完整解决方案第3步
从演示文件夹中打开 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 & 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步
用
替换家庭控制器 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。
答案 1 :(得分:-1)
WebRTC标准+使用WebSockets / AJAX。