在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
條碼掃描器可能不總是適用於我們的應用程式。 您可能已經有條碼的數位圖像,並想知道它在英語文本中代表什麼。 此外,此掃描器僅能讀取一維條碼,這些條碼包含的數據有限,並且只能用於 Windows RT 類庫。 二维条码(也稱為 QR 碼)現在很常見,並且可以容納更多資訊。
可以透過簡單的 API 呼叫和幾個編碼步驟來創建一個基於 C# 的應用程式以讀取條碼。 受 .NET 支援的應用程式可以在 Windows、macOS 或 Linux 上運行,而不依賴任何第三方工具或 API。
本文將比較兩個最強大的 .NET Core 應用程式庫,用於以程式化方式讀取條碼。 這兩個庫是 IronBarcode 和 ZXing.NET。 我們將了解 IronBarcode 為什麼比 ZXing.NET 更強大和穩健。
BarcodeWriterPixelData
類別來自訂條碼格式
屬性QrCodeEncodingOptions
進一步自訂高度、寬度和邊距的類別寫
步驟2中的物件方法ZXing.NET 是一個用於解碼和生成條碼的庫(像 QR Code、PDF 417、EAN、UPC、Aztec、Data Matrix、Codabar). ZXing (zebra crossing的縮寫) 是一個基於Java的開源庫,支援多種一維和二維條碼格式。
其基本特徵如下:
IronBarcode 是一個 C# 函式庫,允許程式設計師讀取和生成條碼。 作為領先的條碼庫,IronBarcode 支援多種一維和二維條碼,包括裝飾條碼。(彩色和品牌)QR碼。 它支援 .NET Standard 和 Core 版本 2 及更高版本,可在 Azure、Linux、macOS、Windows 和網路上跨平台使用。 IronBarcode 是一個知名的 .NET 系統類別庫或元件,允許 C#、VB.NET 和 F# 開發人員使用標準化的編程語言進行工作。 它將使客戶能夠瀏覽掃描標籤並創建新的標準化標籤。 它在處理二维條碼和其他三维标准化條碼時表現極其出色。
IronBarcode 現在支援二維條碼。 它提供了優化這些代碼的顏色、樣式和像素化的功能,並且能夠增加標誌,以用於印刷或廣告材料。 這個庫還可以讀取傾斜和變形的條碼,這是其他條碼軟體可能無法讀取的。
要使用 ZXing.NET 庫,請在您的 ASP.NET Core 應用程序中使用 NuGet 套件管理器主控台安裝以下兩個套件:
Install-Package ZXing.Net
Install-Package ZXing.Net.Bindings.CoreCompat.System.Drawing -Version 0.16.5-beta
或者,使用 NuGet 套件管理器在您的專案中安裝 ZXing.NET。 要這樣做,請前往 工具 > NuGet 套件管理員 > 管理 NuGet 套件以解決方案...,然後切換到“瀏覽”標籤,搜尋“ZXing.NET”。
使用 NuGet 套件管理器安裝 IronBarcode,或直接從網站下載 DLL。產品網站. 這IronBarcode命名空間包含所有 IronBarcode 類別。
可以使用 Visual Studio 的 NuGet 套件管理器安裝 IronBarcode,套件名稱為 "Barcode"。
Install-Package BarCode
首先,在專案檔案的根資料夾中創建一個名為「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;
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
視圖並在其中包含以下程式碼。 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>
// 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 Code 將被生成並儲存為 .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 碼的像素和複雜性增加。 在下圖中,我們可以看到顯示的QR碼檔案。
我們首先從 IronBarcode.BarcodeWriterEncoding
列舉中指定條碼的值和條碼格式。 然後我們可以將其保存為圖像、System.Drawing.Image
或位圖代碼對象。 這就是您所需的全部源代碼。!
// 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 碼,例如將標誌圖形放置在圖像中心的網格上。 它也可以被設計成符合特定品牌或圖形識別的顏色。
在測試中,請在下面的代碼範例中創建一個標誌,並了解使用 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()
因此,System.Drawing.Image
顯示的 PDF417 條碼如下圖所示:
當您將 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# 條碼庫甚至可以讀取受損的條碼縮圖。
條碼縮圖大小的校正是自動完成的。 IronBarcode 在 C# 中使文件可讀。
讀取方法會自動檢測過小而不被視為合法條碼的條碼圖像,並對其進行放大。 他們清除所有與處理縮略圖相關的數位噪音,使其再次可讀。
// 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
方法。 IronBarcode透過其 RotationCorrection
參數嘗試矯正傾斜並從不完美的數位樣本中讀取條碼。
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
要讀取 QR 碼文件,請在您的控制器中添加一個 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 是一個可用於在線解碼的條碼和 QR 碼掃描器。 上傳 PNG 或其他格式的 QR 碼圖片,系統將開始解碼。 同樣地,您可以為任何數據生成二維碼。 大多數情況下,該信息將是您想在QR碼中編碼的URL或文字。
導航至ZXing解碼器網站。
ZXing.NET庫是一個免費的開源庫,允許您構建條碼閱讀應用程式,但它是基於Apache許可證構建的,不允許自由用於商業用途。
IronBarcode 的開發者許可證免費提供。 IronBarcode 擁有獨特的定價方案:Lite 套組起價 $749,無其他額外費用。 SaaS 和 OEM 項目也可以再次分發。 每個許可證都包括永久許可證、開發/測試/生產的有效性、30天退款保證以及一年的軟體支援與升級。(一次性購買). 訪問此頁面頁面查看IronBarcode的完整定價和許可資訊。
IronBarcode 包含一個易於使用的 API,供開發人員在 .NET 中讀取和寫入條碼,這在實際使用情況下優化了準確性和低錯誤率。
例如,BarcodeWriter
類別會驗證並校正 UPCA 和 UPCE 條碼上的「校驗碼」。 它還會對太短以至於無法輸入特定數字格式的數字進行補零。 如果您的數據與指定的數據格式不兼容,IronBarcode 會通知開發者可以使用的更合適的條碼格式。
IronBarcode 在读取条码时表现出色,特别是当条码是通过扫描或从摄影图像中读取时,换句话说,即使图像在图形上不完美,也不是机器生成的截图。
IronBarcode 是基於 ZXing.NET 构建的(斑馬線)核心,具備更強的處理能力。 它配備了易於使用的 API,相較於 ZXing.NET 核心庫,錯誤率較低。 不僅如此,IronBarcode 還支持更廣泛的條碼格式,比一般的 ZXing.NET 庫支持的格式更多。
IronBarcode 是 ZXing.NET 的改進版本,為用戶提供商業用途的平台,並且可以在多個平台上使用相同的套件。 它還提供完善的技術支持,隨時準備在您需要的地方提供幫助。
IronBarcode包括自動旋轉、透視校正和數位噪聲校正,並且可以檢測圖像中條碼的類型。
總之,IronBarcode 是一個多功能的 .NET 軟體庫和 C# 二維碼生成器,用於讀取各種條碼格式,無論是截圖、照片、掃描還是其他不完美的現實圖像。
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 Software產品的價格獲得五個Iron Software產品的套裝。 訪問此頁面網站如需更多資訊。