大芳旅游 發表於 2025-7-9 10:32:00

国产化条码类库Spire.Barcode教程:使用 C# 读取二维码(QR Code)——从图片或数据流解析

<p><img src="https://image.evget.com/attachment/keditor/image/20250709/095610_8.png"></p>
<p>二维码已成为现代应用的常见组成部分,广泛应用于用户身份验证、移动支付、商品包装和活动票务等场景。很多使用 C# 开发的系统需要从图像或扫描件中提取二维码信息,因此掌握二维码识别技术显得尤为重要。</p>
<p>为满足这类需求,开发者需要一种既可靠又易于集成的二维码解码方式。本文将通过一个简洁明了的示例,演示如何借助&nbsp;<strong>Spire.Barcode for .NET</strong>&nbsp;<strong>使用 C# 从图像中读取二维码</strong>,并在桌面或服务器端项目中轻松实现识别功能。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>&nbsp; &nbsp;获取Spire.Barcode for .NET免费试用,请联系Spire系列产品官方授权代理商<span style="color: rgba(0, 0, 0, 1)">慧都科技</span></strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><em><strong>加入Spire技术交流QQ群(125237868),与更多开发者一起提升文档开发技能。</strong></em></span></p>
<h2>1. 项目配置</h2>
<p>首先我们需要使用支持二维码解码的 .NET 条码库。<em><strong>E-iceblue旗下Spire系列产品是国产化文档处理和转换领域的佼佼者,支持国产化信创。</strong></em>本指南采用&nbsp;<strong>Spire.Barcode for .NET</strong>,该库提供简洁API帮助开发者轻松从图片文件和数据流读取二维码。</p>
<h3>1.1 通过NuGet安装库</h3>
<p>可通过NuGet包管理器安装:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>Install-Package Spire.Barcode</code></pre>
<h3>1.2 创建控制台项目</h3>
<p>演示案例使用 Visual Studio 创建 C# 控制台应用:</p>
<ul>
<li>支持&nbsp;<strong>.NET Framework</strong>、<strong>.NET Core/.NET 6+</strong>、<strong>ASP.NET</strong>&nbsp;或 Xamarin 跨平台移动开发</li>
<li>添加&nbsp;<strong>Spire.Barcode.dll</strong>&nbsp;引用(如未使用NuGet)</li>
</ul>
<h2>2. 用 C# 从图片读取二维码</h2>
<p>通过库提供的静态方法&nbsp;<strong>BarcodeScanner.Scan()</strong>&nbsp;即可<strong>从图片文件读取二维码</strong>。该方法接收图片路径和&nbsp;BarcodeType&nbsp;作为参数,返回匹配指定类型(如二维码)的所有解码结果。此方法支持 JPG、PNG、EMF 等图片格式,适用于控制台应用、桌面应用或处理上传图像的服务端程序。</p>
<h3>2.1 C# 示例代码:从图片文件解码二维码</h3>
<pre class="prettyprint lang-js highlighter-hljs"><code>using Spire.Barcode;

class Program
{
    static void Main(string[] args)
    {
      // 加载二维码图片
      string imagePath = @"C:\qr-code.png";

      // 条码扫描器从图片读取二维码
      string[] results = BarcodeScanner.Scan(imagePath, BarCodeType.QRCode);

      // 显示解码结果
      foreach (string result in results)
      {
            Console.WriteLine("二维码内容: " + result + "\n");
      }
    }
}</code></pre>
<p>二维码图片及 C# 解码效果:</p>
<p><img src="https://www.e-iceblue.cn/images/tutorials-images/read-qr-code-csharp-1.png"></p>
<h3>2.2 代码说明</h3>
<ul>
<li><strong>Scan()</strong>&nbsp;方法读取并解码图片中所有条码</li>
<li><strong>BarCodeType.QRCode</strong>&nbsp;参数确保仅检测二维码(可修改为其他类型)</li>
<li>返回数组格式以支持多二维码场景</li>
</ul>
<h2>3. 用 C# 从数据流读取二维码</h2>
<p>在Web API或现代应用中处理内存图像时,常需操作&nbsp;<strong>Stream</strong>&nbsp;对象——例如处理文件上传或读取云存储时。</p>
<p><strong>BarcodeScanner.Scan()</strong>&nbsp;方法直接支持&nbsp;<strong>Stream</strong>&nbsp;输入,无需转换即可轻松解码二维码:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>using Spire.Barcode;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
      using (FileStream fs = new FileStream(@"C:\qr-code.png", FileMode.Open, FileAccess.Read))
      {
            // 直接从数据流解析二维码
            string[] results = BarcodeScanner.Scan(fs, BarCodeType.QRCode, false);
            foreach (string result in results)
            {
                Console.WriteLine("二维码内容: " + result);
            }
      }
    }
}</code></pre>
<p>此方法特别适用于 WPF 或 ASP.NET Core 等需要内存处理的应用。</p>
<h2>4. 提高准确率与错误处理</h2>
<p>实际场景中可能因图像质量导致识别失败,以下是提升解码成功率的实践方案:</p>
<h3>4.1 优化识别准确率</h3>
<ul>
<li><strong>使用高清图片</strong>,避免模糊或过度压缩</li>
<li><strong>保留静区</strong>(二维码周围空白区域)</li>
<li><strong>优先选择PNG格式</strong>保证清晰度</li>
<li><strong>避免透视畸变</strong>——使用正对拍摄的图片</li>
</ul>
<h3>4.2 增强错误处理</h3>
<p>通过 try-catch 块优化异常处理:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>try
{
    string[] results = BarcodeScanner.Scan(imagePath, BarCodeType.QRCode);

    if (results.Length == 0)
    {
      Console.WriteLine("未检测到二维码");
    }
    else
    {
      Console.WriteLine("二维码内容: " + results);
    }
}
catch (Exception ex)
{
    Console.WriteLine("解码错误: " + ex.Message);
}</code></pre>
<h2>5. 进阶技巧:获取二维码在图片上的坐标</h2>
<p>有时需要获取二维码在图片中的精确位置(用于裁剪或标注),可通过&nbsp;ScanInfo()&nbsp;方法获取边界框:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>ScanResult[] results = scanner.ScanInfo(imagePath, BarCodeType.QRCode);
foreach (BarcodeInfo result in results)
{
    Console.WriteLine("内容: " + result.DataString);
    Console.WriteLine($"坐标: " + string.Join(",", result.Vertexes.Select(p =&gt; $"({p.X},{p.Y})")) + "\n");
}</code></pre>
<p>该方案可同时获取二维码数据和位置信息。</p>
<p>效果演示:</p>
<p><img src="https://www.e-iceblue.cn/images/tutorials-images/read-qr-code-csharp-2.png"></p>
<h2>6. 常见问题</h2>
<h3>如何在 C# 中读取二维码?</h3>
<p>使用&nbsp;<strong>Spire.Barcode for .NET</strong>&nbsp;库的&nbsp;<strong>BarcodeScanner.Scan()</strong>&nbsp;方法,几行代码即可实现从图片或数据流读取二维码。</p>
<h3>如何读取其他类型条码(非二维码)?</h3>
<p>向&nbsp;<strong>Scan()</strong>&nbsp;方法传入图片路径后,将自动检测所有支持类型。若需限定检测类型(如仅 QR 码或 Code128),传入对应的&nbsp;<strong>BarCodeType</strong>&nbsp;参数即可。</p>
<h3>如何扫描一张图片上的多个二维码?</h3>
<p>直接使用&nbsp;<strong>BarcodeScanner.Scan()</strong>&nbsp;方法即可扫描一张图片上的多个二维码。该方法支持自动识别图片上条码的类型,并返回所有条码的扫描结果。</p>
<h2>7. 总结</h2>
<p>使用&nbsp;<strong>Spire.Barcode for .NET&nbsp;</strong>库,仅需少量代码即可在 C# 中实现二维码读取。该方案支持图片和数据流解码,完美适配桌面应用、服务端及 WPF 程序,且配置简单性能优异。</p>
<p>基于此基础,可进一步探索二维码生成、文档集成和实时扫描等进阶应用。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>&nbsp;获取Spire.Barcode for .NET免费试用,请联系Spire系列产品官方授权代理商<span style="color: rgba(0, 0, 0, 1)">慧都科技</span></strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><em><strong>加入Spire技术交流QQ群(125237868),与更多开发者一起提升文档开发技能。</strong></em></span></p><br><br>
来源:https://www.cnblogs.com/software-Development/p/18974446
頁: [1]
查看完整版本: 国产化条码类库Spire.Barcode教程:使用 C# 读取二维码(QR Code)——从图片或数据流解析