フッターコンテンツにスキップ
IRONBARCODEの使用
C# Windowsアプリケーションでバーコードスキャナーを読み取る方法

C# Windowsアプリでバーコードスキャナーを使用する方法

このチュートリアルでは、IronBarcodeライブラリを例に使用して、C#コンソールアプリケーションおよび.NET WindowsフォームアプリケーションでQRコードとバーコードをスキャンする方法を示します。

IronBarcodeライブラリを使用すると、複数のバーコードを同時にスキャンして読み取ることができ、また、不完全な画像でも成功してスキャンできます。 まず、バーコードスキャナーとは何かを明確にしましょう。

バーコードスキャナーとは何ですか?

バーコードは、さまざまな幅の平行な黒い線と白いスペースからなる四角または長方形の画像です。 バーコードスキャナーまたはバーコードリーダーは、印刷されたバーコードを読み取り、バーコードに含まれるデータをデコードし、そのデータをコンピューターに送信することができるデバイスです。

次の手順では、IronBarcodeライブラリを使用してバーコードスキャナーを作成する方法を紹介します。

C#でバーコードを読む方法

  • Microsoft Visual Studioで.NET Windowsフォームアプリケーションプロジェクトを作成します。
  • バーコードライブラリをインストールする
  • どんなバーコードやQRコードも読み取ります。
  • 1回のスキャンで複数のバーコードやQRコードを読み取ります。
  • IronBarcodeを使用して不完全なスキャンや写真からの読み取りを可能にします。

1. Microsoft Visual StudioでWindowsフォームアプリケーションを作成する

Visual Studioを開き、新しいプロジェクトを作成をクリックし、Windowsフォームアプリケーションテンプレートを選択して次へを押し、プロジェクトに名前を付けて次へを押し、対象の.NETフレームワークを選択して作成ボタンをクリックします。

プロジェクトを作成したら、Visual Studioツールボックスからフォームを次のようにデザインします: PictureBox、Label、TextBox、およびボタンコントロール。

C# Windowsアプリケーションでバーコードスキャナーを使用する方法、図1: バーコードスキャナー バーコードスキャナー

2. C#でバーコード.NETライブラリをインストールする

バーコードライブラリは、次の3つの方法のいずれかを使用してインストールできます。

1. パッケージマネージャコンソール

パッケージマネージャコンソールで次のコマンドを書きます。 これにより、パッケージがダウンロードされインストールされます。

Install-Package BarCode

2. NuGetパッケージマネージャーソリューション

NuGetパッケージソリューションを使用してバーコードライブラリをインストールすることもできます。 次の手順に従うだけです。

ツールをクリック > NuGetパッケージマネージャ > ソリューションのNuGetパッケージ管理

これにより、NuGetパッケージマネージャが開きます。 ブラウズをクリックしてバーコードを検索し、クラスライブラリをインストールします。

3. リンクからダウンロード

代わりに、IronBarCode.Dllをダウンロードし、プロジェクトに参照として追加できます。

ダウンロードした後、バーコードリーダープロジェクトに次の参照を追加します。

using IronBarCode;
using IronBarCode;
Imports IronBarCode
$vbLabelText   $csharpLabel

3. どんなバーコードやQRコードも読み取る

IronBarcodeライブラリを使用して.NETでバーコードやQRコードを読むのは非常に簡単です。.NET Barcode Readerを参照してください。

バーコードスキャナー

プロジェクトで読み取りたい画像をブラウズします。 それはPictureBoxで開きます; 「コードをスキャン」をクリックしてください。 テキストボックスにテキストが表示されます。

画像を開くための「ブラウズ」ボタンのコードはこちらです:

// Open file dialog   
OpenFileDialog open = new OpenFileDialog();
// Image filters  
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg;*.png;*.jpeg;*.gif;*.bmp";
if (open.ShowDialog() == DialogResult.OK) {  
    // Display image in PictureBox
    pictureBox1.Image = new Bitmap(open.FileName); 
    // Store image file path in class data member. Initialize it as string ImageFileName;
    ImageFileName = open.FileName; 
}
// Open file dialog   
OpenFileDialog open = new OpenFileDialog();
// Image filters  
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg;*.png;*.jpeg;*.gif;*.bmp";
if (open.ShowDialog() == DialogResult.OK) {  
    // Display image in PictureBox
    pictureBox1.Image = new Bitmap(open.FileName); 
    // Store image file path in class data member. Initialize it as string ImageFileName;
    ImageFileName = open.FileName; 
}
' Open file dialog   
Dim open As New OpenFileDialog()
' Image filters  
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg;*.png;*.jpeg;*.gif;*.bmp"
If open.ShowDialog() = DialogResult.OK Then
	' Display image in PictureBox
	pictureBox1.Image = New Bitmap(open.FileName)
	' Store image file path in class data member. Initialize it as string ImageFileName;
	ImageFileName = open.FileName
End If
$vbLabelText   $csharpLabel

「コードをスキャン」ボタンのコードはこちらです:

// Read the barcode from the image file path
BarcodeResult Result = BarcodeReader.Read(ImageFileName);
// Display the decoded text in TextBox
textBox1.Text = Result.Text;
// Read the barcode from the image file path
BarcodeResult Result = BarcodeReader.Read(ImageFileName);
// Display the decoded text in TextBox
textBox1.Text = Result.Text;
' Read the barcode from the image file path
Dim Result As BarcodeResult = BarcodeReader.Read(ImageFileName)
' Display the decoded text in TextBox
textBox1.Text = Result.Text
$vbLabelText   $csharpLabel

バーコードスキャナーがテキストボックスにバーコードデータを次のように表示します:

C# Windowsアプリケーションでバーコードスキャナーを使用する方法、図2: C#でスキャンされたバーコード画像 C#でスキャンされたバーコード画像

QRコードスキャナー

このセクションでは、IronBarcodeライブラリが実世界のスキューされたQRコード状況を効果的に処理することを示します。 スキューされた角度のQRコードはReadメソッドで処理されて読み取ることができますが、時間がかかる場合があります。 IronBarcodeライブラリは、そのような画像入力を処理するために余分なパラメータとしてBarcodeReaderOptionsを使用するカスタマイズされた方法を提供します。 コードは次の通りです:

// Define a collection of image filters to apply
var filtersToApply = new ImageFilterCollection() {
    new SharpenFilter(),
    new InvertFilter(),
    new ContrastFilter(),
    new BrightnessFilter(),
    new AdaptiveThresholdFilter(),
    new BinaryThresholdFilter()
};

// Configure barcode reader options with specified filters
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions() {
    ImageFilters = filtersToApply,
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
};

// Read the barcode/QR code with custom options and display result
BarcodeResult Result = BarcodeReader.Read(ImageFileName, myOptionsExample);
textBox1.Text = Result.Text;
// Define a collection of image filters to apply
var filtersToApply = new ImageFilterCollection() {
    new SharpenFilter(),
    new InvertFilter(),
    new ContrastFilter(),
    new BrightnessFilter(),
    new AdaptiveThresholdFilter(),
    new BinaryThresholdFilter()
};

// Configure barcode reader options with specified filters
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions() {
    ImageFilters = filtersToApply,
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
};

// Read the barcode/QR code with custom options and display result
BarcodeResult Result = BarcodeReader.Read(ImageFileName, myOptionsExample);
textBox1.Text = Result.Text;
' Define a collection of image filters to apply
Dim filtersToApply = New ImageFilterCollection() From {
	New SharpenFilter(),
	New InvertFilter(),
	New ContrastFilter(),
	New BrightnessFilter(),
	New AdaptiveThresholdFilter(),
	New BinaryThresholdFilter()
}

' Configure barcode reader options with specified filters
Dim myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = filtersToApply,
	.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128
}

' Read the barcode/QR code with custom options and display result
Dim Result As BarcodeResult = BarcodeReader.Read(ImageFileName, myOptionsExample)
textBox1.Text = Result.Text
$vbLabelText   $csharpLabel

スキューされたQRコード画像を開いた後の出力は次のようになります:

C# Windowsアプリケーションでバーコードスキャナーを使用する方法、図4: スキューされたQRコード画像 スキューされたQRコード画像

1回のスキャンで複数のバーコードを読み取る

PDFドキュメント

バーコード画像はPDFファイルからスキャンでき、各結果は希望どおりに適切に表示できます。 次のサンプルコードは、PDFファイルから複数のバーコードを読み取ることを可能にします。

// Scan for multiple barcodes within a PDF document
BarcodeResult[] PDFResults = BarcodeReader.ReadPdf("MultipleBarcodes.pdf");

// Work with the results found
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);
}
// Scan for multiple barcodes within a PDF document
BarcodeResult[] PDFResults = BarcodeReader.ReadPdf("MultipleBarcodes.pdf");

// Work with the results found
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);
}
' Scan for multiple barcodes within a PDF document
Dim PDFResults() As BarcodeResult = BarcodeReader.ReadPdf("MultipleBarcodes.pdf")

' Work with the results found
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
$vbLabelText   $csharpLabel

PDFファイルに含まれるバーコードとQRコード:

C# Windowsアプリケーションでバーコードスキャナーを使用する方法、図3: PDFからバーコードを読み取る結果 PDFからバーコードを読み取る結果

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

実際の使用ケースでは、バーコードは画像、スキャン、サムネイル、または写真でよく不完全で見つかり、デジタルノイズを含む場合やスキューしている場合があります。 このセクションでは、サムネイルからバーコードデータを読み取る方法を示します。

サムネイル

IronBarcodeライブラリは、C# Barcode Generatorを使用して、バーコードの破損したサムネイルを読み取ることができます。

C# Windows应用程序でバーコードスキャナーを使用する方法、図5: 自動バーコードサムネイルサイズ修正 自動バーコードサムネイルサイズ修正。 IronBarcode in C#でファイルが読める 実際のバーコードを合理的に表現するのに小さすぎるバーコード画像を自動的に検出し、これらのサムネイル化に関連するすべてのデジタルノイズをアップスケールしてクリーニングし、それらを再び読み取り可能にします。

IronBarcodeは、スキャンや幅広いバーコード画像形式を読み取るための多用途な.NETソフトウェアライブラリであり、これらのバーコードが完璧なスクリーングラブであっても、実際には写真、スキャン、またはその他の不完全な実世界の画像であってもそれを行うことができます。

// 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.Read("ThumbnailOfBarcode.gif");
// 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.Read("ThumbnailOfBarcode.gif");
' 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.Read("ThumbnailOfBarcode.gif")
$vbLabelText   $csharpLabel

まとめ

IronBarcode is a versatile .NET software library and C# QR Code Generator for scanning and reading a wide range of barcode image formats, and it can do so whether or not these barcodes are perfect screen grabs or are in fact photographs, scans, or other imperfect real-world images. Additionally, IronBarcode offers a wide range of customization options to improve barcode reading speed, such as crop regions or multi-threading, and the accuracy of the ML model. 現在、Iron Suite全体を購入すると、2つの価格で5つのライブラリを入手できます。

Currently, if you buy the complete Iron Suite, you can get five libraries for the price of just two.

よくある質問

C#アプリケーションにおけるバーコードスキャナーとは何ですか?

バーコードスキャナーは印刷されたバーコードを読み取り、情報をデコードしてコンピューターに送信するデバイスです。C#アプリケーションでは、IronBarcodeのようなライブラリを使用してこの機能を実装できます。

C#でバーコードスキャン用のWindowsフォームアプリケーションをどのように作成できますか?

Visual Studioを開き、「Windowsフォームアプリケーションテンプレート」を使用して新しいプロジェクトを作成し、目標とする.NET Frameworkを設定し、PictureBox、Label、TextBox、およびボタンを使用してフォームをデザインします。

C#プロジェクトでバーコードライブラリをインストールする推奨方法は何ですか?

C#プロジェクトにIronBarcodeのようなバーコードライブラリをインストールするには、Install-Package IronBarCodeを使用したパッケージマネージャーコンソール、NuGetパッケージマネージャー、またはDLLをダウンロードして参照に追加することで行えます。

C#ライブラリを使用して一度に複数のバーコードを読み取ることは可能ですか?

はい、IronBarcodeを使用すれば、BarcodeReader.ReadPdfメソッドを使用してPDFドキュメントからでも一度に複数のバーコードを読み取ることができます。

そのライブラリは低品質の画像からバーコードをどうやって読み取るのですか?

IronBarcodeは画像フィルターやアップスケーリング技術を適用してデジタルノイズを軽減し、正確な読み取りを実現します。

IronBarcodeのようなC#ライブラリでサポートされるバーコードフォーマットは何ですか?

IronBarcodeはQRコードやCode128を含む多様なバーコードフォーマットをサポートしています。不完全な画像やカメラで撮影された画像からもこれらのフォーマットを読み取ることができます。

バーコード読み取りを.NETアプリケーションに実装する手順は何ですか?

画像をPictureBoxに読み込み、「スキャンコード」アクションを発動させ、IronBarcodeを使用してテキストボックスにデコードされたテキストを表示します。

IronBarcodeは傾いたQRコードを効果的に処理できますか?

はい、IronBarcodeはBarcodeReaderOptionsを使用して必要な画像フィルターと調整を適用し、正確な読み取りを実現します。

IronBarcodeはバーコード読み取りにどのようなカスタマイズ機能を提供しますか?

IronBarcodeはクロップ領域、マルチスレッド、パラメーター調整などの機能を提供して、バーコード読み取りの速度と精度を向上させます。

C#でバーコードライブラリを使用する詳細情報はどこで入手できますか?

Iron Softwareの公式ドキュメンテーションページでC#でバーコードライブラリを使用する詳細情報を入手できます。

Jordi Bardia
ソフトウェアエンジニア
Jordiは、最も得意な言語がPython、C#、C++であり、Iron Softwareでそのスキルを発揮していない時は、ゲームプログラミングをしています。製品テスト、製品開発、研究の責任を分担し、Jordiは継続的な製品改善において多大な価値を追加しています。この多様な経験は彼を挑戦させ続け、興味を持たせており、Iron Softwareで働くことの好きな側面の一つだと言います。Jordiはフロリダ州マイアミで育ち、フロリダ大学でコンピュータサイエンスと統計学を学びました。