spire.barcode for .net控件支持创建和识别多达39种条形码,其中包括各种常见的一维和二维条码,例如code39,code128,qr code,pdf417和datamatrix等。这些条形码种类可以在spire.barcode.barcodetype枚举中查看。本文将介绍如何使用spire.barcode for .net控件识别条形码,即从条形码图片中读取数据。
首先我们创建了一个windows forms应用程序,引用spire.barcode.dll文件到工程中,并给form1添加了一些控件。其中load image按钮用于加载条形码图片, picturebox控件用于展示图片,read data按钮用于识别条形码中的数据, textbox控件用于展示识别结果。如下图所示:
form1.cs代码如下:
c#
using system;
using system.drawing;
using system.text;
using system.windows.forms;
using spire.barcode;
namespace scanbarcode
{
public partial class form1 : form
{
public form1()
{
initializecomponent();
}
private void btnloadimage_click(object sender, eventargs e)
{
//加载条形码图片
image image = image.fromfile("code128.png");
picturebox1.image = image;
}
private void btnreaddata_click(object sender, eventargs e)
{
bitmap bitmap = new bitmap(picturebox1.image);
//识别条形码图片中的数据(barcodescanner类包含多个scan重载方法,可根据自己的需求选择相应的方法)
string[] data = barcodescanner.scan(bitmap, barcodetype.code128);
for (int i = 0; i < data.length; i )
{
this.textbox1.text = data[i].tostring();
}
}
}
}
vb.net
imports system.drawing
imports system.text
imports system.windows.forms
imports spire.barcode
namespace scanbarcode
public partial class form1
inherits form
public sub new()
initializecomponent()
end sub
private sub btnloadimage_click(sender as object, e as eventargs)
'加载条形码图片
dim image__1 as image = image.fromfile("code128.png")
picturebox1.image = image__1
end sub
private sub btnreaddata_click(sender as object, e as eventargs)
dim bitmap as new bitmap(picturebox1.image)
'识别条形码图片中的数据(barcodescanner类包含多个scan重载方法,可根据自己的需求选择相应的方法)
dim data as string() = barcodescanner.scan(bitmap, barcodetype.code128)
for i as integer = 0 to data.length - 1
me.textbox1.text = data(i).tostring()
next
end sub
end class
end namespace
效果图: