C# 使用ZXing.Net识别二维码和条码

目录

写在前面

代码实现

调用示例


写在前面

上一篇写了 C# 使用ZXing.Net生成二维码和条码-CSDN博客 

使用ZXing.Net解码非常简单,事实上就只用一行代码就好了,这么简单那为什么还要贴在这里呢,原因是开始时,在网上看资料看到一篇文章,稀里哗啦写了一堆代码,然后拿来运行一下,竟然还得不到预期的结果;看了下用了最复杂的方式,调用了参数最多的重载函数,问题却没有解决,所以在网上查找资料,鉴别成本有时候是很高的;大部分的代码拷来拷去,完全不做验证,实在是浪费时间;最简单有效的办法还是尽量往信息的源头去追溯,比如官网文档、GitHub上的源码介绍、业内大牛的文章等。最好还是自己把代码跑一遍,不仅可以确认也能加深印象,下次碰到同类问题或许拿来就用了,效率就是这么提升的。

官网: GitHub - micjahn/ZXing.Net: .Net port of the original java-based barcode reader and generator library zxing 

代码实现

    var reader = new BarcodeReader();
    var result = reader.Decode((Bitmap)pictureBox1.Image);
    if (result != null)
    {
        lblMessage.Text = result.Text;
    }
    else
    {
        lblMessage.Text = "None";
    }

反面教材原代码如下:

// create a barcode reader instance
IBarcodeReader reader = new BarcodeReader();  
// 加载图片文件
Bitmap image = new Bitmap("D:\\PrideJoy\\Zxing.Demo\\Zxing.demo\\bin\\Debug\\net7.0\\qr-image.jpg");

// 获取rawRGB数据
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
System.Drawing.Imaging.BitmapData bmpData = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = Math.Abs(bmpData.Stride) * image.Height;
byte[] rawRGB = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rawRGB, 0, bytes);
image.UnlockBits(bmpData);

// 获取格式(format)
RGBLuminanceSource.BitmapFormat format;
switch (image.PixelFormat)
{
    case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
        format = RGBLuminanceSource.BitmapFormat.Gray8;
        break;
    case System.Drawing.Imaging.PixelFormat.Format16bppGrayScale:
        format = RGBLuminanceSource.BitmapFormat.Gray16;
        break;
    case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
        format = RGBLuminanceSource.BitmapFormat.RGB24;
        break;
    case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
        format = RGBLuminanceSource.BitmapFormat.RGB32;
        break;
    case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
        format = RGBLuminanceSource.BitmapFormat.ARGB32;
        break;
    // 其他格式的处理
    default:
        format = RGBLuminanceSource.BitmapFormat.Unknown;
        break;
}

// 获取宽度(width)和高度(height)
int width = image.Width;
int height = image.Height;
var result = reader.Decode(rawRGB,width,height, format);
// do something with the result
if (result != null)
{
    Console.WriteLine("内容为:"+result.Text);
}

调用示例

相关推荐

  1. Unity利用ZXing库 生成识别

    2023-12-30 16:44:03       52 阅读
  2. uniapp——长按识别

    2023-12-30 16:44:03       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-30 16:44:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-30 16:44:03       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-30 16:44:03       87 阅读
  4. Python语言-面向对象

    2023-12-30 16:44:03       96 阅读

热门阅读

  1. vue3-回顾之,状态管理神器 大菠萝- pinia

    2023-12-30 16:44:03       56 阅读
  2. CSS学习之-02

    2023-12-30 16:44:03       61 阅读
  3. c++:new和delete的运算符重载

    2023-12-30 16:44:03       57 阅读
  4. k8s报错处理

    2023-12-30 16:44:03       56 阅读
  5. 212. Word Search II

    2023-12-30 16:44:03       56 阅读
  6. 深入浅出理解Web认证:Session、Cookie与Token

    2023-12-30 16:44:03       60 阅读
  7. 二、计算机软件及其使用-文字处理软件 Word 2016

    2023-12-30 16:44:03       64 阅读
  8. Windows自动化之UIautomation(一)

    2023-12-30 16:44:03       51 阅读
  9. 前端网络面试:浏览器输入地址后发生了什么?

    2023-12-30 16:44:03       55 阅读
  10. C语言计算三阶行列式

    2023-12-30 16:44:03       68 阅读
  11. 20世纪40年代是指哪一年

    2023-12-30 16:44:03       235 阅读