当前位置: 首页 > news >正文

.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示例代码(含关键优化)

using ZXing.Common;
using ZXing;
using ZXing.Windows.Compatibilitypublic 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 = "❌ 识别失败:未检测到有效条码";}}
}

在这里插入图片描述

三、识别率优化技巧

//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);
}

四、常见问题解决

在这里插入图片描述

在这里插入图片描述

相关文章:

  • CVPR2025重磅突破:AnomalyAny框架实现单样本生成逼真异常数据,破解视觉检测瓶颈!
  • Oracle 19c导入数据出现ORA-56935 ORA-39065
  • spring boot 拦截器HandlerInterceptor 不生效的原因排查
  • L2-053 算式拆解 - java
  • DL00924-基于深度学习YOLOv11的工程车辆目标检测含数据集
  • LLM:decoder-only 思考
  • Linux的SHELL脚本中的常用命令
  • Allegro 输出生产数据详解
  • Java设计模式从基础到实际运用
  • 龙虎榜——20250529
  • 学生管理系统V2.0
  • thc-ssl-dos:SSL 压力测试的轻量级工具!全参数详细教程!Kali Linux教程!
  • Linux:shell脚本常用命令
  • 打印机无法远程打印?可以本地打印,本地网络打印机设置给异地使用
  • 【HW系列】—C2远控服务器(webshell链接工具, metasploit、cobaltstrike)的漏洞特征流量特征
  • Linux 进程控制
  • DeepSeek 赋能卫星遥感:AI 驱动数据分析新范式
  • 【笔记】suna部署之获取 Supabase API key 和 project URL
  • 数据可视化--使用matplotlib绘制高级图表
  • 从数据持久化到网络通信与OpenCV:Qt应用程序开发的深度探索与实战
  • 网站快速收录方法/超级外链工具
  • 国际教育机构网站建设开发方案/手机制作网页用什么软件
  • 做3d在哪个网站上接单比较好/seo工作流程
  • 深圳建立网站公司/怎么建公司网站
  • 在线编辑/深圳百度搜索排名优化
  • 公司做网站推广/重庆seo顾问