.NET WinForm图像识别二维码/条形码并读取其中内容

需求:图像识别出一张图片中的二维码或者条形码,并读取其中内容。

一、安装库(特别注意,网上很多都没说清楚)

如果是基于.net framework,则安装ZXing.Net(建议0.14.0版本左右,具体看实际,版本太高,部分接口发生变化)

如果是基于.Net Standard 2.0 or .NET CORE 3.0/3.1 or .NET 5.0 or higher,则安装ZXing.Net.Bindings.Windows.Compatibility

二、WinForm示例代码(含关键优化)

csharp 复制代码
using ZXing.Common;
using ZXing;
using ZXing.Windows.Compatibility

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }

    // 选择图片按钮点击事件
    private void btnSelectImage_Click(object sender, EventArgs e)
    {
        OpenFileDialog dialog = new OpenFileDialog();
        dialog.Filter = "图片文件|*.jpg;*.png;*.bmp";
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.Image = Image.FromFile(dialog.FileName);
        }
    }

    // 识别条码按钮点击事件
    private void btnDecode_Click(object sender, EventArgs e)
    {
        if (pictureBox1.Image == null)
        {
            MessageBox.Show("请先选择图片");
            return;
        }

        var bitmap = new Bitmap(pictureBox1.Image);
        
        // 创建解码器(关键配置)
        var reader = new BarcodeReader
        {
            Options = new DecodingOptions
            {
                PossibleFormats = new[] { 
                    BarcodeFormat.QR_CODE, 
                    BarcodeFormat.CODE_128,  // 条形码
                    BarcodeFormat.EAN_13 
                },
                TryHarder = true,      // 提高复杂图像识别率
                CharacterSet = "UTF-8" // 支持中文
            }
        };
        
        // 识别条码(支持多码)
        Result[] results = reader.DecodeMultiple(bitmap);
        
        if (results != null)
        {
            foreach (Result result in results)
            {
                txtResult.AppendText($"✅ 识别成功!类型:{result.BarcodeFormat},内容:{result.Text}\r\n");
            }
        }
        else
        {
            txtResult.Text = "❌ 识别失败:未检测到有效条码";
        }
    }
}

三、识别率优化技巧

csharp 复制代码
//1. 图像预处理(解决模糊/低对比度问题)
csharp
// 转换为灰度图+二值化
var luminanceSource = new BitmapLuminanceSource(bitmap);
var binarizer = new HybridBinarizer(luminanceSource);
var binBitmap = new BinaryBitmap(binarizer);

Result result = reader.Decode(binBitmap); // 使用处理后的图像
//2. 多尺度识别(针对小尺寸条码)
csharp
for (double scale = 1.0; scale <= 2.0; scale += 0.2)
{
    var scaledBitmap = new Bitmap(bitmap, 
        new Size((int)(bitmap.Width * scale), (int)(bitmap.Height * scale)));
    
    Result result = reader.Decode(scaledBitmap);
    if (result != null) break;
}
//3. 区域裁剪(复杂背景中定位条码)
csharp
// 假设已知条码在图像右下角1/4区域
Rectangle cropArea = new Rectangle(
    bitmap.Width / 2, 
    bitmap.Height / 2, 
    bitmap.Width / 2, 
    bitmap.Height / 2
);

using (Bitmap cropped = bitmap.Clone(cropArea, bitmap.PixelFormat))
{
    Result result = reader.Decode(cropped);
}

四、常见问题解决

相关推荐
zzzhpzhpzzz9 小时前
Win10快速安装.NET3.5
.net·win10
许泽宇的技术分享11 小时前
Windows MCP.Net:基于.NET的Windows桌面自动化MCP服务器深度解析
windows·自动化·.net
百锦再19 小时前
.NET 的 WebApi 项目必要可配置项都有哪些?
java·开发语言·c#·.net·core·net
hqwest1 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
做一位快乐的码农1 天前
基于.net、C#、asp.net、vs的保护大自然网站的设计与实现
c#·asp.net·.net
YF云飞2 天前
.NET 在鸿蒙系统(HarmonyOS Next)上的适配探索与实践
华为·.net·harmonyos
小码编匠2 天前
C# Bitmap 类在工控实时图像处理中的高效应用与避坑
后端·c#·.net
咕白m6252 天前
C# 将 Excel 转为 CSV 的高效解决方案
.net
不知名搬运工3 天前
18 ABP Framework 模块管理
.net
追逐时光者3 天前
精选 5 款 .NET 开源、功能强大的工作流系统,告别重复造轮子!
后端·.net