简体   繁体   中英

How to scan the barcode image using ZXing in mvc?

I try to scan the barcode image using image and paste the barcode number in textbox

[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();
}

I getting the error in LoadForm

Error: 'Image' does not contain a definition for LoadForm.

Ajax call:

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

I Solved, Maybe someone from somewhere will get usefull.

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

It returns Scan type and Scan Content. Thanks

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM