在实际环境中测试
在生产中测试无水印。
随时随地为您服务。
条形码扫描仪并不总是适合我们的应用。您可能已经有了条形码的数字图像,并希望知道它代表的英文文本。此外,这种扫描仪只能读取一维条形码,而一维条形码包含的数据量有限,只能在 Windows RT 类库中使用。二维条形码 (也称二维码) 现在,"...... "已经很常见,可以容纳更多的信息。
使用简单的 API 调用和几个编码步骤,就可以创建一个基于 C# 的应用程序来读取条形码。支持 .NET 的应用程序可在 Windows、macOS 或 Linux 上运行,无需依赖任何第三方工具或 API。
本文将比较两个功能最强大的 .NET Core 应用程序库,它们可用于以编程方式读取条形码。这两个库分别是 IronBarcode 和 ZXing.NET。我们将了解 IronBarcode 比 ZXing.NET 更强大、更健壮的原因。
条码写入器像素数据
类来定制条形码格式
属性QrCodeEncodingOptions
类,以进一步自定义高度、重量和边距写
方法ZXing.NET 是一个解码和生成条形码的库 (如 QR 码、PDF 417、EAN、UPC、Aztec、数据矩阵、Codabar).ZXing 是 "斑马线 "的缩写,是一个基于 Java 的开源库,支持多种一维和二维条形码格式。
其基本特征如下:
IronBarcode 是一个允许程序员读取和生成条形码的 C# 库。作为一个领先的条形码库,IronBarcode 支持多种一维和二维条形码,其中包括装饰条形码、条形码和条形码。 (彩色和品牌) 二维码。它支持 .NET 标准和核心版本 2 及更高版本,可在 Azure、Linux、macOS、Windows 和 Web 上跨平台使用。IronBarcode是.NET系统的一个著名类库或组件,允许C#、VB.NET和F#开发人员使用标准化编程语言工作。它能让客户浏览扫描仪标签并创建新的标准化标签。它与二维条形码和其他三维标准化条形码配合使用效果特别好。
IronBarcode 现在支持二维条码。它提供了优化这些条形码的着色、样式和像素的功能,并能在条形码上添加徽标,以用于印刷或广告材料。该库还可以读取倾斜和变形的条形码,而其他条形码软件可能无法读取这些条形码。
要使用 ZXing.NET 库,请使用 NuGet 包管理器控制台在 ASP.NET Core 应用程序中安装以下两个包:
Install-Package ZXing.Net
Install-Package ZXing.Net.Bindings.CoreCompat.System.Drawing -Version 0.16.5-beta
或者,使用 NuGet 包管理器在项目中安装 ZXing.NET。为此,请转到 Tools > NuGet Package Manager > Manage NuGet packages for solutions...,然后切换到 "Browse(浏览)"选项卡并搜索 "ZXing.NET"。
使用 NuGet 软件包管理器安装 IronBarcode,或直接从 产品网站.......。 IronBarcode 命名空间包含所有 IronBarcode 类。
IronBarcode 可使用 Visual Studio 的 NuGet 软件包管理器安装:软件包名称为 "Barcode"。
Install-Package BarCode
首先,在项目文件的根目录下新建一个名为 "qrr "的文件夹。
然后,我们将在 "qrr "文件夹中创建 QR 文件并存储图像系统文件。
在控制器中,添加 `GenerateFile()方法,如下源代码所示。
public ActionResult GenerateFile()
{
return View();
}
[HttpPost]
public ActionResult GenerateFile(string qrText)
{
Byte [] byteArray;
var width = 250; // width of the QR Code
var height = 250; // height of the QR Code
var margin = 0;
var qrCodeWriter = new ZXing.BarcodeWriterPixelData
{
Format = ZXing.BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Height = height,
Width = width,
Margin = margin
}
};
var pixelData = qrCodeWriter.Write(qrText);
// creating a PNG bitmap from the raw pixel data; if only black and white colors are used it makes no difference if the raw pixel data is BGRA oriented and the bitmap is initialized with RGB
using (var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
{
using (var ms = new MemoryStream())
{
var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
try
{
// we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan, pixelData.Pixels.Length);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
// save to folder
string fileGuid = Guid.NewGuid().ToString().Substring(0, 4);
bitmap.Save(Server.MapPath("~/qrr") + "/file-" + fileGuid + ".png", System.Drawing.Imaging.ImageFormat.Png);
// save to stream as PNG
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byteArray = ms.ToArray();
}
}
return View(byteArray);
}
public ActionResult GenerateFile()
{
return View();
}
[HttpPost]
public ActionResult GenerateFile(string qrText)
{
Byte [] byteArray;
var width = 250; // width of the QR Code
var height = 250; // height of the QR Code
var margin = 0;
var qrCodeWriter = new ZXing.BarcodeWriterPixelData
{
Format = ZXing.BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Height = height,
Width = width,
Margin = margin
}
};
var pixelData = qrCodeWriter.Write(qrText);
// creating a PNG bitmap from the raw pixel data; if only black and white colors are used it makes no difference if the raw pixel data is BGRA oriented and the bitmap is initialized with RGB
using (var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
{
using (var ms = new MemoryStream())
{
var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
try
{
// we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan, pixelData.Pixels.Length);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
// save to folder
string fileGuid = Guid.NewGuid().ToString().Substring(0, 4);
bitmap.Save(Server.MapPath("~/qrr") + "/file-" + fileGuid + ".png", System.Drawing.Imaging.ImageFormat.Png);
// save to stream as PNG
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byteArray = ms.ToArray();
}
}
return View(byteArray);
}
Public Function GenerateFile() As ActionResult
Return View()
End Function
<HttpPost>
Public Function GenerateFile(ByVal qrText As String) As ActionResult
Dim byteArray() As Byte
Dim width = 250 ' width of the QR Code
Dim height = 250 ' height of the QR Code
Dim margin = 0
Dim qrCodeWriter = New ZXing.BarcodeWriterPixelData With {
.Format = ZXing.BarcodeFormat.QR_CODE,
.Options = New QrCodeEncodingOptions With {
.Height = height,
.Width = width,
.Margin = margin
}
}
Dim pixelData = qrCodeWriter.Write(qrText)
' creating a PNG bitmap from the raw pixel data; if only black and white colors are used it makes no difference if the raw pixel data is BGRA oriented and the bitmap is initialized with RGB
Using bitmap = New System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
Using ms = New MemoryStream()
Dim bitmapData = bitmap.LockBits(New System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
Try
' we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan, pixelData.Pixels.Length)
Finally
bitmap.UnlockBits(bitmapData)
End Try
' save to folder
Dim fileGuid As String = Guid.NewGuid().ToString().Substring(0, 4)
bitmap.Save(Server.MapPath("~/qrr") & "/file-" & fileGuid & ".png", System.Drawing.Imaging.ImageFormat.Png)
' save to stream as PNG
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
byteArray = ms.ToArray()
End Using
End Using
Return View(byteArray)
End Function
剩下的唯一改动就是将 QR 代码文件保存在 "qrr "文件夹中。这需要使用下面的两行代码来完成。
string fileGuid = Guid.NewGuid().ToString().Substring(0, 4);
bitmap.Save(Server.MapPath("~/qrr") + "/file-" + fileGuid + ".png", System.Drawing.Imaging.ImageFormat.Png);
string fileGuid = Guid.NewGuid().ToString().Substring(0, 4);
bitmap.Save(Server.MapPath("~/qrr") + "/file-" + fileGuid + ".png", System.Drawing.Imaging.ImageFormat.Png);
Dim fileGuid As String = Guid.NewGuid().ToString().Substring(0, 4)
bitmap.Save(Server.MapPath("~/qrr") & "/file-" & fileGuid & ".png", System.Drawing.Imaging.ImageFormat.Png)
您还必须创建 GenerateFile
视图,并在其中包含以下代码。生成文件 "视图与索引视图相同。
@model Byte []
@using (Html.BeginForm(null, null, FormMethod.Post))
{
<table>
<tbody>
<tr>
<td>
<label>Enter text for creating QR Code</label>
</td>
<td>
// text box to enter text...
<input type="text" name="qrText" />
</td>
</tr>
<tr>
<td colspan="2">
<button>Submit</button>
</td>
</tr>
</tbody>
</table>
}
@{
if (Model != null)
{
<h3>QR Code Successfully Generated</h3>
// img tag to display generated QR code...
<img src="@String.Format("data:image/png;base64,{0}", Convert.ToBase64String(Model))" />
}
}
@model Byte []
@using (Html.BeginForm(null, null, FormMethod.Post))
{
<table>
<tbody>
<tr>
<td>
<label>Enter text for creating QR Code</label>
</td>
<td>
// text box to enter text...
<input type="text" name="qrText" />
</td>
</tr>
<tr>
<td colspan="2">
<button>Submit</button>
</td>
</tr>
</tbody>
</table>
}
@{
if (Model != null)
{
<h3>QR Code Successfully Generated</h3>
// img tag to display generated QR code...
<img src="@String.Format("data:image/png;base64,{0}", Convert.ToBase64String(Model))" />
}
}
model Function [using](Html.BeginForm ByVal As (Nothing, Nothing, FormMethod.Post)) As Byte()
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: <table> <tbody> <tr> <td> <label> Enter text for creating QR Code</label> </td> <td> <input type="text" name="qrText" /> </td> </tr> <tr> <td colspan="2"> <button> Submit</button> </td> </tr> </tbody> </table>
"qrText" /> </td> </tr> (Of tr) <td colspan="2"> (Of button) Submit</button> </td> </tr> </tbody> </table>
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: <table> <tbody> <tr> <td> <label> Enter text for creating QR Code</label> </td> <td> <input type="text" name="qrText" /> </td> </tr> <tr> <td colspan
"text" name="qrText" /> </td> </tr> (Of tr) <td colspan
(Of table) (Of tbody) (Of tr) (Of td) (Of label) Enter text for creating QR Code</label> </td> (Of td) <input type="text" name
End Function
@
If True Then
If Model IsNot Nothing Then
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
(Of h3) QR Code Successfully Generated</h3> <img src="@String.Format("data:image/png
base64,
If True Then
0
End If
", Convert.ToBase64String(Model))" />
End If
End If
在文本框中输入任意值,然后点击 "提交 "按钮。QR 代码将生成并以 .PNG 文件格式保存在 "qrr "文件夹中。
IronBarcode 支持多种常用条码格式,包括
var MyBarCode = IronBarcode.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);
var MyBarCode = IronBarcode.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);
Dim MyBarCode = IronBarcode.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)
另一方面,ZXing 是一个基于 Java 的开源一维/二维条形码图像处理库。支持 UPC-A、UPC-E、EAN-8、Code 93、Code 128、QR Code、Data Matrix、Aztec、PDF 417 和其他条形码格式。
要使用 IronBarcode 创建 QR 码,我们可以使用 "QRCodeWriter "类,而不是 "BarcodeWriter "类。该类为创建 QR 代码引入了一些新颖而有趣的功能。它使我们能够设置 QR 纠错级别,让您在 QR 代码的大小和可读性之间取得平衡。
// Generate a simple QR Code image and save as PNG
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("MyQR.png");
// Generate a simple QR Code image and save as PNG
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("MyQR.png");
' Generate a simple QR Code image and save as PNG
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("MyQR.png")
通过纠错,我们可以明确在现实世界中读取 QR 码的难易程度。纠错级别越高,二维码越大,像素越多,复杂度越高。在下图中,我们可以看到显示的 QR 码文件。
我们首先从 IronBarcode.BarcodeWriterEncoding
枚举中指定条形码的值和条形码格式。然后,我们可以保存为图像、System.Drawing.Image
或位图(Bitmap)代码对象。这就是您需要的所有源代码!
// Generate a simple BarCode image and save as PNG using following namespaces
using IronBarCode;
GeneratedBarcode MyBarCode = IronBarcode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128);
MyBarCode.SaveAsPng("MyBarCode.png");
// This line opens the image in your default image viewer
System.Diagnostics.Process.Start("MyBarCode.png");
// Generate a simple BarCode image and save as PNG using following namespaces
using IronBarCode;
GeneratedBarcode MyBarCode = IronBarcode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128);
MyBarCode.SaveAsPng("MyBarCode.png");
// This line opens the image in your default image viewer
System.Diagnostics.Process.Start("MyBarCode.png");
' Generate a simple BarCode image and save as PNG using following namespaces
Imports IronBarCode
Private MyBarCode As GeneratedBarcode = IronBarcode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128)
MyBarCode.SaveAsPng("MyBarCode.png")
' This line opens the image in your default image viewer
System.Diagnostics.Process.Start("MyBarCode.png")
IronBarcode 还支持 QR 码的风格化,例如在图像的正中央放置徽标图形并将其固定在网格上。还可以对 QR 码进行着色,使其与特定品牌或图形标识相匹配。
为了进行测试,请在下面的代码示例中创建一个徽标,看看使用 QRCodeWriter.CreateQRCodeWithLogo
方法有多简单。
// Adding a Logo
var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "visual-studio-logo.png", 500);
MyQRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);
// Adding a Logo
var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "visual-studio-logo.png", 500);
MyQRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);
' Adding a Logo
Dim MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "visual-studio-logo.png", 500)
MyQRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen)
最后,我们将生成的 QR 代码保存为 PDF 文件。为方便起见,最后一行代码将 QR 代码保存为 HTML 文件。
// Adding a Logo
var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "visual-studio-logo.png", 500);
MyQRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);
//Save as PDF
MyQRWithLogo.SaveAsPdf("MyQRWithLogo.pdf");
//Also Save as HTML
MyQRWithLogo.SaveAsHtmlFile("MyQRWithLogo.html");
// Adding a Logo
var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "visual-studio-logo.png", 500);
MyQRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);
//Save as PDF
MyQRWithLogo.SaveAsPdf("MyQRWithLogo.pdf");
//Also Save as HTML
MyQRWithLogo.SaveAsHtmlFile("MyQRWithLogo.html");
' Adding a Logo
Dim MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "visual-studio-logo.png", 500)
MyQRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen)
'Save as PDF
MyQRWithLogo.SaveAsPdf("MyQRWithLogo.pdf")
'Also Save as HTML
MyQRWithLogo.SaveAsHtmlFile("MyQRWithLogo.html")
下面,我们将看到如何仅用一行代码就创建、样式化和导出条形码。
IronBarcode 包含一个类似于 System.Linq
的流畅 API。我们创建一个条形码,设置其边距,并通过链式方法调用在一行中将其导出为位图。这将非常有用,并使代码更易于阅读。
using IronBarCode;
using System.Drawing;
// Fluent API for Barcode image generation.
string MyValue = "https://ironsoftware.com/csharp/barcode";
Bitmap BarcodeBmp = IronBarcode.BarcodeWriter.CreateBarcode(MyValue, BarcodeEncoding.PDF417).ResizeTo(300,200).SetMargins(100).ToBitmap();
using IronBarCode;
using System.Drawing;
// Fluent API for Barcode image generation.
string MyValue = "https://ironsoftware.com/csharp/barcode";
Bitmap BarcodeBmp = IronBarcode.BarcodeWriter.CreateBarcode(MyValue, BarcodeEncoding.PDF417).ResizeTo(300,200).SetMargins(100).ToBitmap();
Imports IronBarCode
Imports System.Drawing
' Fluent API for Barcode image generation.
Private MyValue As String = "https://ironsoftware.com/csharp/barcode"
Private BarcodeBmp As Bitmap = IronBarcode.BarcodeWriter.CreateBarcode(MyValue, BarcodeEncoding.PDF417).ResizeTo(300,200).SetMargins(100).ToBitmap()
结果,PDF417 条形码的 "System.Drawing.Image "图像如下所示:
当您将 IronBarcode 类库与 .NET 条码阅读器结合使用时,读取条码或 QR 码将变得轻而易举。在第一个示例中,我们可以看到如何仅用一行代码读取条形码。
我们可以获取条形码的值、图像、编码类型和二进制数据 (如有) 然后输出到控制台。
using IronBarCode;
using System;
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("GetStarted.png");
if (Result !=null && Result.Text == "https://ironsoftware.com/csharp/barcode/")
{
Console.WriteLine("GetStarted was a success. Read Value: " + Result.Text);
}
using IronBarCode;
using System;
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("GetStarted.png");
if (Result !=null && Result.Text == "https://ironsoftware.com/csharp/barcode/")
{
Console.WriteLine("GetStarted was a success. Read Value: " + Result.Text);
}
Imports IronBarCode
Imports System
Private Result As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("GetStarted.png")
If Result IsNot Nothing AndAlso Result.Text = "https://ironsoftware.com/csharp/barcode/" Then
Console.WriteLine("GetStarted was a success. Read Value: " & Result.Text)
End If
我们将用几行代码来读取扫描的 PDF 文档并找出所有一维条形码。
正如您所看到的,这与从单个文档中读取单个条形码非常相似,只不过我们现在知道了发现条形码的页码。
using IronBarCode;
using System;
using System.Drawing;
// Multiple barcodes may be scanned up from a single document or image. A PDF document may also be used as the input image
PagedBarcodeResult [] PDFResults = BarcodeReader.ReadBarcodesFromPdf("MultipleBarcodes.pdf");
// Work with the results
foreach (var PageResult in PDFResults)
{
string Value = PageResult.Value;
int PageNum = PageResult.PageNumber;
System.Drawing.Bitmap Img = PageResult.BarcodeImage;
BarcodeEncoding BarcodeType = PageResult.BarcodeType;
byte [] Binary = PageResult.BinaryValue;
Console.WriteLine(PageResult.Value+" on page "+ PageNum);
}
using IronBarCode;
using System;
using System.Drawing;
// Multiple barcodes may be scanned up from a single document or image. A PDF document may also be used as the input image
PagedBarcodeResult [] PDFResults = BarcodeReader.ReadBarcodesFromPdf("MultipleBarcodes.pdf");
// Work with the results
foreach (var PageResult in PDFResults)
{
string Value = PageResult.Value;
int PageNum = PageResult.PageNumber;
System.Drawing.Bitmap Img = PageResult.BarcodeImage;
BarcodeEncoding BarcodeType = PageResult.BarcodeType;
byte [] Binary = PageResult.BinaryValue;
Console.WriteLine(PageResult.Value+" on page "+ PageNum);
}
Imports IronBarCode
Imports System
Imports System.Drawing
' Multiple barcodes may be scanned up from a single document or image. A PDF document may also be used as the input image
Private PDFResults() As PagedBarcodeResult = BarcodeReader.ReadBarcodesFromPdf("MultipleBarcodes.pdf")
' Work with the results
For Each PageResult In PDFResults
Dim Value As String = PageResult.Value
Dim PageNum As Integer = PageResult.PageNumber
Dim Img As System.Drawing.Bitmap = PageResult.BarcodeImage
Dim BarcodeType As BarcodeEncoding = PageResult.BarcodeType
Dim Binary() As Byte = PageResult.BinaryValue
Console.WriteLine(PageResult.Value &" on page " & PageNum)
Next PageResult
您将获得包含 PDF 中所有位图条形码的结果数据。
// Multi-frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
PagedBarcodeResult [] MultiFrameResults = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);
foreach (var PageResult in MultiFrameResults)
{
//...
}
// Multi-frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
PagedBarcodeResult [] MultiFrameResults = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);
foreach (var PageResult in MultiFrameResults)
{
//...
}
' Multi-frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
Dim MultiFrameResults() As PagedBarcodeResult = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels)
For Each PageResult In MultiFrameResults
'...
Next PageResult
下面的示例展示了如何从扫描的 PDF 文件中读取 QR 码和 PDF-417 条形码。我们设置了适当的条形码旋转校正和条形码图像校正级别,以轻度清洁文档,而不会因为超出我们的需求而导致显著的性能下降。
// PDF documents can also be scanned, and multiple threads will be used automatically in the background for improved performance
var ScanResults = BarcodeReader.ReadBarcodesFromPdf("Scan.pdf", BarcodeEncoding.All, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.LightlyCleanPixels);
// Work with the results
foreach (var PageResult in ScanResults)
{
string Value = PageResult.Value;
///...
}
// PDF documents can also be scanned, and multiple threads will be used automatically in the background for improved performance
var ScanResults = BarcodeReader.ReadBarcodesFromPdf("Scan.pdf", BarcodeEncoding.All, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.LightlyCleanPixels);
// Work with the results
foreach (var PageResult in ScanResults)
{
string Value = PageResult.Value;
///...
}
' PDF documents can also be scanned, and multiple threads will be used automatically in the background for improved performance
Dim ScanResults = BarcodeReader.ReadBarcodesFromPdf("Scan.pdf", BarcodeEncoding.All, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.LightlyCleanPixels)
' Work with the results
For Each PageResult In ScanResults
Dim Value As String = PageResult.Value
'''...
Next PageResult
下面的示例显示此 C# 条码库甚至可以读取损坏的条码缩略图。
条码缩略图大小的修正是自动完成的。C# 中的 IronBarcode 可使文件可读。
阅读器方法会自动检测那些小得不像是合法条形码的条形码图像,并将其放大。它们会清除与处理缩略图相关的所有数字噪音,使其恢复可读性。
// Small or 'Thumbnail' barcode images are automatically detected by IronBarcode and corrected wherever possible even if they have much digital noise.
BarcodeResult SmallResult = BarcodeReader.QuicklyReadOneBarcode("ThumbnailOfBarcode.gif", BarcodeEncoding.Code128);
// Small or 'Thumbnail' barcode images are automatically detected by IronBarcode and corrected wherever possible even if they have much digital noise.
BarcodeResult SmallResult = BarcodeReader.QuicklyReadOneBarcode("ThumbnailOfBarcode.gif", BarcodeEncoding.Code128);
' Small or 'Thumbnail' barcode images are automatically detected by IronBarcode and corrected wherever possible even if they have much digital noise.
Dim SmallResult As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("ThumbnailOfBarcode.gif", BarcodeEncoding.Code128)
在现实世界中,我们可能需要从不完美的图像中读取条形码。这些图像可能是倾斜的图像或带有数字噪音的照片。大多数开源的 .NET 条码生成和读取库都无法做到这一点。而 IronBarcode 则可以轻松地从不完美的图像中读取条形码。
我们现在来看看 ReadASingleBarcode
方法。通过其 "RotationCorrection "参数,IronBarcode 尝试去歪并从不完美的数字样本中读取条形码。
using IronBarCode;
using System;
using System.Drawing;
// All BarcodeResult.Read methods provide the developer with control to correct image and photograph correction and straightening rotation and perspective from skewed images
// * RotationCorrection e.g BarcodeReader.BarcodeRotationCorrection.Extreme un-rotates and removes perspective from barcode images.
// * ImageCorrection e.g BarcodeReader.BarcodeImageCorrection.DeepCleanPixels separates barcodes from background imagery and digital noise.
// * BarcodeEncoding e.g. BarcodeEncoding.Code128, Setting a specific Barcode format improves speed and reduces the risk of false positive results
// Example with a photo image
var PhotoResult = BarcodeReader.ReadASingleBarcode("Photo.png", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Medium, BarcodeReader.BarcodeImageCorrection.DeepCleanPixels);
string Value = PhotoResult.Value;
System.Drawing.Bitmap Img = PhotoResult.BarcodeImage;
BarcodeEncoding BarcodeType = PhotoResult.BarcodeType;
byte [] Binary = PhotoResult.BinaryValue;
Console.WriteLine(PhotoResult.Value);
using IronBarCode;
using System;
using System.Drawing;
// All BarcodeResult.Read methods provide the developer with control to correct image and photograph correction and straightening rotation and perspective from skewed images
// * RotationCorrection e.g BarcodeReader.BarcodeRotationCorrection.Extreme un-rotates and removes perspective from barcode images.
// * ImageCorrection e.g BarcodeReader.BarcodeImageCorrection.DeepCleanPixels separates barcodes from background imagery and digital noise.
// * BarcodeEncoding e.g. BarcodeEncoding.Code128, Setting a specific Barcode format improves speed and reduces the risk of false positive results
// Example with a photo image
var PhotoResult = BarcodeReader.ReadASingleBarcode("Photo.png", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Medium, BarcodeReader.BarcodeImageCorrection.DeepCleanPixels);
string Value = PhotoResult.Value;
System.Drawing.Bitmap Img = PhotoResult.BarcodeImage;
BarcodeEncoding BarcodeType = PhotoResult.BarcodeType;
byte [] Binary = PhotoResult.BinaryValue;
Console.WriteLine(PhotoResult.Value);
Imports IronBarCode
Imports System
Imports System.Drawing
' All BarcodeResult.Read methods provide the developer with control to correct image and photograph correction and straightening rotation and perspective from skewed images
' * RotationCorrection e.g BarcodeReader.BarcodeRotationCorrection.Extreme un-rotates and removes perspective from barcode images.
' * ImageCorrection e.g BarcodeReader.BarcodeImageCorrection.DeepCleanPixels separates barcodes from background imagery and digital noise.
' * BarcodeEncoding e.g. BarcodeEncoding.Code128, Setting a specific Barcode format improves speed and reduces the risk of false positive results
' Example with a photo image
Private PhotoResult = BarcodeReader.ReadASingleBarcode("Photo.png", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Medium, BarcodeReader.BarcodeImageCorrection.DeepCleanPixels)
Private Value As String = PhotoResult.Value
Private Img As System.Drawing.Bitmap = PhotoResult.BarcodeImage
Private BarcodeType As BarcodeEncoding = PhotoResult.BarcodeType
Private Binary() As Byte = PhotoResult.BinaryValue
Console.WriteLine(PhotoResult.Value)
IronBarcode 还可以同时读取多个条码。当我们创建一个文档列表,并使用条码阅读器读取众多文档时,IronBarcode 能带来更好的效果。条码扫描过程中的 "ReadBarcodesMultithreaded "方法会使用多个线程,并有可能使用 CPU 的所有内核,这比一次读取一个条码的速度要快得多。
// The BarcodeResult.ReadBarcodesMultiThreaded method allows for faster barcode scanning of multiple images or PDFs. All threads are automatically managed by IronBarcode.
var ListOfDocuments = new [] { "Image1.png", "image2.JPG", "image3.pdf" };
PagedBarcodeResult [] BatchResults = BarcodeReader.ReadBarcodesMultiThreaded(ListOfDocuments);
// Work with the results
foreach (var Result in BatchResults)
{
string Value = Result.Value;
//...
}
// The BarcodeResult.ReadBarcodesMultiThreaded method allows for faster barcode scanning of multiple images or PDFs. All threads are automatically managed by IronBarcode.
var ListOfDocuments = new [] { "Image1.png", "image2.JPG", "image3.pdf" };
PagedBarcodeResult [] BatchResults = BarcodeReader.ReadBarcodesMultiThreaded(ListOfDocuments);
// Work with the results
foreach (var Result in BatchResults)
{
string Value = Result.Value;
//...
}
' The BarcodeResult.ReadBarcodesMultiThreaded method allows for faster barcode scanning of multiple images or PDFs. All threads are automatically managed by IronBarcode.
Dim ListOfDocuments = { "Image1.png", "image2.JPG", "image3.pdf" }
Dim BatchResults() As PagedBarcodeResult = BarcodeReader.ReadBarcodesMultiThreaded(ListOfDocuments)
' Work with the results
For Each Result In BatchResults
Dim Value As String = Result.Value
'...
Next Result
要读取二维码文件,请在控制器中添加一个 ViewFile
操作方法,如下所示。
public ActionResult ViewFile()
{
List<KeyValuePair<string, string>> fileData = new List<KeyValuePair<string, string>>();
KeyValuePair<string, string> data;
string [] files = Directory.GetFiles(Server.MapPath("~/qrr"));
foreach (string file in files)
{
// create a barcode reader instance
IBarcodeReader reader = new BarcodeReader();
// load a bitmap
var barcodeBitmap = (Bitmap)Image.FromFile(Server.MapPath("~/qrr") + "/" + Path.GetFileName(file));
// detect and decode the barcode inside the bitmap
var result = reader.Decode(barcodeBitmap);
// do something with the result
data = new KeyValuePair<string, string>(result.ToString(), "/QR/" + Path.GetFileName(file));
fileData.Add(data);
}
return View(fileData);
}
public ActionResult ViewFile()
{
List<KeyValuePair<string, string>> fileData = new List<KeyValuePair<string, string>>();
KeyValuePair<string, string> data;
string [] files = Directory.GetFiles(Server.MapPath("~/qrr"));
foreach (string file in files)
{
// create a barcode reader instance
IBarcodeReader reader = new BarcodeReader();
// load a bitmap
var barcodeBitmap = (Bitmap)Image.FromFile(Server.MapPath("~/qrr") + "/" + Path.GetFileName(file));
// detect and decode the barcode inside the bitmap
var result = reader.Decode(barcodeBitmap);
// do something with the result
data = new KeyValuePair<string, string>(result.ToString(), "/QR/" + Path.GetFileName(file));
fileData.Add(data);
}
return View(fileData);
}
Public Function ViewFile() As ActionResult
Dim fileData As New List(Of KeyValuePair(Of String, String))()
Dim data As KeyValuePair(Of String, String)
Dim files() As String = Directory.GetFiles(Server.MapPath("~/qrr"))
For Each file As String In files
' create a barcode reader instance
Dim reader As IBarcodeReader = New BarcodeReader()
' load a bitmap
Dim barcodeBitmap = CType(Image.FromFile(Server.MapPath("~/qrr") & "/" & Path.GetFileName(file)), Bitmap)
' detect and decode the barcode inside the bitmap
Dim result = reader.Decode(barcodeBitmap)
' do something with the result
data = New KeyValuePair(Of String, String)(result.ToString(), "/QR/" & Path.GetFileName(file))
fileData.Add(data)
Next file
Return View(fileData)
End Function
// create a barcode reader instance
BarcodeReader reader = new BarcodeReader();
// load a bitmap
var barcodeBitmap = (Bitmap)Image.LoadFrom("C:\\sample-barcode-image.png");
// detect and decode the barcode inside the bitmap
var result = reader.Decode(barcodeBitmap);
// do something with the result
if (result != null)
{
txtDecoderType.Text = result.BarcodeFormat.ToString();
txtDecoderContent.Text = result.Text;
}
// create a barcode reader instance
BarcodeReader reader = new BarcodeReader();
// load a bitmap
var barcodeBitmap = (Bitmap)Image.LoadFrom("C:\\sample-barcode-image.png");
// detect and decode the barcode inside the bitmap
var result = reader.Decode(barcodeBitmap);
// do something with the result
if (result != null)
{
txtDecoderType.Text = result.BarcodeFormat.ToString();
txtDecoderContent.Text = result.Text;
}
' create a barcode reader instance
Dim reader As New BarcodeReader()
' load a bitmap
Dim barcodeBitmap = CType(Image.LoadFrom("C:\sample-barcode-image.png"), Bitmap)
' detect and decode the barcode inside the bitmap
Dim result = reader.Decode(barcodeBitmap)
' do something with the result
If result IsNot Nothing Then
txtDecoderType.Text = result.BarcodeFormat.ToString()
txtDecoderContent.Text = result.Text
End If
ZXing Decoder Online 是一款支持解码的在线条形码和二维码扫描器。上传 PNG 或其他格式的二维码图像,它就会开始解码。同样,您也可以为任何数据生成二维码。大多数情况下,这些信息将是您希望编码到 QR 代码中的 URL 或文本。
导航至 ZXing Decoder 网站。
ZXing.NET 库是一个免费的开源库,允许您构建条形码读取应用程序,但其基于 Apache 许可构建,不允许将其用于商业目的。
IronBarcode 的开发者许可证是免费提供的。IronBarcode 有一个独特的定价方案:Lite bundle 起价为 $749,没有额外费用。SaaS 和 OEM 项目也可再次分发。每个许可证包括永久许可证、开发/分期/生产有效性、30 天退款保证以及一年的软件支持和升级。 (一次性购买).访问此处 页码 查看 IronBarcode 的完整定价和许可证信息。
IronBarcode 包含一个易于使用的 API,供开发人员在.NET 中读写条形码,从而优化了准确性,并降低了实际应用中的错误率。
例如,"BarcodeWriter "类将验证和纠正 UPCA 和 UPCE 条形码上的 "校验和"。它还会对太短而无法输入特定数字格式的数字进行 "零填充"。如果您的数据与指定的数据格式不兼容,IronBarcode 将通知开发者一个更合适的条形码格式供其使用。
IronBarcode 擅长于读取已扫描条形码或从照片图像中读取的条形码,换句话说,当图像不是完美的图形,也不是机器生成的屏幕截图时。
IronBarcode 是在 ZXing.NET 的基础上开发的。 (斑马线) ZXing.NET 核心库的处理能力有所提高。与 ZXing.NET 核心库相比,它具有易于使用的 API 和较低的错误率。不仅如此,IronBarcode 还支持比一般 ZXing.NET 库更多的条形码格式。
IronBarcode 是 ZXing.NET 的改进版,为用户提供了商业用途平台,并可在多个平台上使用同一软件包。它还拥有全面的技术支持,随时为您提供所需的帮助。
IronBarcode 包括自动旋转、透视校正和数字噪声校正,并能检测图像中编码的条形码类型。
总之,IronBarcode 是一款多功能的 .NET 软件库和 C# QR 码生成器,可用于读取各种条形码格式,无论是屏幕截图、照片、扫描还是其他不完美的真实世界图像。
IronBarcode 是创建和识别条形码最有效的库之一。在创建和识别条形码方面,它也是最快速的库之一。该库兼容不同的操作系统。它易于设计,支持多种条形码格式。此外,还支持各种符号、格式和字符。
ZXing.NET 条形码是一个功能强大的库,可生成和识别各种图像格式的条形码。我们可以读取和创建各种格式的图像。ZXing.NET 还允许您改变条形码的外观,改变其高度、宽度、条形码文本等。
与 ZXing.NET 相比,IronBarcode 软件包提供可靠的许可和支持。IronBarcode 的价格为 $749。虽然 ZXing 是免费的,但它不能用于商业用途,也缺乏全面的支持。除了比 ZXing.NET 更灵活之外,IronBarcode 解决方案还具有更多功能。由此可见,IronBarcode 比 ZXing.NET 更具优势。
如果比较识别和生成条形码的处理时间,IronBarcode 优于 ZXing.NET。IronBarcode 还具有多种属性,允许我们从不同的图像格式和 PDF 文档中读取条形码。它还允许我们在条形码或 QR 码中包含图像,这是其他任何库都不具备的。
IronBarcode 在早期开发阶段是免费的。您可以获得 免费试用 用于生产或商业用途。根据开发人员的要求,IronBarcode 提供三种价格等级。您可以选择最能满足您需求的解决方案。现在,您只需购买两套 Iron 软件产品,即可获得一套五套 Iron 软件产品。请访问 网站 如需更多信息,请联系