MVC Capture网络摄像头图像,将图像文件路径保存到数据库
- 从编辑"视图捕获网络摄像头图像. (工作中)
- 为图像分配文件名和路径. (工作中)
- 将图像保存到图像文件夹. (工作中)
- 将图像的路径存储在数据库中. (不起作用)
这是我的Capture控制器:
Here is my Capture controller:
public void Capture(String FileLocation)
{
//var FileLocation = Server.MapPath("~/Images/test.jpg");
var stream = Request.InputStream;
string dump;
using (var reader = new StreamReader(stream))
dump = reader.ReadToEnd();
if (System.IO.File.Exists(FileLocation))
{
System.IO.File.Delete(FileLocation);
System.IO.File.WriteAllBytes(FileLocation, String_To_Bytes2(dump));
}
else System.IO.File.WriteAllBytes(FileLocation, String_To_Bytes2(dump));
return;
}
这是编辑"控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(TrappingEvent trappingevent)
{
if (ModelState.IsValid)
{
db.Entry(trappingevent).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.PersonId = new SelectList(db.People, "Id", "First_Name", trappingevent.PersonId);
return View(trappingevent);
}
根据我的有限理解,最好将文件路径作为变量从Capture void传递到控制器以绑定到模型.
From my limited understanding it would be nice to pass the file path as a variable from the Capture void to the controller for binding to the model.
感谢您的帮助.
有效!好吧,我以回旋的方式做到了,但仍然很高兴听到建议.
Its working! Well I did it in a roundabout way and still happy to hear suggestions.
要点:
-
当我单击捕获按钮时,我运行该功能:
when I click the capture button i run the function:
function CaptureAndAssignText() {
var Div = document.getElementById("ImagePath");//ImagePath is the field
// I want to bind to the model.
newText = "ID_" + @Model.Id + ".jpg";//Here I grab the Id of
//the current model to use as the file name
Div.value = newText;//Assign the file name to the input
//field ready for submission.
webcam.capture();//Run the capture function
}
这是Capture功能(非常标准-有人可以告诉我是否需要为此准备文件吗?)
Here is the Capture function(fairly standard - can someone tell me if I need document ready for this?)
$(document).ready(function () {
$("#Camera").webcam({
width: 320,
height: 240,
mode: "save",
swffile: "@Url.Content("~/Scripts/jscam.swf")",
onTick: function () { },
onSave: function () {
},
onCapture: function () {
webcam.save("@Url.Action("Capture", "TrapActivity", new { id = @Model.Id , pid = @Model.PersonId})");
},
debug: function () { },
onLoad: function () { }
});
});
这是我的控制器,经过修改后可以接受模型ID作为字符串(已包含字符串转字节"功能,以方便参考):
Here is my controller, modified to accept the model ID as a string (have included the String to Bytes function for easy reference):
public ActionResult Capture(string ID)
{
var path = Server.MapPath("~/Images/ID_" + ID + ".jpg" );
//var path1 = "~/Images/test.jpg;";
var stream = Request.InputStream;
string dump;
using (var reader = new StreamReader(stream))
dump = reader.ReadToEnd();
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
System.IO.File.WriteAllBytes(path, String_To_Bytes2(dump));
}
else System.IO.File.WriteAllBytes(path, String_To_Bytes2(dump));
return View(ID);
}
private byte[] String_To_Bytes2(string strInput)
{
int numBytes = (strInput.Length) / 2;
byte[] bytes = new byte[numBytes];
for (int x = 0; x < numBytes; ++x)
{
bytes[x] = Convert.ToByte(strInput.Substring(x * 2, 2), 16);
}
return bytes;
}
现在,一旦单击捕获即可.可以像提交强类型表单一样提交表单.
Now once capture has been clicked. The form can be submitted just as if you were submitting a strongly typed form.
此方法可能存在问题
- 这种方式只允许存储一张图像.
- 我以一种非常奇怪的方式存储文件路径.
我乐于接受建议.