C# .NET QR 码生成器

This article was translated from English: Does it need improvement?
Translated
View the article in English

珍妮弗·赖特

正如您在创建条形码入门教程中所读到的,使用 IronBarcode 创建条形码、设计条形码样式以及将条形码导出为图像都非常简单,通常只需一行代码即可完成。

在本示例中,我们将更深入地了解 QR 码,QR 码在工业应用和零售业中越来越受欢迎。

在C#中安装QR Code Generator库

适用于的C# NuGet库

安装使用 NuGet

Install-Package BarCode
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

适用于的C# NuGet库

安装使用 NuGet

Install-Package BarCode
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

开始在您的项目中使用IronPDF,并立即获取免费试用。

第一步:
green arrow pointer

查看 IronBarcodeNuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变。

适用于的C# NuGet库 nuget.org/packages/BarCode/
Install-Package BarCode

考虑安装 IronBarcode DLL 直接。下载并手动安装到您的项目或GAC表单中: IronBarCode.zip

手动安装到你的项目中

下载DLL

在开始之前,我们需要安装 条形码 NuGet 软件包。

Install-Package BarCode

https://www.nuget.org/packages/BarCode/ 作为替代方案, 可下载 IronBarCode.Dll 并作为 [.NET 条形码 DLL].

导入名称空间

在本教程中,我们需要确保我们的类文件引用了 IronBarCode 以及一些普通的系统程序集。

using IronBarCode;
using System;
using System.Drawing;
using System.Linq;
using IronBarCode;
using System;
using System.Drawing;
using System.Linq;
Imports IronBarCode
Imports System
Imports System.Drawing
Imports System.Linq
VB   C#


用一行代码创建 QR 码

第一个示例向我们展示了如何用一些简单的文本、500 像素的正方形直径和中等误差校正创建一个标准化条形码。

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-1.cs
using IronBarCode;

// Generate a Simple BarCode image and save as PDF
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("MyQR.png");
Imports IronBarCode

' Generate a Simple BarCode image and save as PDF
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("MyQR.png")
VB   C#

通过纠错,我们可以确定 QR 码在现实环境中的读取难度。纠错级别越高,二维码越大,像素越多,复杂度越高。

C# 创建 QR 码图像

添加徽标

在第二个示例中,我们将讨论一个用例,即一家公司希望在其 QR 代码中添加一个徽标,这也是我们最近经常看到的情况。通过使用 QRCodeWriter.CreateQRCodeWithLogo 方法,您可以在下面的代码示例中看到这有多简单。

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-2.cs
using IronBarCode;
using IronSoftware.Drawing;

// You may add styling with color, logo images or branding:
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);
myQRCodeWithLogo.ResizeTo(500, 500).SetMargins(10).ChangeBarCodeColor(Color.DarkGreen);

// Logo will automatically be sized appropriately and snapped to the QR grid.
myQRCodeWithLogo.SaveAsPng("myQRWithLogo.png");
Imports IronBarCode
Imports IronSoftware.Drawing

' You may add styling with color, logo images or branding:
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)
myQRCodeWithLogo.ResizeTo(500, 500).SetMargins(10).ChangeBarCodeColor(Color.DarkGreen)

' Logo will automatically be sized appropriately and snapped to the QR grid.
myQRCodeWithLogo.SaveAsPng("myQRWithLogo.png")
VB   C#

此示例将 Visual Studio 徽标添加到条形码中。它会自动将徽标的大小调整到合适的尺寸,使纯代码仍然可读,并将徽标与 QR 码方格对齐,使其看起来合适。下一行代码会将条形码染成深绿色。不过,它不会使徽标变色。

C# 创建带有徽标图像的 QR 码

Save as Image PDF or HTML

最后,我们将 QR 保存为 PDF。为方便起见,最后一行代码会在默认的 PDF 浏览器中打开 PDF。

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-3.cs
using IronBarCode;

// You may add styling with color, logo images or branding:
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);

myQRCodeWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);

// Save as PDF
myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf");

// Also Save as HTML
myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html");
Imports IronBarCode

' You may add styling with color, logo images or branding:
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)

myQRCodeWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen)

' Save as PDF
myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf")

' Also Save as HTML
myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html")
VB   C#

验证 QR 代码

在 QR 代码中添加徽标和更改颜色时,我们要确保 QR 代码仍然可读。

通过使用 `GeneratedBarcode.Verify()方法,我们可以测试我们的条形码是否仍然可读。

在下面的代码示例中,我们将看到将 QR 代码改为浅蓝色实际上会使其变得不可读。我们会在代码中检测到这一点,并选择深蓝色。

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-4.cs
using IronBarCode;
using IronSoftware.Drawing;
using System;

// Verifying QR Codes
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode MyVerifiedQR = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);

MyVerifiedQR.ChangeBarCodeColor(System.Drawing.Color.LightBlue);

if (!MyVerifiedQR.Verify())
{
    Console.WriteLine("\t LightBlue is not dark enough to be read accurately.  Lets try DarkBlue");
    MyVerifiedQR.ChangeBarCodeColor(Color.DarkBlue);
}
MyVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html");

// open the barcode html file in your default web browser
System.Diagnostics.Process.Start("MyVerifiedQR.html");
Imports Microsoft.VisualBasic
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System

' Verifying QR Codes
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private MyVerifiedQR As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)

MyVerifiedQR.ChangeBarCodeColor(System.Drawing.Color.LightBlue)

If Not MyVerifiedQR.Verify() Then
	Console.WriteLine(vbTab & " LightBlue is not dark enough to be read accurately.  Lets try DarkBlue")
	MyVerifiedQR.ChangeBarCodeColor(Color.DarkBlue)
End If
MyVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html")

' open the barcode html file in your default web browser
System.Diagnostics.Process.Start("MyVerifiedQR.html")
VB   C#

最后几行代码将二维码导出为独立的 HTML 文件,不含任何资产,然后在默认浏览器中打开该 HTML 文件。

C# 读写 QR

读写二进制数据

二维码是处理二进制数据的绝佳格式。有时,二进制数据比处理文本更节省空间或更合适。

在本例中,我们将对字符串中的一些二进制数据进行编码,将其写入 QR 格式的条形码中,然后将其作为二进制数据的位数组读回。您将注意到这一功能在许多条码库中并不常见,这使得 IronBarcode 在这方面独树一帜。

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-5.cs
using IronBarCode;
using System;
using System.Linq;

// Create Some Binary Data - This example equally well for Byte[] and System.IO.Stream
byte[] BinaryData = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/");

// WRITE QR with Binary Content
QRCodeWriter.CreateQrCode(BinaryData, 500).SaveAsImage("MyBinaryQR.png");

// READ QR with Binary Content
var MyReturnedData = BarcodeReader.Read("MyBinaryQR.png").First();
if (BinaryData.SequenceEqual(MyReturnedData.BinaryValue))
{
    Console.WriteLine("\t Binary Data Read and Written Perfectly");
}
else
{
    throw new Exception("Corrupted Data");
}
Imports Microsoft.VisualBasic
Imports IronBarCode
Imports System
Imports System.Linq

' Create Some Binary Data - This example equally well for Byte[] and System.IO.Stream
Private BinaryData() As Byte = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/")

' WRITE QR with Binary Content
QRCodeWriter.CreateQrCode(BinaryData, 500).SaveAsImage("MyBinaryQR.png")

' READ QR with Binary Content
Dim MyReturnedData = BarcodeReader.Read("MyBinaryQR.png").First()
If BinaryData.SequenceEqual(MyReturnedData.BinaryValue) Then
	Console.WriteLine(vbTab & " Binary Data Read and Written Perfectly")
Else
	Throw New Exception("Corrupted Data")
End If
VB   C#
C# 读写二进制数据作为 QR 码

总之 条形码 C# 库 是专为现实世界的 QR 码使用而设计的。它不仅能快速准确地读取 QR 码,还允许我们对 QR 码进行样式设计、添加徽标、验证条形码,并用二进制数据进行编码。

阅读 QR 码

为了避免您在不同教程之间跳来跳去,我将添加一个代码示例,介绍我最喜欢的使用 IronBarcode 读取 QR 码的方法。

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-6.cs
using IronBarCode;
using System;
using System.Linq;

BarcodeResults result = BarcodeReader.Read("QR.png", new BarcodeReaderOptions() { ExpectBarcodeTypes = BarcodeEncoding.QRCode });
if (result != null)
{
    Console.WriteLine(result.First().Value);
}
Imports IronBarCode
Imports System
Imports System.Linq

Private result As BarcodeResults = BarcodeReader.Read("QR.png", New BarcodeReaderOptions() With {.ExpectBarcodeTypes = BarcodeEncoding.QRCode})
If result IsNot Nothing Then
	Console.WriteLine(result.First().Value)
End If
VB   C#

还有一种更高级的形式,具有更多的开发人员控制功能:

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-7.cs
using IronBarCode;
using System;
using System.Linq;

BarcodeReaderOptions options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Faster,
    ExpectMultipleBarcodes = false,
    ExpectBarcodeTypes = BarcodeEncoding.All,
    Multithreaded = false,
    MaxParallelThreads = 0,
    CropArea = null,
    UseCode39ExtendedMode = false,
    RemoveFalsePositive = false,
    ImageFilters = null
};

BarcodeResults result = BarcodeReader.Read("QR.png", options);
if (result != null)
{
    Console.WriteLine(result.First().Value);
}
Imports IronBarCode
Imports System
Imports System.Linq

Private options As New BarcodeReaderOptions With {
	.Speed = ReadingSpeed.Faster,
	.ExpectMultipleBarcodes = False,
	.ExpectBarcodeTypes = BarcodeEncoding.All,
	.Multithreaded = False,
	.MaxParallelThreads = 0,
	.CropArea = Nothing,
	.UseCode39ExtendedMode = False,
	.RemoveFalsePositive = False,
	.ImageFilters = Nothing
}

Private result As BarcodeResults = BarcodeReader.Read("QR.png", options)
If result IsNot Nothing Then
	Console.WriteLine(result.First().Value)
End If
VB   C#

向前迈进

要了解更多有关此示例和使用 C# 中的 QR 代码我们鼓励你在我们的 GitHub 页面上 fork 或从我们的网站上下载源代码。

源代码下载

下载此 C# QR 码生成器教程和 DLL。

进一步的文件

您还可以找到 QRCodeWriter条码阅读器 的类。

它们记录了 IronBarcode 的全部功能。 VB.NET 条形码生成器这些内容不可能在一本教程中一一道来。

.NET 条码库可以通过为产品及其特定的实物批次生产代码生成唯一标识符,彻底改变线上和线下产品目录的连接方式。

珍妮弗·赖特

应用架构主管

Jenny 在一家制药巨头的数字产品开发部门领导软件产品架构。Jenny 的团队正在使用 IronBarcode 将在线和离线产品目录连接起来。