当前位置:   article > 正文

C# zxing条形码开源库的简单使用例子_c#使用zxing

c#使用zxing

                                   C# zxing条形码开源库的简单使用例子

一、简述

       记--使用zxing条形码开源库生成条形码和识别图片中的条形码。

       例子:链接: https://pan.baidu.com/s/1Xe_ZEs07mfj-YyvgApK_Zw 提取码: cy6t

        zxing库下载:https://pan.baidu.com/s/10jeBSyZPvDxvPb6LXw-xlQ 提取码: xufm 

        官网: https://github.com/micjahn/ZXing.Net/releases     

        

二、效果

        根据输入的英文字符串生成对应的条形码,然后将条形码保存为图片,再根据图片识别出条形码的内容。

        

三、工程结构

         

         

四、源文件

        Form1.cs文件

        

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using ZXing.Common;
  11. namespace BarCode
  12. {
  13. public partial class MainForm : Form
  14. {
  15. public MainForm()
  16. {
  17. InitializeComponent();
  18. }
  19. //生成条形码
  20. private void buttonCreatorBarCode_Click(object sender, EventArgs e)
  21. {
  22. string barCodeStr = textBoxBarCodeStr.Text;
  23. if (barCodeStr != "")
  24. {
  25. setBarCode128(barCodeStr);
  26. }
  27. }
  28. /// <summary>
  29. /// 将生成的条形码设置到pictureBox
  30. /// </summary>
  31. /// <param name="acode">条形码对应的文本</param>
  32. public void setBarCode128(string barCodeStr)
  33. {
  34. try
  35. {
  36. EncodingOptions encodeOption = new EncodingOptions();
  37. encodeOption.Height = 100;
  38. encodeOption.Width = 300;
  39. ZXing.BarcodeWriter wr = new ZXing.BarcodeWriter();
  40. wr.Options = encodeOption;
  41. wr.Format = ZXing.BarcodeFormat.CODE_128;
  42. Bitmap img = wr.Write(barCodeStr);
  43. pictureBoxBarCode.Image = img;
  44. }
  45. catch
  46. { }
  47. }
  48. /// <summary>
  49. /// 识别条形码图片
  50. /// </summary>
  51. /// <param name="picPath">条形码图片的路径</param>
  52. public void getBarCode128FromPic(string picPath)
  53. {
  54. try
  55. {
  56. Bitmap img = new Bitmap(picPath);
  57. pictureBoxBarCodePic2.Image = img;
  58. DecodingOptions decodeOption = new DecodingOptions();
  59. ZXing.BarcodeReader reader = new ZXing.BarcodeReader();
  60. reader.Options = decodeOption;
  61. ZXing.Result result = reader.Decode(img);
  62. string barCodeStr = result.ToString();
  63. labelBarCodeResult.Text = "识别结果是:"+barCodeStr;
  64. }
  65. catch
  66. { }
  67. }
  68. /// <summary>
  69. /// 打开图片文件
  70. /// </summary>
  71. /// <param name="sender"></param>
  72. /// <param name="e"></param>
  73. private void buttonOpenPicFile_Click(object sender, EventArgs e)
  74. {
  75. try
  76. {
  77. OpenFileDialog ofd = new OpenFileDialog();
  78. ofd.Title = "请选择条形码图片文件";//对话框的标题
  79. ofd.InitialDirectory = "D:\\Test";//对话框打开的目录
  80. ofd.Filter = "png文件|*.png|所有文件|*.*";//过滤文件类型
  81. DialogResult result = ofd.ShowDialog();
  82. string res = result.ToString();
  83. if (res == "OK")
  84. {
  85. string filePath = ofd.FileName;
  86. getBarCode128FromPic(filePath);
  87. }
  88. }
  89. catch { }
  90. }
  91. }
  92. }

五、总结

         5.1 添加引用zxing库          

 

         5.2 注意

               

              单独将生成的可执行文件(xxx.exe)放在其他目录运行或者是其它电脑运行,需要将zxing库所在路径设置到环境变量或者是将zxing.dll放在可执行文件的同级目录下。 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/931357
推荐阅读
相关标签
  

闽ICP备14008679号