从图像 C# MVC4 中读取二维码

reading a qr code from image C# MVC4

我有一个 Jquery 网络摄像头插件,它可以保存来自网络摄像头的图像,效果非常好,我可以在我的 C 盘中看到它。

现在我添加了 Emgu CV 库。当我按 "capture QR Code" 从保存的图像中解码 QR 码时,它不会将 QR 的 Url 显示到 Viewbag.Result,知道我做错了什么吗?下面是我的代码涉及网络摄像头和 Emgu CV

Webcam.cshtml

  @{
    ViewBag.Title = "Webcam";
}
@section scripts
{
    <script src="@Url.Content("~/Scripts/jquery.webcam.js")">
    </script>
    <script>
        $("#Camera").webcam({
             width: 400,
             height: 320,
             mode: "save",
             swffile: "@Url.Content("~/Scripts/jscam.swf")",
             onTick: function () { },
             onSave: function () { },
             onCapture: function () {
                 webcam.save("@Url.Content("~/QR/Capture")/");
             },
             debug: function () { },
             onLoad: function () { }
         });
     </script> 
}

<section id ="loginForm">
    <input type="button" value="Capture QR Code" onclick="webcam.capture();" />

<div id="Camera"></div>
<p>1. Take Picture of QR Code in front of the webcam</p>


<p>@ViewBag.Result </p>

</section>

二维码控制器

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Emgu.CV;
using ZXing;
using System.Drawing;



namespace JobTracker.Controllers
{
public class QRController : Controller
{
    //
    // GET: /QR/

    [Authorize]
    public ActionResult Webcam()
    {
        return View();
    }

    public void Capture()
    {
        var stream = Request.InputStream;
        string dump;

        using (var reader = new StreamReader(stream))
            dump = reader.ReadToEnd();

        var path = Server.MapPath("~/QR.jpg");
        System.IO.File.WriteAllBytes(path, String_To_Bytes2(dump));

        // create a barcode reader instance 
        IBarcodeReader reader1 = new BarcodeReader();
        reader1.Options.PossibleFormats = new BarcodeFormat[] { BarcodeFormat.QR_CODE };
        // load a bitmap
        var barcodeBitmap = (Bitmap)Bitmap.FromFile("D:\C# Web Application\JobTracker-Dev2\JobTracker\QR.jpg");
        // detect and decode the barcode inside the bitmap
        var result = reader1.Decode(barcodeBitmap);
        // do something with the result
        if (result != null)
        {
            ViewBag.Result = result.Text;
        }


    }

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


}
}

jquery.webcam.js

    (function ($) {

    var webcam = {

    "extern": null, // external select token to support jQuery dialogs
    "append": true, // append object instead of overwriting

    "width": 320,
    "height": 240,

    "mode": "callback", // callback | save | stream

    "swffile": "jscam.swf",
    "quality": 85,

    "debug":    function () {},
    "onCapture":    function () {},
    "onTick":   function () {},
    "onSave":   function () {},
    "onLoad":   function () {}
    };

    window["webcam"] = webcam;

    $["fn"]["webcam"] = function(options) {

    if (typeof options === "object") {
        for (var ndx in webcam) {
        if (options[ndx] !== undefined) {
            webcam[ndx] = options[ndx];
        }
        }
    }

    var source = '<object id="webcamobject" type="application/x-shockwave-flash" data="'+webcam["swffile"]+'" width="'+webcam["width"]+'" height="'+webcam["height"]+'"><param name="movie" value="'+webcam["swffile"]+'" /><param name="FlashVars" value="mode='+webcam["mode"]+'&amp;quality='+webcam["quality"]+'" /><param name="allowScriptAccess" value="always" /></object>';

    if (null !== webcam["extern"]) {
        $(webcam["extern"])[webcam["append"] ? "append" : "html"](source);
    } else {
        this[webcam["append"] ? "append" : "html"](source);
    }

    var run = 3;
    (_register = function() {
        var cam = document.getElementById('webcamobject');

        if (cam && cam["capture"] !== undefined) {

        /* Simple callback methods are not allowed :-/ */
        webcam["capture"] = function(x) {
            try {
            return cam["capture"](x);
            } catch(e) {}
        }
        webcam["save"] = function(x) {
            try {
            return cam["save"](x);
            } catch(e) {}
        }
        webcam["setCamera"] = function(x) {
            try {
            return cam["setCamera"](x);
            } catch(e) {}
        }
        webcam["getCameraList"] = function() {
            try {
            return cam["getCameraList"]();
            } catch(e) {}
        }
        webcam["pauseCamera"] = function() {
            try {
            return cam["pauseCamera"]();
            } catch(e) {}
        }       
        webcam["resumeCamera"] = function() {
            try {
            return cam["resumeCamera"]();
            } catch(e) {}
        }
        webcam["onLoad"]();
        } else if (0 == run) {
        webcam["debug"]("error", "Flash movie not yet registered!");
        } else {
        /* Flash interface not ready yet */
        run--;
        window.setTimeout(_register, 1000 * (4 - run));
        }
    })();
    }

})(jQuery);

放弃此方法,转而使用 JSQRcode