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



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

[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 中得到错误

错误:"Image"不包含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>

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

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

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

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

最新更新