当前位置:   article > 正文

【WinForm】C#使用ZXing.Net生成二维码和条形码,包含识别条码内容_c# zxing 生成条形码

c# zxing 生成条形码

首先是生成条码

先看看效果

新建项目,在项目中引入zxing.dll

 

生成二维码的代码很简单,如下

  1. Image image = new BarcodeWriter()
  2. {
  3. Format = BarcodeFormat.QR_CODE,
  4. Options = new QrCodeEncodingOptions
  5. {
  6. DisableECI = true,
  7. CharacterSet = "UTF-8",
  8. Width = 200,
  9. Height = 200,
  10. Margin = 0
  11. }
  12. }.Write("123");

就这么简单

Format 表示生成条码的类型,可以生成二维码,一维码(条形码)

     BarcodeFormat.QR_CODE 表示二维码

     BarcodeFormat.CODE_128 表示条形码

Options 可以设置条码的编码,大小,边距

 

首先,条码的格式有很多种,一起来探索一下

在窗体加载时,我们将BarcodeFormat的所有数据绑定在ComboBox上

  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. Array array = Enum.GetValues(typeof(BarcodeFormat));
  4. Dictionary<BarcodeFormat, string> bFs = new Dictionary<BarcodeFormat, string>();
  5. foreach (var item in array)
  6. {
  7. bFs.Add((BarcodeFormat)item, item.ToString());
  8. }
  9. cmbBarcodeFormat.DataSource = new BindingSource() { DataSource = bFs };
  10. cmbBarcodeFormat.ValueMember = "Key";
  11. cmbBarcodeFormat.DisplayMember = "Value";
  12. }

点击生成按钮时,生成条码显示在PictureBox上

  1. private void btnGenerate_Click(object sender, EventArgs e)
  2. {
  3. if (!int.TryParse(tbWidth.Text, out int width))
  4. {
  5. MessageBox.Show("请输入正确整数", "提示");
  6. return;
  7. }
  8. if (!int.TryParse(tbHeight.Text, out int height))
  9. {
  10. MessageBox.Show("请输入正确整数", "提示");
  11. return;
  12. }
  13. if (tbContent.Text == string.Empty)
  14. {
  15. MessageBox.Show("请输入内容", "提示");
  16. return;
  17. }
  18. picBarcode.Width = width;
  19. picBarcode.Height = height;
  20. BarcodeWriter barcodeWriter = new BarcodeWriter()
  21. {
  22. Format = cbSenior.Checked ? (BarcodeFormat)cmbBarcodeFormat.SelectedValue :
  23. (rbQRCode.Checked ? BarcodeFormat.QR_CODE : BarcodeFormat.CODE_128),
  24. Options = new QrCodeEncodingOptions
  25. {
  26. DisableECI = true,
  27. CharacterSet = "UTF-8",
  28. Width = width,
  29. Height = height,
  30. Margin = 0
  31. }
  32. };
  33. try
  34. {
  35. picBarcode.Image = barcodeWriter.Write(tbContent.Text);
  36. }
  37. catch (Exception ex)
  38. {
  39. MessageBox.Show("生成失败\r\n" + ex.Message, "提示");
  40. }
  41. }

勾选高级时,可选所有条码类型,但是测试过程中,部分类型会生成失败,自己探索吧

  1. private void cbSenior_CheckedChanged(object sender, EventArgs e)
  2. {
  3. bool b = cbSenior.Checked;
  4. rbQRCode.Enabled = !b;
  5. rbCode.Enabled = !b;
  6. lbSeniorInfo.Visible = b;
  7. lbBarcodeFormat.Visible = b;
  8. cmbBarcodeFormat.Visible = b;
  9. }

这个只能输入7个或8个数字

这个只能输入12个或13个数字

这个只能输入偶位数

这个只能输入11个或12个数字

 

点击保存时,用DrawToBitmap将图片保存到所选目录

  1. private void btnSave_Click(object sender, EventArgs e)
  2. {
  3. try
  4. {
  5. Control control = picBarcode;
  6. Bitmap bitmap = new Bitmap(control.Width, control.Height);
  7. control.DrawToBitmap(bitmap, new Rectangle(0, 0, control.Width, control.Height));
  8. SaveFileDialog saveFileDialog = new SaveFileDialog()
  9. {
  10. Filter = "图片(*.png)|*.png|图片(*.jpg)|*.jpg|所有文件|*.*",
  11. FileName = "barcode",
  12. RestoreDirectory = true
  13. };
  14. if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
  15. {
  16. string fileName = saveFileDialog.FileName;
  17. bitmap.Save(fileName);
  18. string path = fileName.Substring(0, fileName.LastIndexOf("\\"));
  19. System.Diagnostics.Process.Start(path);//打开文件夹
  20. }
  21. }
  22. catch (Exception ex)
  23. {
  24. MessageBox.Show("保存失败\r\n" + ex.Message, "提示");
  25. }
  26. }

保存二维码

 

接下来就是识别条码了

我们可以将图片拖进软件进行识别

首先设置Form的AllowDrop为True,再添加DragEnter和DragDrop两个事件

  1. BarcodeReader barcodeReader = new BarcodeReader();
  2. private void FrmBarcode_DragEnter(object sender, DragEventArgs e)
  3. {
  4. e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Link : DragDropEffects.None;
  5. }
  6. private void FrmBarcode_DragDrop(object sender, DragEventArgs e)
  7. {
  8. try
  9. {
  10. string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
  11. picBarcode.Load(path);
  12. Result result = barcodeReader.Decode((Bitmap)picBarcode.Image);
  13. tbContent.Text = result?.Text;
  14. }
  15. catch(Exception ex)
  16. {
  17. MessageBox.Show("解析失败\r\n" + ex.Message, "提示");
  18. }
  19. }

将图片拖到软件中,即可识别

如果不是条码,则识别为空

现在在拖入刚才保存下来的条码

识别成功


资源地址:

https://download.csdn.net/download/weixin_38211198/11172340

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

闽ICP备14008679号