翼度科技»论坛 编程开发 .net 查看内容

C#中轻松实现二维码和条形码识别:OpenCvSharp和ZXing详细教程

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
 
概述:本教程使用OpenCvSharp和ZXing库,详细介绍了在C#中识别二维码和条形码的步骤。通过导入必要的命名空间、加载图像,并使用ZXing库进行二维码和条形码的识别,提供了清晰的示例代码。这方便了开发人员在项目中集成二维码和条形码识别功能。
要使用OpenCvSharp来分别识别二维码和条形码,首先需要确保已经安装了OpenCvSharp库。你可以通过以下步骤在C#中使用OpenCvSharp来实现这两种识别。
步骤 1:安装 OpenCvSharp

在 Visual Studio 中,通过 NuGet 包管理器安装 OpenCvSharp。右击项目 -> NuGet 包管理器 -> 管理 NuGet 包,搜索 OpenCvSharp 并安装。
步骤 2:导入必要的命名空间

在代码文件的开头,导入必要的命名空间。
  1. using OpenCvSharp;
  2. using OpenCvSharp.Extensions;
  3. using ZXing;
复制代码
步骤 3:加载图像

加载包含二维码或条形码的图像。
  1. Mat image = new Mat("path_to_your_image.jpg");
复制代码
步骤 4:二维码识别

使用 ZXing 库来识别二维码。
  1.         /// <summary>
  2.         /// 只识别二维码
  3.         /// </summary>
  4.         static void DecodeQRCodes()
  5.         {
  6.             var img = Cv2.ImRead("2.jpg");
  7.             string[] qrCodeTexts = null;
  8.             //二维码识别
  9.             using (QRCodeDetector qRCodeDetector = new QRCodeDetector())
  10.             {
  11.                 Point2f[] points;
  12.                 var hasQRCode = qRCodeDetector.DetectMulti(img, out points);
  13.                 if (hasQRCode)
  14.                 {
  15.                     qRCodeDetector.DecodeMulti(img, points, out qrCodeTexts);
  16.                 }
  17.             }
  18.             if (qrCodeTexts != null)
  19.             {
  20.                 Console.WriteLine($"检测到{qrCodeTexts.Length}个二维码:");
  21.                 for (int i = 0; i < qrCodeTexts.Length; i++)
  22.                 {
  23.                     Console.WriteLine($"第{(i + 1)}个的内容为:{qrCodeTexts[i]}");
  24.                 }
  25.             }
  26.         }
复制代码
步骤 5:条形码识别

使用 ZXing 库来识别条形码。
  1.         /// <summary>
  2.         /// 识别条码(也可以识别二维码)
  3.         /// </summary>
  4.         static void DecodeBarCodes()
  5.         {
  6.             // 读取图像
  7.             var img = Cv2.ImRead("1.jpg");
  8.             BarcodeReader barcodeReader = new BarcodeReader
  9.             {
  10.                 Options = new DecodingOptions
  11.                 {
  12.                     TryHarder = true,
  13.                     PossibleFormats = new[]
  14.                     {
  15.                         BarcodeFormat.CODE_128,
  16.                         BarcodeFormat.QR_CODE,//这个是二维码
  17.                         BarcodeFormat.EAN_13,
  18.                         BarcodeFormat.EAN_8,
  19.                         BarcodeFormat.CODE_39,
  20.                         BarcodeFormat.CODE_93
  21.                     }
  22.                 },
  23.                 AutoRotate = true,
  24.             };
  25.             // 进行条形码解码
  26.             var barcodes = barcodeReader.DecodeMultiple(img);
  27.             if (barcodes != null)
  28.             {
  29.                 Console.WriteLine($"检测到{barcodes.Length}个条码:");
  30.                 for (int i = 0; i < barcodes.Length; i++)
  31.                 {
  32.                     Console.WriteLine($"第{(i + 1)}个的内容为:{barcodes[i].Text},格式为:{barcodes[i].BarcodeFormat}");
  33.                 }
  34.             }
  35.         }
复制代码
示例代码

下面是完整的示例代码,其中包括了加载图像、二维码识别和条形码识别的步骤:
  1. using System;using OpenCvSharp;
  2. using OpenCvSharp.Extensions;
  3. using ZXing;class Program{        static void Main()        {            Action action = () =>            {                DecodeQRCodes();            };            Action action2 = () =>            {                DecodeBarCodes();            };            long executionTime_qrcode = action.GetExecutionTimer();            Console.WriteLine();            long executionTime_barcode = action2.GetExecutionTimer();            Console.WriteLine();            Console.WriteLine($"识别二维码用时{executionTime_qrcode}毫秒,条件用时{executionTime_barcode}毫秒");            Console.ReadKey();        }}
复制代码
运行效果:
 
请注意,示例中的路径 "path_to_your_image.jpg" 应替换为你实际图像文件的路径。此外,确保图像中包含有效的二维码或条形码。
 


来源:https://www.cnblogs.com/hanbing81868164/p/18060223
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具