一、技术选型对比
| 库名称 | 支持格式 | 识别速度 | 准确率 | 开源协议 |
|---|---|---|---|---|
| ZXing.NET | QR/Code128/EAN13等30+格式 | 中等 | 高 | MIT |
| ZBar | QR/Code128等主流格式 | 快 | 极高 | LGPL |
| TencentOCR | 通用条码+文字识别 | 慢 | 极高 | 闭源 |
二、核心实现方案(ZXing.NET)
1. 环境配置
Install-Package ZXing.Net
Install-Package ZXing.Net.Bindings.SkiaSharp # 图像处理增强
2. 基础识别代码
using ZXing;
using ZXing.Common;
using SkiaSharp;public class BarcodeScanner
{public string Scan(string imagePath){using var bitmap = SKBitmap.Decode(imagePath);var luminanceSource = new SKBitmapLuminanceSource(bitmap);var reader = new BarcodeReader{AutoRotate = true,TryHarder = true,Options = new DecodingOptions{PossibleFormats = new[] { BarcodeFormat.CODE_128, BarcodeFormat.EAN_13 },CharacterSet = "UTF-8",PureBarcode = false}};var result = reader.Decode(luminanceSource);return result?.Text ?? "未识别到条码";}
}
三、图像预处理优化
1. 灰度化与二值化
public static SKBitmap Preprocess(SKBitmap src)
{var dst = new SKBitmap(src.Width, src.Height);using var canvas = new SKCanvas(dst);// 转换为灰度图canvas.DrawBitmap(src, 0, 0, new SKPaint{ColorFilter = SKColorFilter.CreateColorMatrix(new float[]{0.299f, 0.587f, 0.114f, 0, 0,0.299f, 0.587f, 0.114f, 0, 0,0.299f, 0.587f, 0.114f, 0, 0,0, 0, 0, 1, 0})});// 自适应阈值二值化var threshold = new AdaptiveThreshold(0.5f, 5);canvas.DrawBitmap(dst, 0, 0, threshold);return dst;
}
2. 图像增强
public static SKBitmap Enhance(SKBitmap src)
{using var filter = new SKImageFilter.Shadow(SKRect.MakeXYWH(0,0,0,0), 3, 3, 0.5f);return SKBitmap.Decode(src.Encode(SKEncodedImageFormat.Png, 100), filter);
}
参考项目 C# 根据照片识别条形码 www.youwenfan.com/contentcng/49545.html
四、高级功能实现
1. 批量识别
public List<string> BatchScan(IEnumerable<string> paths)
{var results = new ConcurrentBag<string>();Parallel.ForEach(paths, path => {var scanner = new BarcodeScanner();var result = scanner.Scan(path);if (!string.IsNullOrEmpty(result)) results.Add($"{Path.GetFileName(path)}: {result}");});return results.ToList();
}
2. 实时摄像头扫描
public class CameraScanner : IDisposable
{private VideoCaptureDevice _capture;private BarcodeReader _reader;public event Action<string> CodeDetected;public CameraScanner(){_reader = new BarcodeReader{AutoRotate = true,TryHarder = true};_capture = new VideoCaptureDevice(GetBestDevice());_capture.FrameCaptured += OnFrameCaptured;}private void OnFrameCaptured(object sender, FrameEventArgs e){var frame = e.Frame;var result = _reader.Decode(frame);if (result != null) CodeDetected?.Invoke(result.Text);}private static VideoCaptureDevice GetBestDevice(){var devices = CaptureDeviceManager.GetDeviceList(DeviceType.VideoCapture);return devices.FirstOrDefault(d => d.MonikerString.Contains("USB")) ?? devices;}public void Start() => _capture.Start();public void Stop() => _capture.Stop();public void Dispose() => _capture?.Dispose();
}
五、常见问题解决方案
1. 中文乱码
// 生成时指定编码
var options = new QrCodeEncodingOptions
{CharacterSet = "UTF-8",ErrorCorrection = ErrorCorrectionLevel.H
};
2. 低质量图片识别
// 图像预处理流水线
var processed = image.Resize(800, 600) // 统一尺寸.Contrast(1.2f) // 增强对比度.Despeckle(3) // 去噪.ToBitmap();
3. 多码同图识别
var results = reader.DecodeMultiple(bitmap);
foreach (var result in results)
{Console.WriteLine($"类型:{result.BarcodeFormat}, 内容:{result.Text}");
}
六、部署建议
-
硬件要求
- 最低配置:i5-8代+8GB内存+USB3.0接口
- 推荐配置:i7-12代+16GB内存+独立显卡
-
依赖项管理
<!-- NuGet依赖 --> <PackageReference Include="ZXing.Net" Version="0.16.9" /> <PackageReference Include="SkiaSharp" Version="2.88.4" /> -
安装包制作
- 使用MSIX打包工具
- 包含.NET运行时6.0
- 添加设备驱动签名
