C#でバーコードを読み取る

This article was translated from English: Does it need improvement?
Translated
View the article in English

によって フランク・ウォーカー

バーコードライブラリをVisual Studioプロジェクトにインストールする

 用 C# NuGet ライブラリ

でインストール NuGet

Install-Package BarCode
または
Java PDF JAR(ジャバPDF JAR)

ダウンロード DLL (ディーエルエル)

DLLをダウンロード

プロジェクトに手動でインストールする

 用 C# NuGet ライブラリ

でインストール NuGet

Install-Package BarCode
または
Java PDF JAR(ジャバPDF JAR)

ダウンロード DLL (ディーエルエル)

DLLをダウンロード

プロジェクトに手動でインストールする

今日からプロジェクトでIronPDFを使い始めましょう。無料のトライアルをお試しください。

最初のステップ:
green arrow pointer

チェックアウト IronBarcode オン Nuget 迅速なインストールと展開のために。8百万以上のダウンロード数により、をC#で変革しています。

 用 C# NuGet ライブラリ nuget.org/packages/BarCode/
Install-Package BarCode

インストールを検討してください IronBarcode DLL 直接。ダウンロードして、プロジェクトまたはGACの形式で手動でインストールしてください。 IronBarCode.zip

プロジェクトに手動でインストールする

DLLをダウンロード

IronBarcodeは、.NETでバーコードを読み取るための多用途で高度かつ効率的なライブラリを提供します。

最初のステップはIronBarcodeをインストールすることです。これを最も簡単に行う方法は、NuGetパッケージを使用することです。ただし、手動でインストールすることも選択できます。 DLL (ディーエルエル) プロジェクトまたはグローバルアセンブリキャッシュに追加します。 IronBarcodeは、C#バーコードスキャナーアプリケーションの作成に適しています。

Install-Package BarCode

最初のバーコードを読み取る

.NETでバーコードやQRコードを読み取るのは、IronBarcodeクラスライブラリを使用すると非常に簡単です。 .NET バーコードリーダー. 最初の例では、このバーコードを1行のコードで読み取る方法を確認できます。

C#でスキャンするためのCode128バーコード画像

値、画像、エンコーディング形式、バイナリデータを抽出することができます (何かあれば)、そしてそれをコンソールに出力できます。

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
VB   C#

もっと頑張って、具体的にしてください。

次の例では、TryHarder変数をQuicklyReadOneBarcodeメソッドに追加します。 これは、文字通りにもっと時間をかけて、隠されている、破損している、または斜めの角度になっている可能性のあるQRコードをより深くスキャンするように試みます。

BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("TryHarderQR.png", BarcodeEncoding.QRCode 
 BarcodeEncoding.Code128 , true);
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("TryHarderQR.png", BarcodeEncoding.QRCode 
 BarcodeEncoding.Code128 , true);
Dim Result As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("TryHarderQR.png", BarcodeEncoding.QRCode BarcodeEncoding.Code128, True)
VB   C#

以下の歪んだQRコードを読み取ります:

C#で45度回転したQRコードをスキャンする

例では、バーコードエンコーディングを指定できることがわかります。(s) 私たちが探しているもの、または複数の形式を指定することができます。 そうすることで、バーコードの読み取りパフォーマンスと精度が大幅に向上します。 その pipe character or 'Bitwize OR' is used to specify multiple formats simultaneously.

次のテクノロジーを使用することで、同様のことをより高い精度で達成できます。 BarcodeReader.ReadASingleBarcode(バーコードリーダー・リード・ア・シングル・バーコード) メソッド。

BarcodeResult Result = IronBarCode.BarcodeReader.ReadASingleBarcode("TryHarderQR.png", BarcodeEncoding.QRCode 
 BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);
BarcodeResult Result = IronBarCode.BarcodeReader.ReadASingleBarcode("TryHarderQR.png", BarcodeEncoding.QRCode 
 BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);
Dim Result As BarcodeResult = IronBarCode.BarcodeReader.ReadASingleBarcode("TryHarderQR.png", BarcodeEncoding.QRCode BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels)
VB   C#

複数のバーコードを読み取る

PDFドキュメント

次の例では、読み取りを行う方法を見ていきます。 スキャンされたPDFドキュメント そして、非常に少ないコード行で1次元フォーマットのすべてのバーコードを見つけます。

ご覧のように、単一のドキュメントから単一のバーコードを読み取ることと非常に似ていますが、バーコードが見つかったページ番号に関する新しい情報が追加されています。


using IronBarCode;
using System;
using System.Drawing;

// Multiple barcodes may be scanned up from a single document or image.  A PDF document may also 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 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 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
VB   C#

異なるページで次のバーコードを見つけました。

C# - PDFの結果からバーコードを読み取る

スキャンおよびTIFF

次の例では、マルチフレームTIFFから同じ結果を得ることができることがわかります。この場合、TIFFはPDFと同様に扱われます。

C# - マルチフレームTIFF画像からバーコードを読み取る
// 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
VB   C#

マルチスレッド

複数のドキュメントを読み取るには、ドキュメントのリストを作成し、それを使用することでIronBarcodeからより良い結果を得ることができます。 BarcodeReader.ReadBarcodesMultithreaded (バーコードリーダー・リードバーコードマルチスレッド) メソッド。 これは、バーコードスキャンプロセスにマルチスレッドとCPUの全コアを使用するため、バーコードを1つずつ読み取るよりも指数関数的に速くなります。

// 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
VB   C#

不完全な画像からバーコードを読み取る

実際の使用ケースでは、必ずしも完璧なスクリーンショットではないバーコードを読み取る必要があるかもしれません。 それらは不完全な画像、スキャン、または写真であり、デジタルノイズが含まれていたり、傾いていたりする可能性があります。 ほとんどの従来のオープンソースの .NET 向けバーコード生成および読み取りライブラリでは、これは不可能でしょう。 しかし、これ C#でのバーコードリーダー これは非常に簡単にします。

次の例では、QuicklyReadOneBarcodeのTryHarderメソッドについて見ていきます。 この単一パラメーターにより、IronBarcode は不完全なデジタルサンプルからバーコードの傾きを補正して読み取ろうとします。

写真

写真の例では、特定のバーコード回転補正とバーコード画像補正を設定し、デジタルノイズおよびスマートフォンのカメラから予想される歪みや傾斜、回転を補正します。

C#で携帯カメラからバーコードを読み取る
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)
VB   C#

スキャン

次の例は、QRコードおよびPDF-417バーコードを読み取る方法について説明します。 スキャンしたPDF. バーコードの回転補正およびバーコード画像補正のレベルを適切に設定し、ドキュメントを軽く清掃するようにしていますが、必要以上の処理を行ってパフォーマンスの大幅な低下を招くことはありません。

C#でスキャンされたPDFドキュメントからバーコードを読み取る
// Multi frame TIFF and GIF images 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;
  ///...
} 
// Multi frame TIFF and GIF images 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;
  ///...
} 
' Multi frame TIFF and GIF images 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
VB   C#

サムネイル

最終の例では、これが C# バーコードジェネレーター は、破損したバーコードのサムネイルを読み取ることさえも可能です。

自動バーコードサムネイルサイズ修正。 Iron Barcodeを使用してC#でファイルを読み取ることができます

弊社のバーコードリーダーメソッドは、実際のバーコードとしては合理的に小さすぎるバーコード画像を自動的に検出し、サムネイルに関連するすべてのデジタルノイズをアップスケールしてクリーンアップします。 再び読み取り可能にします。

// Small or 'Thumbnail' barcode images are automatically detected by IronBarCode and corrected for 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 for 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 for wherever possible even if they have much digital noise.
Dim SmallResult As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("ThumbnailOfBarcode.gif", BarcodeEncoding.Code128)
VB   C#

概要

要約すると、IronBarcodeは多用途な.NETソフトウェアライブラリであり C# QRコードジェネレーター さまざまなバーコード形式を読み取るために使用できます。また、これらのバーコードが完全なスクリーングラブであるか、実際の写真、スキャン、または他の不完全な現実世界の画像であるかに関わらず、それを行うことができます。

さらに読む

IronBarcodeの使用方法についてさらに学ぶには、このセクション内の他のチュートリアルや、当社のホームページにあるサンプルを参照することをお勧めします。 ほとんどの開発者がこれで始めるのに十分だと感じています。

摂 Our APIリファレンス 特に参照して バーコードリーダー クラスおよび バーコードエンコーディング Enum は、これを使用することで達成可能なことを詳しく示します。 C# バーコード ライブラリ.

ソースコードのダウンロード

このチュートリアルをダウンロードし、自分で実行することを強くお勧めします。 ソースコードをダウンロードするか、GitHubでフォークすることで、これを実行できます。 こちらのソース .NET バーコードリーダー チュートリアルは、C#で記述されたVisual Studio 2017コンソールアプリケーションプロジェクトとして利用可能です。

.NETソフトウェアエンジニア .NETからPDFファイルを生成する最も効率的な方法の一つです。追加のAPIを学習する必要もなく、また複雑な設計システムを操作する必要もありません。

フランク・ウォーカー

.NETプロダクトデベロッパー

フランクはIronBarcodeの開発の主要な力となってきました。昨年からIronOCRでの作業を行い、IronBarcode内のOCRの利用ケースを構築する上で影響力を発揮し、現在のツールに成長させました。