在C#中使用ZBar识别条形码
目录:
一.识别库
二.从一张图片中提取多个条形码
三.注意事项
从博客园学了很多,本着分享的目的,希望后来者遇到类似问题时,不必重复造轮子,早点下班回家^-^。
一.识别库
目前主流的识别库主要有ZXing.NET和ZBar,这里我使用的是ZBar,ZXing.NET也试过,同等条件下,识别率不高。
ZBar相关类库包括:libzbar.dll,libzbar-cil.dll,libiconv-2.dll;
很奇怪为什么不能直接引用libzbar.dll,实际使用时引用的是libzbar-cil.dll,libiconv-2.dll是libzbar-cil.dll用来映射libzbar.dll的。
ZBar识别库包含在源码中,文末可直接下载。
二.从一张图片中提取多个条形码
先上截图:
需要提取条形码的图片:
识别结果
主要代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | /// <summary> /// 条码识别 /// </summary> private void ScanBarCode( string fileName) { DateTime now = DateTime.Now; Image primaryImage = Image.FromFile(fileName); Bitmap pImg = MakeGrayscale3((Bitmap)primaryImage); using (ZBar.ImageScanner scanner = new ZBar.ImageScanner()) { scanner.SetConfiguration(ZBar.SymbolType.None, ZBar.Config.Enable, 0); scanner.SetConfiguration(ZBar.SymbolType.CODE39, ZBar.Config.Enable, 1); scanner.SetConfiguration(ZBar.SymbolType.CODE128, ZBar.Config.Enable, 1); List<ZBar.Symbol> symbols = new List<ZBar.Symbol>(); symbols = scanner.Scan((Image)pImg); if (symbols != null && symbols.Count > 0) { string result = string .Empty; symbols.ForEach(s => result += "条码内容:" + s.Data + " 条码质量:" + s.Quality + Environment.NewLine); MessageBox.Show(result); } } } /// <summary> /// 处理图片灰度 /// </summary> /// <param name="original"></param> /// <returns></returns> public static Bitmap MakeGrayscale3(Bitmap original) { //create a blank bitmap the same size as original Bitmap newBitmap = new Bitmap(original.Width, original.Height); //get a graphics object from the new image Graphics g = Graphics.FromImage(newBitmap); //create the grayscale ColorMatrix System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix( new float [][] { new float [] {.3f, .3f, .3f, 0, 0}, new float [] {.59f, .59f, .59f, 0, 0}, new float [] {.11f, .11f, .11f, 0, 0}, new float [] {0, 0, 0, 1, 0}, new float [] {0, 0, 0, 0, 1} }); //create some image attributes ImageAttributes attributes = new ImageAttributes(); //set the color matrix attribute attributes.SetColorMatrix(colorMatrix); //draw the original image on the new image //using the grayscale color matrix g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); //dispose the Graphics object g.Dispose(); return newBitmap; } |
三.注意事项
如果条码识别率不高,考虑是图片的DPI不够。我的项目初期使用的是500万像素的高拍仪,拍出来的图片识别率始终不高,DPI为96。后来更换为800万像素的高拍仪,DPI为120,识别率从60%直接上升到95%+。当然,也需要对图片做一些裁剪。另外,灰度处理是必须的,可减少拍摄照片时反光引起的识别率不高问题。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」