C# .NET QR 码生成器
正如您在创建条形码入门教程中所读到的,使用 IronBarcode 创建条形码、设计条形码样式以及将条形码导出为图像都非常简单,通常只需一行代码即可完成。
在本示例中,我们将更深入地了解 QR 码,QR 码在工业应用和零售业中越来越受欢迎。
如何在 C# 中读取并生成 QR 码
- 下载二维码生成器库
- 添加一行代码即可创建 QR 代码
- 将您的徽标添加到 QR 代码中
- 验证您的 QR 代码是否可读
- 读写二进制数据的 QR
在C#中安装QR Code Generator库
开始在您的项目中使用IronPDF,并立即获取免费试用。
查看 IronBarcode 上 Nuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变。
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
用一行代码创建 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")
通过纠错,我们可以确定 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")
此示例将 Visual Studio 徽标添加到条形码中。它会自动将徽标的大小调整到合适的尺寸,使纯代码仍然可读,并将徽标与 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")
验证 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")
最后几行代码将二维码导出为独立的 HTML 文件,不含任何资产,然后在默认浏览器中打开该 HTML 文件。
读写二进制数据
二维码是处理二进制数据的绝佳格式。有时,二进制数据比处理文本更节省空间或更合适。
在本例中,我们将对字符串中的一些二进制数据进行编码,将其写入 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
总之 条形码 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
还有一种更高级的形式,具有更多的开发人员控制功能:
: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
向前迈进
要了解更多有关此示例和使用 C# 中的 QR 代码我们鼓励你在我们的 GitHub 页面上 fork 或从我们的网站上下载源代码。
源代码下载
下载此 C# QR 码生成器教程和 DLL。
进一步的文件
您还可以找到 QRCodeWriter 和 条码阅读器 的类。
它们记录了 IronBarcode 的全部功能。 VB.NET 条形码生成器这些内容不可能在一本教程中一一道来。