如何在MVC中使用ZXing扫描条形码图像?

时间:2018-07-16 12:40:42

标签: javascript ajax asp.net-mvc zxing barcode-scanner

我尝试使用图像扫描条形码图像并将条形码编号粘贴到文本框中

[HttpPost]
public ActionResult ScanDetail( Scaner Scanning)
{
    IBarcodeReader reader = new BarcodeReader();

    // load a bitmap    
    var barcodeBitmap = (Bitmap)Image.LoadFrom("C:\\sample-barcode-image.png");

    // detect and decode the barcode inside the bitmap
    var result = reader.Decode(barcodeBitmap);

    // do something with the result
    if (result != null)
    {
        Scanning.ScanType = result.BarcodeFormat.ToString();
        Scanning.ScanContent = result.Text;   
    }

    return View();
}

我在LoadForm中遇到错误

  

错误:“图像”不包含LoadForm的定义。

Ajax调用:

<script type="text/javascript">
    $("#BtnScan").click(function () {

        var ScanType = $('#ScanType').val();
        var ScanContent = $('#ScanContent').val();
       $.ajax({
            url: "@Url.Action("ScanDetail", "Home")",
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            data: JSON.stringify({
                ScanType: $("#ScanType").val(),
                ScanContent: $("#ScanContent").val()
            }),
            async: false
        });
    });
</script>

2 个答案:

答案 0 :(得分:0)

我解决了,也许某个地方的人会有用。

  

Scaner.cs

    public string ScanContent { get; set; }
    public string ScanType { get; set; }
  

Home Controller.cs

 [HttpPost]
    public ActionResult ScanDetail(Scaner Scanning)
    {
        IBarcodeReader reader = new BarcodeReader();


        using (Bitmap oldBmp = new Bitmap("E:\\barcodeQR.jpg"))
        using (Bitmap newBmp = new Bitmap(oldBmp))
        using (Bitmap targetBmp = newBmp.Clone(new Rectangle(0, 0, newBmp.Width, newBmp.Height), PixelFormat.Format32bppArgb))
        {
            // targetBmp is now in the desired format.
            var barcodeBitmap = (targetBmp);
            var result = reader.Decode(barcodeBitmap);
            // do something with the result
            if (result != null)
            {
                Scanning.ScanType = result.BarcodeFormat.ToString();
                Scanning.ScanContent = result.Text;

            }

        }
        return View();
    }

它返回扫描类型和扫描内容。 谢谢

答案 1 :(得分:-1)

var barcodeBitmap = (Bitmap)Image.FromFile("C:\\sample-barcode-image.png");