LeadTools中文入门教程(5):读取和编写条形码

LeadTools作为全球领先的图像处理开发包,在条码处理方面功能非常强大。LEADTOOLS Barcode SDK是世界上首屈一指的工具包,它用于开发图像中一维和二维条码读写的应用程序。

LeadTools作为全球领先的图像处理开发包,在条码处理方面功能非常强大。LEADTOOLS Barcode SDK是世界上首屈一指的工具包,它用于开发图像中一维和二维条码读写的应用程序。对于.NET(C# & VB.NET)、C/C++、Java和Web开发者来说,它是快速的、具有高准确和可靠性的条码引擎。LeadTools帮助程序员在记录时间内创建令人惊叹的,功能全面的条码应用程序。LeadTools综合支持100余种条码类型和子类型,如UPC、EAN、Code 128、数据矩阵和PDF417等。

本博文概览:

  • 条码知识简介
  • 创建“读取条码和编写条码”应用程序的具体步骤

条码知识简介

条码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符。常见的条形码是由反射率相差很大的黑条(简称条)和白条(简称空)排成的平行线图案。条形码可以标出物品的生产国、制造厂家、商品名称、生产日期、图书分类 、邮件起止地点、类别、日期等信息,因而在商品流通、图书管理、邮政管理、银行系统等许多领域都得到了广泛的应用。

创建“读取条码和编写条码”应用程序的具体步骤

1. 打开Visual Studio .NET。

2. 点击 文件->新建->项目…。

3. 打开新建项目对话框后,在模板中选择“Visual C#”或“Visual Basic”,随后选择“Windows窗体应用程序”。在名称栏中输入项目名称“Reading and Writing Barcodes”,并使用“浏览”按钮选择您工程的存储路径,点击“确定”。

4. 在“解决方案资源管理器”中,右击“引用”,选择“添加引用”。根据当前工程的 Framework 版本和生成目标平台,选择添加相应的LeadTools控件,例如工程中的版本为 Framework 4.0、生成目标平台是 x86,则浏览选择Leadtools For .NET文件夹” BinDotNet4Win32”,选择以下的DLL“:

  • Leadtools.dll
  • Leadtools.Forms.dll
  • Leadtools.Barcode.dll
  • Leadtools.Barcode.OneD.dll
  • Leadtools.Codecs.dll
  • Leadtools.Codecs.Tif.dll
  • Leadtools.Codecs.Fax.dll

点击“确定”按钮,将以上所有的DLL添加到应用程序中。

5. 切换到Form1代码视图,将以下代码添加至文件开始:

 [C#]    using Leadtools;    using Leadtools.Codecs;    using Leadtools.Forms;    using Leadtools.Barcode;

6. 拖拽三个button至Form1。根据下表设置它们的属性:

Name Text
loadImageButton 加载图像
readBarcodesButton 读取条码
writeBarcodeButton 编写条码

7. 将以下私有变量添加至Form1:

 [C#]    private BarcodeEngine barcodeEngineInstance; // 条码引擎    private RasterImage theImage; // 当前加载图像    private string imageFileName; // 最后加载的图像,用于编写条码

8. 将以下初始化代码添加至Form1:

 [C#]    protected override void OnLoad(EventArgs e)    {       // 用LEADTOOLS提供的解锁密钥替换或使用评估版内核           //解锁一维条码的读取       string MY_LICENSE_FILE = "d:\temp\TestLic.lic";       string MY_DEVELOPER_KEY = "xyz123abc";       RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY);            // 创建BarcodeEngine实例       barcodeEngineInstance = new BarcodeEngine();            base.OnLoad(e);    }

9. 将以下清除代码添加至Form1:

 [C#]    protected override void OnFormClosed(FormClosedEventArgs e)    {       // 删除我们的资源           if(theImage != null)       {          theImage.Dispose();       }            base.OnFormClosed(e);    }

10. 将以下代码添加至loadImageButton按钮的Click事件句柄:

 [C#]    private void loadImageButton_Click(object sender, EventArgs e)    {       using(OpenFileDialog dlg = new OpenFileDialog())       {          if(dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)          {             fileName = dlg.FileName;          }          else          {             return;          }       }            // 加载图像并显示       using(RasterCodecs codecs = new RasterCodecs())       {          RasterImage newImage = codecs.Load(fileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1);               //如果有旧图像,将其删除          if(theImage != null)          {             theImage.Dispose();          }               theImage = newImage;          imageFileName = fileName;       }    }

11. 将以下代码添加至readBarcodesButton按钮的Click事件句柄:

 [C#]    private void readBarcodesButton_Click(object sender, EventArgs e)    {       if(theImage == null)       {          MessageBox.Show("请先加载图像");          return;       }            try       {          // 读取所有条码。          // 第一个参数为一个图像,我们将从这个图像中读取条码。          // 第二个参数为搜索矩形。空矩形表示整个图像。          // 第三个参数为可读取条码的最大数目。0表示。          // 最后一个参数为一个我们感兴趣的BarcodeSymbology数组。可传参null(或不传值)表示我们希望读取          // 所有可用的条码(这些条码均来自于这个图像,并有目前的解锁支持机制支持)          BarcodeData[] dataArray = barcodeEngineInstance.Reader.ReadBarcodes(theImage, LogicalRectangle.Empty, 0, null);               StringBuilder sb = new StringBuilder();          sb.AppendFormat("共有{0}个条码", dataArray.Length);          sb.AppendLine();               for(int i = 0; i < dataArray.Length; i++)          {             BarcodeData data = dataArray[i];                  sb.AppendFormat("符 : {0}, 位置: {1}, 数据: {2}", data.Symbology.ToString(), data.Bounds.ToString(), data.Value);             sb.AppendLine();          }                  MessageBox.Show(sb.ToString());       }       catch(Exception ex)       {          MessageBox.Show(ex.Message);       }    }

12. 将以下代码添加至writeBarcodeButton按钮的Click事件句柄:

 [C#]    private void writeBarcodeButton_Click(object sender, EventArgs e)    {       if(theImage == null)       {          return;       }            // 创建一个 UPC A 条码       BarcodeData data = new BarcodeData();       data.Symbology = BarcodeSymbology.UPCA;       data.Value = "01234567890";       data.Bounds = new LogicalRectangle(10, 10, 600, 200, LogicalUnit.Pixel);            // 设置选项启用错误检查,并将文本显示在条码的下方               OneDBarcodeWriteOptions options = new OneDBarcodeWriteOptions();       options.EnableErrorCheck = true;       try       {          // 编写条码                     barcodeEngineInstance.Writer.WriteBarcode(theImage, data, options);               // 保存图像          string dir = System.IO.Path.GetDirectoryName(imageFileName);          string name = System.IO.Path.GetFileNameWithoutExtension(imageFileName);          string saveFileName = System.IO.Path.Combine(dir, name + "_WriteBarcode.tif");               using(RasterCodecs codecs = new RasterCodecs())          {             codecs.Save(theImage, saveFileName, RasterImageFormat.Tif, theImage.BitsPerPixel);          }               MessageBox.Show(string.Format("此条码已编写成功,并保存至 {0}", saveFileName));       }       catch(Exception ex)       {          MessageBox.Show(ex.Message);       }    }

13. 编译并运行程序。以下为运行结果。

首先点击“加载图像”按钮,效果如下图:

leadtools

随后若您点击 “读取条码”按钮则可读取出所有条码并显示信息,效果如下图:

leadtools

若您点击“编写条码”按钮则可在图像中编写一个一维UPC A条码并保存,效果如下图:

leadtools

DEMO下载:

Reading and Writing Barcodes.rar

文章转自:葡萄城控件产品博客,http://blog.gcpowertools.com.cn

如果你希望亲自体验一下LeadTools读取和编写条码的功能,不妨查看并免费下载LeadTools试用版自己来试试。

如需帮助,请联系在线客服!

标签:图像缩放图像处理图像打印条码识别条码读取条码打印文档图像BarcodeWinForms

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2016年6月10日
下一篇 2016年6月10日

相关推荐

发表回复

登录后才能评论