C#使用ZBar实现识别条形码

 更新时间:2023年07月24日 08:17:47   作者:2206  
目前主流的识别库主要有ZXing.NET和ZBar,本文主要介绍的是如何使用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的。

二.从一张图片中提取多个条形码

先上截图:

需要提取条形码的图片:

识别结果

三.主要代码

/// <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%+。当然,也需要对图片做一些裁剪。另外,灰度处理是必须的,可减少拍摄照片时反光引起的识别率不高问题。

到此这篇关于C#使用ZBar实现识别条形码的文章就介绍到这了,更多相关C# ZBar识别条形码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C#中Task任务类用法详解

    C#中Task任务类用法详解

    这篇文章主要为大家详细介绍了C#中Task任务类用法的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-11-11
  • c# 垃圾回收(GC)优化

    c# 垃圾回收(GC)优化

    这篇文章主要介绍了c# 垃圾回收(GC)优化的相关资料,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下
    2021-02-02
  • C#数字图象处理之肤色检测的方法

    C#数字图象处理之肤色检测的方法

    这篇文章主要介绍了C#数字图象处理之肤色检测的方法,可实现针对肤色的检测功能,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • C#实现将选中复选框的信息返回给用户的方法

    C#实现将选中复选框的信息返回给用户的方法

    这篇文章主要介绍了C#实现将选中复选框的信息返回给用户的方法,涉及C#针对复选框操作的相关技巧,需要的朋友可以参考下
    2015-06-06
  • C#中Linq延迟查询的例子

    C#中Linq延迟查询的例子

    这篇文章主要介绍了C#中Linq延迟查询的例子,本文用一个实例来讲解延迟查询的使用,需要的朋友可以参考下
    2015-06-06
  • C#中关于可空类型的小知识

    C#中关于可空类型的小知识

    这篇文章主要介绍了C#中关于可空类型的小知识,本文讲解可空类型运算的小技巧,需要的朋友可以参考下
    2015-04-04
  • winform简单缓存类实例

    winform简单缓存类实例

    这篇文章主要介绍了winform简单缓存类,涉及C#缓存使用技巧,非常简单实用,需要的朋友可以参考下
    2015-09-09
  • 跳一跳自动跳跃C#代码实现

    跳一跳自动跳跃C#代码实现

    这篇文章主要为大家详细介绍了跳一跳自动跳跃C#代码实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • C#多线程基础知识汇总

    C#多线程基础知识汇总

    这篇文章主要介绍了C#多线程基础知识的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • C# WebService发布以及IIS发布

    C# WebService发布以及IIS发布

    这篇文章主要介绍了C# WebService发布以及IIS发布的相关资料,感兴趣的小伙伴们可以参考一下
    2016-07-07

最新评论