【问题标题】:MVC Capture webcam image, save file path of image to the databaseMVC捕获网络摄像头图像,将图像的文件路径保存到数据库
【发布时间】:2016-11-02 15:19:08
【问题描述】:

这是我想要实现的要点:

  • 从“编辑”视图捕获网络摄像头图像。 (工作)
  • 为图像分配文件名和路径。 (工作)
  • 将图像保存到图像文件夹。 (工作)
  • 将图像的路径存储在数据库中。 (不工作)

这是我的 Capture 控制器:

      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 传递给控制器​​以绑定到模型。

感谢您的帮助。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 webcam-capture


    【解决方案1】:

    它的工作!好吧,我绕道而行,但仍然很高兴听到建议。

    这是要点:

    • 当我单击捕获按钮时,我运行该功能:

      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
      }
      
    • 这是捕获功能(相当标准 - 有人可以告诉我是否需要为此准备好文档吗?)

      $(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 作为字符串(已包含 String to Bytes 函数以便于参考):

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

    现在单击捕获后。可以像提交强类型表单一样提交表单。

    此方法存在潜在问题

    • 这种方式只允许存储一张图片。
    • 我以一种非常奇怪的方式存储文件路径。

    我非常愿意接受建议。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-17
      • 2013-06-03
      • 2014-08-23
      • 2013-02-09
      • 2010-11-23
      • 1970-01-01
      相关资源
      最近更新 更多