Kody kreskowe i QR w aplikacjach C# i VB.NET
Odczytywanie i zapisywanie kodów kreskowych w C# oraz we wszystkich innych językach .NET jest prostym procesem dzięki bibliotece IronBarcode.
Instalacja IronBarcode
Pierwszym krokiem jest instalacja IronBarcode, którą można przeprowadzić pobierając pakiet z NuGet lub pobierając plik DLL.
Aby zainstalować pakiet NuGet IronBarcode, można użyć Menedżera pakietów NuGet w Visual Studio:
Install-Package BarCode
Można również przeprowadzić instalację za pomocą interfejsu wiersza poleceń dotnet:
dotnet add package IronBarCode
Odczytywanie kodów kreskowych i QR
Odczytanie kodu kreskowego wymaga tylko jednej linii kodu w IronBarcode.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-1.cs
using IronBarCode;
BarcodeResults results = BarcodeReader.Read("QuickStart.jpg");
if (results != null)
{
foreach (BarcodeResult result in results)
{
Console.WriteLine(result.Text);
}
}
Imports IronBarCode
Private results As BarcodeResults = BarcodeReader.Read("QuickStart.jpg")
If results IsNot Nothing Then
For Each result As BarcodeResult In results
Console.WriteLine(result.Text)
Next result
End If
Dzięki tej jednej linii kodu możliwe jest wykrywanie i skanowanie wszystkich typów kodów kreskowych z dokumentu wejściowego z wyjątkową wydajnością — wszystko w jednym kroku! Metoda obsługuje szeroki zakres formatów obrazów, takich jak JPEG, PNG i BMP, a także PDF oraz formaty wieloklatkowe, takie jak GIF i TIFF. Dostępne są konfigurowalne opcje poprawiające wydajność.
Aby zwiększyć prędkość odczytu, można utworzyć obiekt BarcodeReaderOptions z ustawieniem Speed skonfigurowanym dla lepszej wydajności. Domyślną wartością jest Balanced, jednak dostępna jest opcja Faster, która pomija pewne sprawdzenia.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-2.cs
using IronBarCode;
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
CropArea = new System.Drawing.Rectangle(100, 200, 300, 400),
};
BarcodeResults result = BarcodeReader.Read("QuickStart.jpg", myOptionsExample);
if (result != null)
{
Console.WriteLine(result.First().Text);
}
Imports IronBarCode
Private myOptionsExample As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
.CropArea = New System.Drawing.Rectangle(100, 200, 300, 400)
}
Private result As BarcodeResults = BarcodeReader.Read("QuickStart.jpg", myOptionsExample)
If result IsNot Nothing Then
Console.WriteLine(result.First().Text)
End If
Można również ustawić ScanMode na OnlyBasicScan, aby zoptymalizować proces odczytu.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-3.cs
using IronBarCode;
BarcodeResults results = BarcodeReader.Read("MultipleBarcodes.png");
// Loop through the results
foreach (BarcodeResult result in results)
{
string value = result.Value;
Bitmap img = result.BarcodeImage;
BarcodeEncoding barcodeType = result.BarcodeType;
byte[] binary = result.BinaryValue;
Console.WriteLine(result.Value);
}
Imports IronBarCode
Private results As BarcodeResults = BarcodeReader.Read("MultipleBarcodes.png")
' Loop through the results
For Each result As BarcodeResult In results
Dim value As String = result.Value
Dim img As Bitmap = result.BarcodeImage
Dim barcodeType As BarcodeEncoding = result.BarcodeType
Dim binary() As Byte = result.BinaryValue
Console.WriteLine(result.Value)
Next result
Inne konfiguracje umożliwiają określenie formatów kodów kreskowych, które mają być skanowane, co może przyspieszyć przetwarzanie poprzez ograniczenie zbędnych operacji skanowania.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-4.cs
using IronBarCode;
BarcodeResults pagedResults = BarcodeReader.Read("MultipleBarcodes.pdf");
// Loop through the results
foreach (BarcodeResult result in pagedResults)
{
int pageNumber = result.PageNumber;
string value = result.Value;
Bitmap img = result.BarcodeImage;
BarcodeEncoding barcodeType = result.BarcodeType;
byte[] binary = result.BinaryValue;
Console.WriteLine(result.Value);
}
// or from a multi-page TIFF scan with image correction:
BarcodeResults multiFrameResults = BarcodeReader.Read(inputImage: "Multiframe.tiff", new BarcodeReaderOptions
{
Speed = ReadingSpeed.Detailed,
ExpectMultipleBarcodes = true,
ExpectBarcodeTypes = BarcodeEncoding.Code128,
Multithreaded = false,
RemoveFalsePositive = false,
ImageFilters = null
});
Imports IronBarCode
Private pagedResults As BarcodeResults = BarcodeReader.Read("MultipleBarcodes.pdf")
' Loop through the results
For Each result As BarcodeResult In pagedResults
Dim pageNumber As Integer = result.PageNumber
Dim value As String = result.Value
Dim img As Bitmap = result.BarcodeImage
Dim barcodeType As BarcodeEncoding = result.BarcodeType
Dim binary() As Byte = result.BinaryValue
Console.WriteLine(result.Value)
Next result
' or from a multi-page TIFF scan with image correction:
Dim multiFrameResults As BarcodeResults = BarcodeReader.Read(inputImage:= "Multiframe.tiff", New BarcodeReaderOptions With {
.Speed = ReadingSpeed.Detailed,
.ExpectMultipleBarcodes = True,
.ExpectBarcodeTypes = BarcodeEncoding.Code128,
.Multithreaded = False,
.RemoveFalsePositive = False,
.ImageFilters = Nothing
})
Zapisywanie kodów kreskowych
Aby zapisać kody kreskowe za pomocą IronBarcode, należy użyć klasy BarcodeWriter.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-5.cs
using IronBarCode;
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.SaveAsImage("myBarcode.png");
Imports IronBarCode
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.SaveAsImage("myBarcode.png")
Stylizacja kodów kreskowych
IronBarcode oferuje kilka opcji pozwalających na manipulowanie wyglądem kodu kreskowego.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-7.cs
using IronBarCode;
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.AddAnnotationTextAboveBarcode("Product URL:");
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.SetMargins(100);
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple);
// All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png");
Imports IronBarCode
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.AddAnnotationTextAboveBarcode("Product URL:")
myBarcode.AddBarcodeValueTextBelowBarcode()
myBarcode.SetMargins(100)
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple)
' All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png")
Eksportowanie kodów kreskowych jako HTML
IronBarcode umożliwia eksportowanie kodów kreskowych jako dokumentów HTML lub jako części treści HTML.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-8.cs
using IronBarCode;
QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf");
Imports IronBarCode
QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf")
Generowanie kodów QR
Do tworzenia kodów QR należy użyć klasy QRCodeWriter, która udostępnia dodatkową konfigurację funkcji specyficznych dla QR, takich jak korekcja błędów.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-9.cs
using IronBarCode;
using IronSoftware.Drawing;
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo);
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf");
Imports IronBarCode
Imports IronSoftware.Drawing
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo)
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf")
Obsługiwane formaty kodów kreskowych
IronBarcode obsługuje szeroki zakres powszechnie używanych formatów kodów kreskowych zarówno do odczytu, jak i zapisu:
- QR, Micro QR oraz Rectangular Micro QR (rMQR).
- Inne dwuwymiarowe kody kreskowe, takie jak Aztec, Data Matrix, MaxiCode i PDF417.
- Skumulowane liniowe kody kreskowe, takie jak Databar.
- Konwencjonalne jednowymiarowe formaty kodów kreskowych, takie jak UPC-A, UPC-E, EAN-8, EAN-13, Codabar, ITF, MSI i Plessey.
Dlaczego warto wybrać IronBarcode?
IronBarcode oferuje przyjazne i łatwe w użyciu API, które umożliwia programistom odczytywanie i zapisywanie kodów kreskowych w środowisku .NET, zapewniając optymalną dokładność, precyzję i szybkość w rzeczywistych zastosowaniach.
Klasa BarcodeWriter automatycznie weryfikuje i poprawia sumy kontrolne kodów kreskowych UPCA i UPCE, a także obsługuje ograniczenia formatu numerycznego. IronBarcode pomaga programistom w wyborze najbardziej odpowiedniego formatu kodu kreskowego dla ich danych.
Biblioteka jest odporna na błędy — stosuje techniki wstępnego przetwarzania obrazu, takie jak automatyczne obracanie i usuwanie szumów, aby zmaksymalizować skuteczność wykrywania kodów kreskowych.
Dalsze kroki
Aby w pełni wykorzystać możliwości IronBarcode, zachęcamy do zapoznania się z samouczkami w tej sekcji dokumentacji oraz do odwiedzenia nas na GitHub.

