C# 和 VB.NET 应用程序中的 # 条形码和 QRs
使用 Iron Software 的 Iron Barcode 软件库,在 C# 和所有其他 .NET 语言中读写条形码变得非常容易。
安装 IronBarcode
第一步是安装 Iron Barcode,可使用我们的 NuGet 软件包或通过以下方式实现 下载 DLL 铁条形码类的信息可在 IronBarcode
命名空间。
安装 IronBarcode 的最简单方法是使用 Visual-Studio 的 NuGet 软件包管理器:
软件包名称为 "Barcode"。
Install-Package BarCode
https://www.nuget.org/packages/Barcode/
阅读条形码或 QR 码
使用 Iron Barcode 读取条码只需几行代码。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-1.cs
using IronBarCode;
BarcodeResults results = BarcodeReader.Read("QuickStart.jpg");
if (results != null)
{
foreach (BarcodeResult result in results)
{
Console.WriteLine(result.Text);
}
}
Imports IronBarCode
Private results As BarcodeResults = BarcodeReader.Read("QuickStart.jpg")
If results IsNot Nothing Then
For Each result As BarcodeResult In results
Console.WriteLine(result.Text)
Next result
End If
为了提高读取条形码的速度和准确性,我们可以指定特定的条形码格式 (或格式) 这样,Iron Barcode 就不必尝试已知的每一种条形码类型。如果已知,我们还可以选择图像的特定裁剪区域或区域进行查找。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-2.cs
using IronBarCode;
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
CropArea = new System.Drawing.Rectangle(100, 200, 300, 400),
};
BarcodeResults result = BarcodeReader.Read("QuickStart.jpg", myOptionsExample);
if (result != null)
{
Console.WriteLine(result.First().Text);
}
Imports IronBarCode
Private myOptionsExample As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
.CropArea = New System.Drawing.Rectangle(100, 200, 300, 400)
}
Private result As BarcodeResults = BarcodeReader.Read("QuickStart.jpg", myOptionsExample)
If result IsNot Nothing Then
Console.WriteLine(result.First().Text)
End If
我们还可以选择在一次简单扫描中扫描多个条形码。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-3.cs
using IronBarCode;
BarcodeResults results = BarcodeReader.Read("MultipleBarcodes.png");
// Loop through the results
foreach (BarcodeResult result in results)
{
string value = result.Value;
Bitmap img = result.BarcodeImage;
BarcodeEncoding barcodeType = result.BarcodeType;
byte[] binary = result.BinaryValue;
Console.WriteLine(result.Value);
}
Imports IronBarCode
Private results As BarcodeResults = BarcodeReader.Read("MultipleBarcodes.png")
' Loop through the results
For Each result As BarcodeResult In results
Dim value As String = result.Value
Dim img As Bitmap = result.BarcodeImage
Dim barcodeType As BarcodeEncoding = result.BarcodeType
Dim binary() As Byte = result.BinaryValue
Console.WriteLine(result.Value)
Next result
我们还可以使用 Iron Barcode 从 PDF 文档的一页或多页或多页 TIFF 文件中读取条形码。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-4.cs
using IronBarCode;
BarcodeResults pagedResults = BarcodeReader.Read("MultipleBarcodes.pdf");
// Loop through the results
foreach (BarcodeResult result in pagedResults)
{
int pageNumber = result.PageNumber;
string value = result.Value;
Bitmap img = result.BarcodeImage;
BarcodeEncoding barcodeType = result.BarcodeType;
byte[] binary = result.BinaryValue;
Console.WriteLine(result.Value);
}
// or from a multi-page TIFF scan with image correction:
BarcodeResults multiFrameResults = BarcodeReader.Read(inputImage: "Multiframe.tiff", new BarcodeReaderOptions
{
Speed = ReadingSpeed.Detailed,
ExpectMultipleBarcodes = true,
ExpectBarcodeTypes = BarcodeEncoding.Code128,
Multithreaded = false,
RemoveFalsePositive = false,
ImageFilters = null
});
Imports IronBarCode
Private pagedResults As BarcodeResults = BarcodeReader.Read("MultipleBarcodes.pdf")
' Loop through the results
For Each result As BarcodeResult In pagedResults
Dim pageNumber As Integer = result.PageNumber
Dim value As String = result.Value
Dim img As Bitmap = result.BarcodeImage
Dim barcodeType As BarcodeEncoding = result.BarcodeType
Dim binary() As Byte = result.BinaryValue
Console.WriteLine(result.Value)
Next result
' or from a multi-page TIFF scan with image correction:
Dim multiFrameResults As BarcodeResults = BarcodeReader.Read(inputImage:= "Multiframe.tiff", New BarcodeReaderOptions With {
.Speed = ReadingSpeed.Detailed,
.ExpectMultipleBarcodes = True,
.ExpectBarcodeTypes = BarcodeEncoding.Code128,
.Multithreaded = False,
.RemoveFalsePositive = False,
.ImageFilters = Nothing
})
书写条形码
要使用 Iron Barcode 创建或写入条形码,我们需要使用 BarcodeWriter 类。使用 BarcodeWriter,编写条形码变得非常简单。我们只需指定条形码格式和我们希望表达的值,它就会创建一个图像,该图像可作为 System.Drawing.Image 对象导出或保存到文件中。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-5.cs
using IronBarCode;
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.SaveAsImage("myBarcode.png");
Imports IronBarCode
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.SaveAsImage("myBarcode.png")
造型条形码
使用 Iron Barcode 可以非常容易地操作条形码的视觉描述。每个 BarcodeWriter 操作的输出都是一个生成的条形码对象。生成的条形码对象具有 Fluent API,允许在一行代码中设置条形码的图形设置,类似于 Linq。
常用的条形码样式功能包括调整条形码大小、设置边距、更改背景颜色、更改条形码颜色以及验证输出条形码是否仍然可读。
同样,我们还可以在条形码上添加注释,如任意字体的文本,或在条形码下方或上方轻松放置条形码的值。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-6.cs
using IronBarCode;
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.AddAnnotationTextAboveBarcode("Product URL:");
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.SaveAsImage("myBarcode.png");
Imports IronBarCode
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.AddAnnotationTextAboveBarcode("Product URL:")
myBarcode.AddBarcodeValueTextBelowBarcode()
myBarcode.SaveAsImage("myBarcode.png")
将条形码导出为 HTML
生成的条形码对象还有一个方便的功能,即从生成的条形码中导出 HTML。它可以将条形码导出为无外部资产的独立 HTML 文档、独立 HTML 标记或数据 URI。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-7.cs
using IronBarCode;
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.AddAnnotationTextAboveBarcode("Product URL:");
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.SetMargins(100);
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple);
// All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png");
Imports IronBarCode
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.AddAnnotationTextAboveBarcode("Product URL:")
myBarcode.AddBarcodeValueTextBelowBarcode()
myBarcode.SetMargins(100)
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple)
' All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png")
生成 QR 代码
使用 Iron Barcode 生成 QR 代码时,我们可以选择使用 QR Code Writer 类而不是 BarcodeWriter 类。该类为 QR 代码编写提供了一些新的有趣功能。它允许我们设置 QR 纠错级别,使您可以在 QR 代码的大小和易读性之间取得平衡。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-8.cs
using IronBarCode;
QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf");
Imports IronBarCode
QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf")
Iron Barcode 还可以对 QR 码进行风格化处理,例如在图像的正中央放置徽标图形并将其固定在网格上。还可以对 QR 码进行着色,使其与特定品牌或图形标识相匹配。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-9.cs
using IronBarCode;
using IronSoftware.Drawing;
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo);
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf");
Imports IronBarCode
Imports IronSoftware.Drawing
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo)
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf")
支持的条形码格式
Iron Barcode 支持大量常用条码格式:
- QR 码,包括带有徽标和颜色的装饰和品牌 QR 码
- 多格式条形码,如 Aztec、数据矩阵、CODE 93 和 CODE 128
- RSS Expanded Databar、UPS MaxiCode 和 USPS IMb OneCode 条形码
- 堆叠线性条形码,如 RSS-14 和 PDF-417
- 传统数字条形码格式,如 UPCA、UPCE、EAN-8、EAN-13、Codabar、ITF、MSI 和 Plessey
:path=/static-assets/barcode/content-code-examples/get-started/get-started-10.cs
using IronBarCode;
var myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.SaveAsImage("myBarcode.png");
myBarcode.SaveAsGif("myBarcode.gif");
myBarcode.SaveAsHtmlFile("myBarcode.html");
myBarcode.SaveAsJpeg("myBarcode.jpg");
myBarcode.SaveAsPdf("myBarcode.Pdf");
myBarcode.SaveAsPng("myBarcode.png");
myBarcode.SaveAsTiff("myBarcode.tiff");
myBarcode.SaveAsWindowsBitmap("myBarcode.bmp");
Image myBarcodeImage = myBarcode.Image;
Bitmap myBarcodeBitmap = myBarcode.ToBitmap();
string dataUrl = myBarcode.ToDataUrl();
string imgTagForHtml = myBarcode.ToHtmlTag();
byte[] pngBytes = myBarcode.ToPngBinaryData();
// Binary barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
using (System.IO.Stream pdfStream = myBarcode.ToPdfStream())
{
// Stream barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
}
myBarcode.StampToExistingPdfPage("ExistingPDF.pdf", 1, 200, 50);
Imports IronBarCode
Private myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.SaveAsImage("myBarcode.png")
myBarcode.SaveAsGif("myBarcode.gif")
myBarcode.SaveAsHtmlFile("myBarcode.html")
myBarcode.SaveAsJpeg("myBarcode.jpg")
myBarcode.SaveAsPdf("myBarcode.Pdf")
myBarcode.SaveAsPng("myBarcode.png")
myBarcode.SaveAsTiff("myBarcode.tiff")
myBarcode.SaveAsWindowsBitmap("myBarcode.bmp")
Dim myBarcodeImage As Image = myBarcode.Image
Dim myBarcodeBitmap As Bitmap = myBarcode.ToBitmap()
Dim dataUrl As String = myBarcode.ToDataUrl()
Dim imgTagForHtml As String = myBarcode.ToHtmlTag()
Dim pngBytes() As Byte = myBarcode.ToPngBinaryData()
' Binary barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
Using pdfStream As System.IO.Stream = myBarcode.ToPdfStream()
' Stream barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
End Using
myBarcode.StampToExistingPdfPage("ExistingPDF.pdf", 1, 200, 50)
Why Choose Iron Barcode?
Iron Barcode 为.NET 开发人员提供了一个友好的 API 来读写条形码,该 API 优化了条形码的准确性,并降低了实际应用中的错误率。
例如,BarcodeWriter 类将自动验证和纠正 UPCA 和 UPCE 条形码上的 "校验和"。它还会 "清零 "对于特定数字格式来说太短的数字。 如果您的数据不适合指定的数据格式,Iron Barcode 将指示开发者使用更合适的条形码格式。
当条形码已被扫描或已从照片图像中读取时,Iron Barcode 将在读取条形码方面大显身手。换句话说,图像不是完美的图形,也不是简单的机器截图。
Iron Barcode 包括自动旋转和透视校正、数字噪音校正,并能自动检测图像中编码的条形码类型。
向前迈进
要进一步了解 Iron Barcode,我们建议您阅读本文档中的教程,访问我们的 GitHub,并阅读 .NET API 参考 的 MSDN 格式。