6

MVC4 ページで jquery Webcam プラグインを使用しています。プラグインはhttp://www.xarg.org/project/jquery-webcam-plugin/にあります。

画像をキャプチャした後、プラグインで保存メソッドを使用していますが、コントローラー アクションに投稿されません。

これはcshtmlページです:

@{
    ViewBag.Title = "Index";
}

<!DOCTYPE html>
<html lang="es">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>@ViewBag.Title - Prueba WebCam</title>
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />
    @Styles.Render("~/styles/base")
    @Scripts.Render("~/scripts/jquery", "~/scripts/jqueryui", "~/scripts/webcam")

    <script type="text/javascript">
        $(function () {
            $("#camera").webcam({
                width: 320,
                height: 240,
                mode: "save",
                swffile: "@Url.Content("~/Scripts/WebCam/jscam_canvas_only.swf")",
                onTick: function () { },
                onSave: function () { alert('Almacenamiento realizado') },
                onCapture: function () { webcam.save("@Url.Action("Save")"); alert('Captura realizada'); },
                debug: function () { },
                onLoad: function () { }
            });
        });

        function CaptureAndSave() {
            webcam.capture();
        }
    </script>
</head>
<body class="home desytec">
    <header>
    </header>
    <!-- MAIN -->
    <div id="main">
        <!-- wrapper-main -->
        <div class="wrapper">
            <!-- headline -->
            <div class="clear"></div>
            <div id="headline">
                <span class="main"></span>
                <span class="sub"></span>
            </div>
            <!-- ENDS headline -->

            <!-- content -->
            <div id="content">
                <div id="camera"></div>
                <br /><br /><br />
                <input type="button" onclick="CaptureAndSave();" value="Capturar" />
            </div>
            <!-- ENDS content -->
        </div>
        <!-- ENDS wrapper-main -->
    </div>
    <!-- ENDS MAIN -->
    <footer>
    </footer>
</body>
</html>

そして、これはコントローラーです:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Capture.Controllers
{
    public class CaptureController : Controller
    {
        //
        // GET: /Capture/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Save(HttpPostedFileBase file)
        {
            try
            {
                if (file != null)
                {
                    string pic = System.IO.Path.GetFileName(file.FileName);
                    string path = System.IO.Path.Combine(Server.MapPath("~/Captures"), pic);
                    file.SaveAs(path);
                    return Json(true, JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {

            }
            return Json(true, JsonRequestBehavior.AllowGet);
        }
    }
}

コントローラーの save メソッドは呼び出されず、実際、firebug を使用すると、POST は実行されません。

ところで。キャンバス (DIV id = camera) でカメラを見ることができるので、カメラは機能します。

そして、キャプチャ ボタンを押した後に OnCapture コールバックが呼び出されます。

これについて何か助けてください。

ありがとうハイメ

4

1 に答える 1

1

モードのみが含まれているSaveため、アクションは呼び出されません。完全な API (つまりモード) については、 をダウンロードして使用する必要があります。jscam_canvas_only.swf"callback""save"jscam.swf

したがって、webcamセットアップを次のように変更します。

$("#camera").webcam({
    //...
    swffile: "@Url.Content("~/Scripts/WebCam/jscam.swf")",
    //...
});

Saveこれでアクションが呼び出されますが、パラメーターfileは常に null になります。これjscam.swfは、要求本文で画像データを 16 進数文字列として送信するためです。

デフォルトのモデル バインディング インフラストラクチャはこれを処理しないため、追加のコードを記述する必要があります。

if (Request.InputStream.Length > 0)
{
    string pic = System.IO.Path.GetFileName("capture.jpg");
    string path = System.IO.Path.Combine(Server.MapPath("~/Captures"), pic);
    using (var reader = new StreamReader(Request.InputStream))
    {
        System.IO.File.WriteAllBytes(path, StringToByteArray(reader.ReadToEnd()));
    }
    return Json(true, JsonRequestBehavior.AllowGet);
}

パラメータを削除してfileから生データにアクセスする必要がありますが、これは 16 進文字列であるため、保存する前にRequest.InputStreamに変換する必要があります。byte[]

.NET に組み込まれているデフォルトの変換はありませんが、SO には優れたソリューションがたくさんあります。

バイト配列を 16 進文字列に、またはその逆に変換するにはどうすればよいですか?

私のサンプルでは、​​この方法を使用しました:

public static byte[] StringToByteArray(String hex)
{
  int NumberChars = hex.Length/2;
  byte[] bytes = new byte[NumberChars];
  using (var sr = new StringReader(hex))
  {
    for (int i = 0; i < NumberChars; i++)
      bytes[i] = 
        Convert.ToByte(new string(new char[2]{(char)sr.Read(), (char)sr.Read()}), 16);
  }
  return bytes;
}
于 2013-12-12T21:22:38.857 に答える