• C#使用Zxing.dll组件解析二维码


    C#使用Zxing.dll组件解析二维码

    1.首先下载Zxing.dll组件,将dll组件放置debug文件夹中,引用参考,引入空间命名。

    2.解码方法

    1. string result = string.Empty;
    2. //--解码
    3. private string RQDecode(Bitmap img)
    4. {
    5. string errText = string.Empty;
    6. Result result = null;
    7. if (img != null)
    8. {
    9. try
    10. {
    11. result = new BarcodeReader().Decode(new Bitmap(pictureBox1.Image));
    12. string barCodeStr = result.ToString();
    13. labelBarCodeResult.Text = "识别结果是:" + barCodeStr;
    14. //listBox1.Items.Add(barCodeStr);
    15. }
    16. catch { return errText; }
    17. if (result != null)
    18. {
    19. return result.Text;
    20. }
    21. else
    22. {
    23. return errText;
    24. }
    25. }
    26. else
    27. {
    28. return errText;
    29. }
    30. }

    3.全部源码

    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;
    11. namespace Code_test
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. }
    19. private string pathname = string.Empty;//定义路径名变量
    20. private void button1_Click(object sender, EventArgs e)
    21. {
    22. DialogOperate openfile = new DialogOperate();//实例化对象
    23. textBox1.Text = openfile.OpenFile();//调用方法,显示图片路径
    24. pathname = textBox1.Text;//获取文件路径
    25. if (pathname != string.Empty)//这个判断用处不大
    26. {
    27. try
    28. {
    29. this.pictureBox1.Load(pathname);//加载图片路径
    30. }
    31. catch (Exception ex)
    32. {
    33. MessageBox.Show(ex.Message);
    34. }
    35. }
    36. }
    37. string result = string.Empty;
    38. //--解码
    39. private string RQDecode(Bitmap img)
    40. {
    41. string errText = string.Empty;
    42. Result result = null;
    43. if (img != null)
    44. {
    45. try
    46. {
    47. result = new BarcodeReader().Decode(new Bitmap(pictureBox1.Image));
    48. string barCodeStr = result.ToString();
    49. labelBarCodeResult.Text = "识别结果是:" + barCodeStr;
    50. //listBox1.Items.Add(barCodeStr);
    51. }
    52. catch { return errText; }
    53. if (result != null)
    54. {
    55. return result.Text;
    56. }
    57. else
    58. {
    59. return errText;
    60. }
    61. }
    62. else
    63. {
    64. return errText;
    65. }
    66. }
    67. private void button2_Click(object sender, EventArgs e)
    68. {
    69. RQDecode(new Bitmap(pictureBox1.Image));//开始解析
    70. }
    71. }
    72. }

    4.效果图

  • 相关阅读:
    OpenJDK16 ZGC 源码分析
    asp.net Core 中模拟微信公众号文件验证
    二分搜索树节点删除(Java 实例代码)
    java-构造方法(超详细,奶奶都能看懂)
    计算机毕业论文选题java毕业设计软件源代码strust2+mybatis客户关系系统[包运行成功]
    shell通配符与glob
    vue3知识点:provide 与 inject
    CleanMyMac最新版V4.11.4版MAC电脑系统加速器
    Android查漏补缺(5)ContentProvider和ContentResolver
    LeetCode739每日温度
  • 原文地址:https://blog.csdn.net/daisy_juhua/article/details/133272399