跳過到頁腳內容
與其他組件的比較
IronBarcode 和 ZXing.NET 庫之间的比较

IronBarcode與ZXing.NET的比較

條碼掃描器可能不適合我們所有的應用。 您可能已經有一張條碼的數位圖像,想了解它在英語文本中表示什麼。 此外,這款掃描器僅能閱讀1D條碼,包含有限數據量,且只能在Windows RT類庫中使用。 2D條碼(也稱為QR碼)現在很常見,可以容納更多信息。

可以使用簡單的API調用和一些編碼步驟來創建一個基於C#的應用程序來閱讀條碼。 .NET支持的應用程序可在Windows、macOS或Linux上運行,而無需依賴任何第三方工具或API。

本文將比較兩個最強大的.NET Core應用程式庫,用於程式化閱讀條碼。 這兩個庫是IronBarcode和ZXing.NET。 我們將了解IronBarcode比ZXing.NET更強大和穩健的原因。


什麼是ZXing.NET

ZXing.NET是一個用於解碼和生成條碼的庫(如QR碼、PDF 417、EAN、UPC、Aztec、Data Matrix、Codabar)。 ZXing,代表"zebra crossing",是一個基於Java的開源庫,支持多種1D和2D條碼格式。

其基本特點如下:

  • 能夠存儲網址、聯繫信息、日曆事件等信息
  • 專為Java SE應用程序量身打造
  • 允許通過intent集成條碼掃描器
  • 是一個簡單的Google Glass應用程序

什麼是IronBarcode

IronBarcode是一個C#庫,允許程序員閱讀和生成條碼。 作為領先的條碼庫,IronBarcode支持各種一維和二維條碼,包括裝飾性(彩色和品牌化)QR碼。 它支持.NET Standard和Core版本2及更高版本,允許在Azure、Linux、macOS、Windows和Web上進行跨平台使用。 IronBarcode是.NET系統中一個著名的類庫或組件,允許C#,VB.NET和F#開發者使用標準化編程語言。 它允許客戶瀏覽掃描器標籤並創建新的標準化標籤。 它在2D條碼和其他3D標準化條碼中表現出色。

IronBarcode現在支持2D條碼。 它提供了優化這些代碼的著色、樣式和像素化的功能,還可以將標誌添加到它們中,用於印刷或廣告材料中。 此庫還可以閱讀偏斜和變形的條碼,這是其他條碼軟件可能無法閱讀的。

安裝IronBarcode和ZXing.NET

安裝ZXing.NET

要使用ZXing.NET庫,請在您的ASP.NET Core應用中使用NuGet Package Manager Console安裝以下兩個包:

1. ZXing.Net

Install-Package ZXing.Net

2. ZXing.Net.Bindings.CoreCompat.System.Drawing

Install-Package ZXing.Net.Bindings.CoreCompat.System.Drawing -Version 0.16.5-beta

或者,可以使用NuGet Package Manager在您的項目中安裝ZXing.NET。 要這樣做,請轉到工具 > NuGet包管理器 > 管理解決方案的NuGet包...,然後切換到"瀏覽"選項卡並搜索"ZXing.NET"。

A Comparison between IronBarcode and Zxing.NET, Figure 1: ASP.NET Web應用程序

ASP.NET Web應用程序

安裝IronBarcode

使用NuGet包管理器安裝IronBarcode或直接從產品網站下載DLL。 IronBarcode 命名空間包含所有IronBarcode類。

IronBarcode可以使用Visual Studio的NuGet包管理器安裝:包名為"Barcode"。

Install-Package BarCode

創建2D條碼

使用ZXing.NET

首先,在項目文件的根文件夾中創建一個名為'qrr'的新文件夾。

然後,我們將創建QR文件,並將圖像系統文件存儲在'qrr'文件夾中。

在控制器中,按照源代碼中的示例添加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;

    // BarcodeWriterPixelData acts as a QR code generator
    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.Scan0, 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;

    // BarcodeWriterPixelData acts as a QR code generator
    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.Scan0, 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);
}
$vbLabelText   $csharpLabel

唯一剩下的更改是將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);
$vbLabelText   $csharpLabel

接下來,您必須創建GenerateFile視圖並在其中包含以下代碼。 GenerateFile 視圖與Index視圖相同。

@model Byte []
@using (Html.BeginForm(null, null, FormMethod.Post))
{
    <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>
}
@{
    if (Model != null)
    {
        <h3>QR Code Successfully Generated</h3>
        <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>
                <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 src="@String.Format("data:image/png;base64,{0}", Convert.ToBase64String(Model))" />
    }
}
$vbLabelText   $csharpLabel

在文本框中輸入任何值,然後單擊"提交"按鈕。 QR碼將被生成並作為.PNG文件保存在'qrr'文件夾中。

A Comparison between IronBarcode and ZXing.NET, Figure 2: QR碼生成器

QR碼生成器

A Comparison between IronBarcode and ZXing.NET, Figure 3: 顯示的QR碼文件

顯示的QR碼文件

支持的條碼格式

IronBarcode支持多種常用的條碼格式,包括:

  • 帶有標誌和顏色的QR碼(包括裝飾和品牌化的代碼)
  • 多格式條碼,包括Aztec、Data Matrix、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是傳統的數字條碼格式。

創建和保存條碼

using IronBarCode;
// Create a barcode and save it in various formats
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");

// Convert barcode to different image formats and obtain binary data
System.Drawing.Image MyBarCodeImage = MyBarCode.Image;
System.Drawing.Bitmap MyBarCodeBitmap = MyBarCode.ToBitmap();
string DataURL = MyBarCode.ToDataUrl();
string ImgTagForHTML = MyBarCode.ToHtmlTag();
byte[] PngBytes = MyBarCode.ToPngBinaryData();

// Save barcode in PDF stream
using (System.IO.Stream PdfStream = MyBarCode.ToPdfStream())
{
    // The Stream barcode image output also works for GIF, JPEG, PDF, PNG, BMP and TIFF
}

// Stamp barcode onto an existing PDF at a specific position
MyBarCode.StampToExistingPdfPage("ExistingPDF.pdf", 1, 200, 50);
using IronBarCode;
// Create a barcode and save it in various formats
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");

// Convert barcode to different image formats and obtain binary data
System.Drawing.Image MyBarCodeImage = MyBarCode.Image;
System.Drawing.Bitmap MyBarCodeBitmap = MyBarCode.ToBitmap();
string DataURL = MyBarCode.ToDataUrl();
string ImgTagForHTML = MyBarCode.ToHtmlTag();
byte[] PngBytes = MyBarCode.ToPngBinaryData();

// Save barcode in PDF stream
using (System.IO.Stream PdfStream = MyBarCode.ToPdfStream())
{
    // The Stream barcode image output also works for GIF, JPEG, PDF, PNG, BMP and TIFF
}

// Stamp barcode onto an existing PDF at a specific position
MyBarCode.StampToExistingPdfPage("ExistingPDF.pdf", 1, 200, 50);
$vbLabelText   $csharpLabel

另一方面,ZXing是一個基於Java的開源1D/2D條碼圖像處理庫。 支持UPC-A、UPC-E、EAN-8、Code 93、Code 128、QR Code、Data Matrix、Aztec、PDF 417和其他條碼格式。

A Comparison between IronBarcode and Zxing.NET, Figure 4: 支持的條碼格式

支持的條碼格式

使用IronBarcode創建QR Code文件

要使用IronBarcode創建QR碼,我們可以使用BarcodeWriter類。 此類引入了一些新穎且有趣的功能來創建QR碼。 它使我們能夠設置QR錯誤校正級別,允許您在QR碼的大小和可讀性之間取得平衡。

using IronBarCode;
// Generate a simple QR Code image and save as PNG
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("MyQR.png");
using IronBarCode;
// Generate a simple QR Code image and save as PNG
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("MyQR.png");
$vbLabelText   $csharpLabel

錯誤校正允許我們指定在真實世界情境中,QR碼的可讀性。 更高級別的錯誤校正會導致更大的QR碼,並帶來更多的像素和複雜性。 在下圖中,我們看到QR碼文件顯示。

A Comparison between IronBarcode and ZXing.NET, Figure 5: 支持的條碼格式

QR碼圖像

我們首先從IronBarCode.BarcodeWriterEncoding枚舉中指定條碼的值和條碼格式。 然後我們可以將其保存為圖像、一個System.Drawing.Image或位圖代碼對象。

using IronBarCode;
using System.Diagnostics;

// Generate a simple BarCode image and save as PNG using the following namespaces
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
Process.Start("MyBarCode.png");
using IronBarCode;
using System.Diagnostics;

// Generate a simple BarCode image and save as PNG using the following namespaces
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
Process.Start("MyBarCode.png");
$vbLabelText   $csharpLabel
A Comparison between IronBarcode and ZXing.NET, Figure 6: 在C#範例中創建條碼圖像

在C#範例中創建條碼圖像

IronBarcode還支持樣式化QR碼,例如將標誌圖像置於網格的正中心,並固定它。 它也可以用於匹配特定品牌或圖形識別的顏色。

為測試,請在下面的代碼示例中創建一個標誌,看看QRCodeWriter.CreateQRCodeWithLogo方法的使用有多簡單。

using IronBarCode;

// Adding a Logo
var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "visual-studio-logo.png", 500);
MyQRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);
using IronBarCode;

// Adding a Logo
var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "visual-studio-logo.png", 500);
MyQRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);
$vbLabelText   $csharpLabel
A Comparison between IronBarcode and ZXing.NET, Figure 7: 創建帶有標誌圖像的QR Code

創建帶有標誌圖像的QR Code

最後,我們將生成的QR碼保存為PDF文件。為方便起見,最後一行代碼將QR碼保存為HTML文件。

using IronBarCode;

// 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");
using IronBarCode;

// 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");
$vbLabelText   $csharpLabel

下方我們可以看到如何用一行代碼創建、樣式化並匯出條碼。

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();
$vbLabelText   $csharpLabel

結果,System.Drawing.Image的一個PDF417條碼如下面所示:

A Comparison between IronBarcode and ZXing.NET, Figure 8: 簡單、流暢的PDF417條碼生成在C#中

簡單、流暢的PDF417條碼生成在C#中

閱讀QR碼文件

使用IronBarcode閱讀QR碼

當您將IronBarcode類庫與.NET條碼閱讀器結合使用時,閱讀條碼或QR碼變得非常簡單。 在我們的第一個示例中,我們可以看到如何使用單行代碼來閱讀條碼。

A Comparison between IronBarcode and ZXing.NET, Figure 9: 待掃描的Code128條碼圖像與C#一起使用

待掃描的Code128條碼圖像與C#一起使用

我們可以獲取條碼的值、圖像、編碼類型和二進制數據(如有),然後將其輸出到控制台。

using IronBarCode;
using System;

// Read a barcode or QR code from an image
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;

// Read a barcode or QR code from an image
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);
}
$vbLabelText   $csharpLabel

閱讀PDF內的條碼

我們將看看如何在幾行代碼中閱讀掃描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);
}
$vbLabelText   $csharpLabel

您將獲得包含PDF中所有位圖條碼的結果數據。

A Comparison between IronBarcode and ZXing.NET, Figure 10: 閱讀儲存在PDF中的條碼結果

閱讀儲存在PDF中的條碼結果

從GIF和TIFF中閱讀條碼

using IronBarCode;
using System;

// 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)
{
    // Process each page result
}
using IronBarCode;
using System;

// 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)
{
    // Process each page result
}
$vbLabelText   $csharpLabel
A Comparison between IronBarcode and ZXing.NET, Figure 11: 從多幀TIFF圖像中閱讀條碼

從多幀TIFF圖像中閱讀條碼

下面的示例展示了如何從掃描的PDF中閱讀QR碼和PDF-417條碼。 我們設置了一個合適的條碼旋轉校正和條碼圖像校正級別,以輕微清潔文件而不對性能造成顯著影響。

using IronBarCode;
using System;

// 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;
    //...
}
using IronBarCode;
using System;

// 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;
    //...
}
$vbLabelText   $csharpLabel
A Comparison between IronBarcode and ZXing.NET, Figure 12: 從掃描的PDF文件中閱讀條碼

從掃描的PDF文件中閱讀條碼

從位圖圖像中閱讀損壞的QR碼

下面的示例顯示了此C#條碼庫甚至可以閱讀損壞的條碼縮略圖。

條碼縮略圖的大小校正是自動完成的。 C#中的IronBarcode使文件可讀。

閱讀器方法自動檢測條碼圖像過小,無法成為合法條碼,並將其放大。 它們清理了所有與縮略圖相關的數位噪音,使它們再次可讀。

using 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);
using 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);
$vbLabelText   $csharpLabel
A Comparison between IronBarcode and ZXing.NET, Figure 13: 自動條碼縮略圖大小校正

自動條碼縮略圖大小校正

從不完美的圖像中閱讀條碼

在真實世界情境中,我们可能想要从不完美的图像中读取条码。 它们可能是倾斜的图像或含有数字噪声的照片。对于大多数开源 .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);
$vbLabelText   $csharpLabel
A Comparison between IronBarcode and ZXing.NET, Figure 14: 从手机摄像头读取条码

从手机摄像头读取条码

IronBarcode 还可以同时读取多个条码。当我们创建文档列表并使用条码读取器来读取多个文档时,我们从 IronBarcode 得到了更好的结果。 条码扫描过程的ReadBarcodesMultithreaded方法使用多个线程,并可能使用您 CPU 的所有内核,这可能比一次读取一个条码要快得多。

using IronBarCode;

// 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;
    //...
}
using IronBarCode;

// 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;
    //...
}
$vbLabelText   $csharpLabel

使用 ZXing.NET 阅读和解码 QR 代码

要阅读 QR 代码文件,请在您的控制器中添加一个 ViewFile action 方法,如下所示。

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);
}
$vbLabelText   $csharpLabel
A Comparison between IronBarcode and ZXing.NET, Figure 15: 阅读和解码 QR 代码

阅读和解码 QR 代码

A Comparison between IronBarcode and ZXing.NET, Figure 16: 解码 QR 代码

解码 QR 代码

解码位图中的条码

using ZXing;
using System.Drawing;

// Create a barcode reader instance
BarcodeReader reader = new BarcodeReader();

// Load a bitmap
var barcodeBitmap = (Bitmap)Image.FromFile("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;
}
using ZXing;
using System.Drawing;

// Create a barcode reader instance
BarcodeReader reader = new BarcodeReader();

// Load a bitmap
var barcodeBitmap = (Bitmap)Image.FromFile("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;
}
$vbLabelText   $csharpLabel

ZXing 在线解码器

ZXing 在线解码器是一个在线可用的条码和 QR 代码扫描器,支持解码。 上传 PNG 格式的 QR 代码图像,它将开始解码。 同样,您可以为任何数据生成 QR 代码。 大多数情况下,这些信息将是您想要在 QR 代码中编码的 URL 或文本。

转到 ZXing 解码器网站。

A Comparison between IronBarcode and ZXing.NET, Figure 17: 解码 QR 代码

ZXing 解码器网站

A Comparison between IronBarcode and ZXing.NET, Figure 18: 解码 QR 代码

ZXing 解码结果

定价与授权

ZXing.NET 库是一个免费的开源库,允许您构建条码阅读应用程序,获 Apache 许可证 2.0 授权,其允许免费商业使用,仅需适当的归属。

IronBarcode 为开发者提供免費的许可。 IronBarcode 的定价方案独特:Lite捆绑包从 $liteLicense 开始,没有额外费用。 SaaS 和 OEM 项目也可以再次分发。 每个许可包括永久授權、開發/試驗/生產環境有效性、30天退款保證,以及一年期的軟體支援和升级(一次性購買)。 访问此頁面查看IronBarcode的完整定價和許可資訊。

為何選擇IronBarcode?

IronBarcode 包含一个便于开发者在 .NET 中读取和写入条码的API,能够优化精度并在真实情境中保持低误码率。

例如,BarcodeWriter類會驗證和更正UPCA和UPCE條碼上的"校驗和"。 它還會將過短的數字"填零",以轉換為特定數字格式。 如果您的數據與指定的數據格式不兼容,IronBarcode 將通知開發人員可能使用的更合適的條碼格式。

當條碼被掃描或读取自一张摄影图像时(即图像不是完美的图形,且不是机器生成的截图),IronBarcode 擅长读取条码。

IronBarcode与ZXing.NET的不同之处?

IronBarcode由ZXing.NET(Zebra Crossing)核心构建,处理能力更强。 与ZXing.NET核心库相比,它的API易于使用且错误率较低。 不仅如此,IronBarcode还支持比通常的ZXing.NET库更多的条码格式。

IronBarcode是一个改进版的ZXing.NET,为用户提供商业使用平台,并可以在多个平台上使用相同的包。 此外,它还提供全面的技术支持,随时准备协助您。

IronBarcode包括自动旋转、透视校正及数字噪声校正,并可识别图像中的条码类型。

結論

總之,IronBarcode是一個多功能的.NET软件库和C# QR代码生成器,可用于读取各种条码格式,无论它们是截圖、照片、扫描或其他不完美的真实图像。

IronBarcode是生成和识别条码的最有效库之一。 在创建和识别条码方面,它也是最迅速的库之一。 该库与不同的操作系統兼容。 设计简单,并支持各种條碼格式。 此外,它支持多种符号、格式和字符。

ZXing.NET條碼是一個功能強大的庫,可以在各種圖像格式中生成和识别條碼。 我們可以以各種格式讀取和創建圖像。 ZXing.NET還允许您更改條碼的外觀,例如更改其高度、寬度、條碼文本等。

与ZXing.NET相比,IronBarcode套件提供了可靠的授权和支持。 IronBarcode价格为$liteLicense。 尽管ZXing是免费和开源的,IronBarcode提供全面的商业支持和专业维护。 除了比ZXing.NET更灵活,Iron Barcode解决方案还具有更多功能。 因此,很明显IronBarcode比ZXing.NET具有很大的优势。

在识别和生成条码的处理时间方面,IronBarcode优于ZXing.NET。 IronBarcode还具有多项特性,允许我们从不同的图像格式和PDF文件中读取条码。 它还允许我们在条码或QR码中包含图像,这是其他任何库中都没有的。

IronBarcode在开发的早期階段是免費的。 您可以獲得一個免費試用,用于生产级别或商业用途。 根據開發者的需求,IronBarcode提供三種定價方案。 您可以選擇最能滿足您需求的解決方案。 現在,您可以以兩個Iron Software項目的價格獲得五個Iron Software產品套件。 訪問此網站以獲取更多信息。

ZXing.NET是其各自所有者的註冊商標。 本网站与ZXing.NET无关、未经其认可或赞助。 所有产品名称、标志和品牌均属于其各自所有者的财产。 比较仅供信息参考,反映了撰写时可公开获得的信息。)]

常見問題解答

我怎样才能在 C# 中生成 QR 碼?

您可以通過利用 IronBarcode 的 `BarcodeWriter` 类在 C# 中生成 QR 碼。只需定义 QR 碼內容,自定义選項如颜色和大小,然後調用 `Write` 方法生成 QR 碼。

是什么讓 IronBarcode 比 ZXing.NET 更加稳健?

IronBarcode 提供增強的功能,如支持更多的條形碼格式,先進的图像處理功能如旋轉和透视校正,以及创建帶有標识和颜色的装饰 QR 碼的能力。它还具有跨平台兼容性,并提供商业支持。

我可以使用 IronBarcode 從有缺陷的图像中读取條形碼嗎?

可以,IronBarcode 可以使用旋轉校正、透视校正和數字噪声减少等功能從有缺陷的图像中读取條形碼,非常適合扫描或摄影图像。

在 C# 中读取條形碼的步骤是什么?

要在 C# 中读取條形碼,使用 IronBarcode 的 `BarcodeReader` 类。使用 `BarcodeReader.QuicklyReadOneBarcode` 方法加载包含條形碼的图像,該方法将解碼并返回條形碼內容(如果识别成功)。

IronBarcode 如何處理跨平台兼容性?

IronBarcode 支持 Windows、macOS、Linux 和 Azure 的跨平台兼容性,允許您将條形碼生成和读取功能集成到各种 .NET 應用程序中,無需第三方依赖。

有哪些選項可用于使用 IronBarcode 自定义 QR 碼?

IronBarcode 允許通過調整颜色、添加品牌標识和配置纠錯级别以平衡大小和可读性来自定义 QR 碼,為各种美学和功能需求提供灵活性。

在商业應用中使用 IronBarcode 是否需要支付許可费用?

IronBarcode 提供不同的許可選項,包括適合于商业應用的永久許可。这与 ZXing.NET 免费但對商业使用有限制形成對比。

在使用條形碼庫時有哪些常见的故障排除场景?

常见問题可能包括由于图像质量差导致的錯误條形碼识别、不支持的條形碼格式或配置錯误。IronBarcode 的高级图像處理功能可以帮助缓解一些识别問题。

為什么有人可能会選择 IronBarcode 而不是 ZXing.NET 進行條形碼處理?

IronBarcode 提供更稳健的 API,更好的錯误處理,廣泛的條形碼格式支持以及生成個性化 QR 碼的功能,还附帶商业許可和專业支持的好處。

如何在 .NET 項目中安装 IronBarcode?

您可以使用 Visual Studio 中的 NuGet 包管理器在 .NET 項目中安装 IronBarcode,通過搜索 'IronBarcode' 并将其添加到您的項目中,确保轻松集成和更新。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担產品测测试,產品開發和研究的责任時,Jordi 為持续的產品改進增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我