图像处理开发工具包:LeadTools
打开Visual Studio .NET。
点击 文件->新建->项目…。
打开新建项目对话框后,在模板中选择“Visual C#”或“Visual Basic”,随后选择“Windows窗体应用程序”。在名称栏中输入项目名称“Reading and Writing Barcodes”,并使用“浏览”按钮选择您工程的存储路径,点击“确定”。
在“解决方案资源管理器”中,右击“引用”,选择“添加引用”。根据当前工程的 Framework 版本和生成目标平台,选择添加相应的LeadTools控件,例如工程中的版本为 Framework 4.0、生成目标平台是 x86,则浏览选择Leadtools For .NET文件夹”
切换到Form1代码视图,将以下代码添加至文件开始: 1: [C#] 2: using Leadtools; 3: using Leadtools.Codecs; 4: using Leadtools.Forms; 5: using Leadtools.Barcode;
拖拽三个button至Form1。根据下表设置它们的属性:
将以下私有变量添加至Form1: 1: [C#] 2: private BarcodeEngine barcodeEngineInstance; // 条码引擎 3: private RasterImage theImage; // 当前加载图像 4: private string imageFileName; // 最后加载的图像,用于编写条码
将以下初始化代码添加至Form1: 1: [C#] 2: protected override void OnLoad(EventArgs e) 3: { 4: // 用LEADTOOLS提供的解锁密钥替换或使用评估版内核 5: //解锁一维条码的读取 6: string MY_LICENSE_FILE = "d:\\temp\\TestLic.lic"; 7: string MY_DEVELOPER_KEY = "xyz123abc"; 8: RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY); 9: 10: // 创建BarcodeEngine实例 11: barcodeEngineInstance = new BarcodeEngine(); 12: 13: base.OnLoad(e); 14: }
将以下清除代码添加至Form1: 1: [C#] 2: protected override void OnFormClosed(FormClosedEventArgs e) 3: { 4: // 删除我们的资源 5: if(theImage != null) 6: { 7: theImage.Dispose(); 8: } 9: 10: base.OnFormClosed(e); 11: }
将以下代码添加至loadImageButton按钮的Click事件句柄: 1: [C#] 2: 3: private void loadImageButton_Click(object sender, EventArgs e) 4: { 5: using(OpenFileDialog dlg = new OpenFileDialog()) 6: { 7: if(dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) 8: { 9: fileName = dlg.FileName; 10: } 11: else 12: { 13: return; 14: } 15: } 16: 17: // 加载图像并显示 18: using(RasterCodecs codecs = new RasterCodecs()) 19: { 20: RasterImage newImage = codecs.Load(fileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1); 21: 22: //如果有旧图像,将其删除 23: if(theImage != null) 24: { 25: theImage.Dispose(); 26: } 27: 28: theImage = newImage; 29: imageFileName = fileName; 30: } 31: }
将以下代码添加至readBarcodesButton按钮的Click事件句柄: 1: [C#] 2: private void readBarcodesButton_Click(object sender, EventArgs e) 3: { 4: if(theImage == null) 5: { 6: MessageBox.Show("请先加载图像"); 7: return; 8: } 9: 10: try 11: { 12: // 读取所有条码。 13: // 第一个参数为一个图像,我们将从这个图像中读取条码。 14: // 第二个参数为搜索矩形。空矩形表示整个图像。 15: // 第三个参数为可读取条码的最大数目。0表示。 16: // 最后一个参数为一个我们感兴趣的BarcodeSymbology数组。可传参null(或不传值)表示我们希望读取 17: // 所有可用的条码(这些条码均来自于这个图像,并有目前的解锁支持机制支持) 18: BarcodeData[] dataArray = barcodeEngineInstance.Reader.ReadBarcodes(theImage, LogicalRectangle.Empty, 0, null); 19: 20: StringBuilder sb = new StringBuilder(); 21: sb.AppendFormat("共有{0}个条码", dataArray.Length); 22: sb.AppendLine(); 23: 24: for(int i = 0; i < dataArray.Length; i++) 25: { 26: BarcodeData data = dataArray[i]; 27: 28: sb.AppendFormat("符号: {0}, 位置: {1}, 数据: {2}", data.Symbology.ToString(), data.Bounds.ToString(), data.Value); 29: sb.AppendLine(); 30: } 31: 32: MessageBox.Show(sb.ToString()); 33: } 34: catch(Exception ex) 35: { 36: MessageBox.Show(ex.Message); 37: } 38: }
将以下代码添加至writeBarcodeButton按钮的Click事件句柄: 1: [C#] 2: 3: private void writeBarcodeButton_Click(object sender, EventArgs e) 4: { 5: if(theImage == null) 6: { 7: return; 8: } 9: 10: // 创建一个 UPC A 条码 11: BarcodeData data = new BarcodeData(); 12: data.Symbology = BarcodeSymbology.UPCA; 13: data.Value = ""; 14: data.Bounds = new LogicalRectangle(10, 10, 600, 200, LogicalUnit.Pixel); 15: 16: // 设置选项启用错误检查,并将文本显示在条码的下方 17: OneDBarcodeWriteOptions options = new OneDBarcodeWriteOptions(); 18: options.EnableErrorCheck = true; 19: 20: try 21: { 22: // 编写条码 23: barcodeEngineInstance.Writer.WriteBarcode(theImage, data, options); 24: 25: // 保存图像 26: string dir = System.IO.Path.GetDirectoryName(imageFileName); 27: string name = System.IO.Path.GetFileNameWithoutExtension(imageFileName); 28: string saveFileName = System.IO.Path.Combine(dir, name + "_WriteBarcode.tif"); 29: 30: using(RasterCodecs codecs = new RasterCodecs()) 31: { 32: codecs.Save(theImage, saveFileName, RasterImageFormat.Tif, theImage.BitsPerPixel); 33: } 34: 35: MessageBox.Show(string.Format("此条码已编写成功,并保存至 {0}", saveFileName)); 36: } 37: catch(Exception ex) 38: { 39: MessageBox.Show(ex.Message); 40: } 41: }
编译并运行程序。以下为运行结果。首先点击“加载图像”按钮,效果如下图:
随后若您点击 “读取条码”按钮则可读取出所有条码并显示信息,效果如下图:
若您点击“编写条码”按钮则可在图像中编写一个一维UPC A条码并保存,效果如下图: