c# - 带有 asp.net mvc 3 的 jquery 网络摄像头插件

标签 c# silverlight flash asp.net-mvc-3 webcam

有没有人得到这个http://www.xarg.org/project/jquery-webcam-plugin/ , 与 aps.net mvc 3 一起工作?我似乎无法使用 WebImage 类或 BitmapImage 解码图像。

我已经厌倦了使用 Silverlight 执行此操作,但我真的不确定如何上传图像。我不需要保存图像,我只想处理它,我真正想做的是通过网络应用程序读取条形码。

我似乎找不到将图像从 Silverlight 或 Flash 上传到我的 MVC 应用程序的好指南。

提前致谢。

最佳答案

documentation包含很多例子。所需要的只是阅读和尝试。

因此,您的 Index.cshtml View 可能看起来像使用浏览器的 HTML5 canvas 元素将来自网络摄像头的原始图像数据编码为 PNG 图像,该图像将使用AJAX 请求:

<script src="@Url.Content("~/Scripts/jquery.webcam.js")" type="text/javascript"></script>

<div id="webcam"></div>
<a href="#" id="upload">Capture image and send for processing</a>

<script type="text/javascript">
    var pos = 0, ctx = null, saveCB, image = [];
    var canvas = document.createElement('canvas');
    canvas.setAttribute('width', 320);
    canvas.setAttribute('height', 240);
    ctx = canvas.getContext('2d');
    image = ctx.getImageData(0, 0, 320, 240);

    var saveCB = function (data) {
        var col = data.split(';');
        var img = image;
        for (var i = 0; i < 320; i++) {
            var tmp = parseInt(col[i]);
            img.data[pos + 0] = (tmp >> 16) & 0xff;
            img.data[pos + 1] = (tmp >> 8) & 0xff;
            img.data[pos + 2] = tmp & 0xff;
            img.data[pos + 3] = 0xff;
            pos += 4;
        }

        if (pos >= 4 * 320 * 240) {
            ctx.putImageData(img, 0, 0);
            $.post('@Url.Action("Upload")', { type: 'data', image: canvas.toDataURL("image/png") }, function (result) {
                if (result.success) {
                    alert('The image was successfully sent to the server for processing');
                }
            });
            pos = 0;
        }
    };

    $('#webcam').webcam({
        width: 320,
        height: 240,
        mode: 'callback',
        swffile: '@Url.Content("~/scripts/jscam_canvas_only.swf")',
        onSave: saveCB,
        onCapture: function () {
            webcam.save();
        }
    });

    $('#upload').click(function () {
        webcam.capture();
        return false;
    });
</script>

和您的家庭 Controller :

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Upload(string image)
    {
        image = image.Substring("data:image/png;base64,".Length);
        var buffer = Convert.FromBase64String(image);
        // TODO: I am saving the image on the hard disk but
        // you could do whatever processing you want with it
        System.IO.File.WriteAllBytes(Server.MapPath("~/app_data/capture.png"), buffer);
        return Json(new { success = true });
    }
}

关于c# - 带有 asp.net mvc 3 的 jquery 网络摄像头插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9447992/

相关文章:

c# - 创建通用类,它将映射到任何类,而不知道类在 C# 中有多少成员

c# - 如何在 ASP.NET CORE 中签署 XML SAML 断言

c# - WP7,我能以某种方式在开始菜单中的应用程序磁贴上捕获触摸事件吗?

c# - 如何在 Silverlight 中为 zIndex 属性设置动画?

actionscript-3 - ActionScript 错误 1151 : randomly selected obstacle

javascript - JavaScript 和 C# 中的 12 小时时间格式比较

c# - 无法从 'AnonymousType#1' 转换为 'int'

c# - Silverlight 使用当前的 .NET 项目

actionscript-3 - ExternalInterface 是否适用于文件 : protocol?

javascript - 动态调整 Flash 对象的大小以填充窗口