IRONBARCODEの使用

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

ジョルディ・バルディア
ジョルディ・バルディア
2022年5月4日
更新済み 2024年1月20日
共有:

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

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

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

バーコードは、一連の幅が異なる平行な黒い線と白いスペースで構成された四角形または長方形の画像です。 バーコードスキャナー(バーコードリーダー)は、印刷されたバーコードを読み取り、バーコードに含まれるデータをデコードして、そのデータをコンピュータに送信できる装置です。

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

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

  • Microsoft Visual Studio で .NET Windows Forms アプリケーション プロジェクトを作成する
  • バーコードライブラリをインストール
  • 任意のバーコードまたはQRコードを読み取る
  • 単一のスキャンで複数のバーコードやQRコードを読み取る
  • IronBarcodeに不完全なスキャンや写真から読み取らせる

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

Visual Studio を開く > Create New Project をクリック > Windows Forms Application Template を選択 > Next を押す > プロジェクトに名前を付ける > Next を押す > 対象とする .NET Framework を選択 > Create ボタンをクリック。

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

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

バーコードスキャナー

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

バーコードライブラリをインストールするには、以下の3つの方法のいずれかを使用できます:

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

以下のコマンドをパッケージマネージャーコンソールに入力してください。 パッケージをダウンロードしてインストールします。

Install-Package BarCode

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

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

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

これは、NuGetパッケージマネージャーを開きます。 「ブラウズ」をクリックし、「Barcode」を検索して、クラスライブラリをインストールしてください。

リンクからダウンロードする

代替手段として、IronBarCode.Dll をダウンロードしてプロジェクトに参照として追加することができます。

ダウンロードした後、次の参照をバーコードリーダープロジェクトに追加してください。

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

3. 任意のバーコードまたはQRコードを読み取る

.NETでバーコードまたはQRコードを読み取ることは、.NET Barcode Readerを使用したIronBarcodeライブラリを使用すると非常に簡単です。

バーコードスキャナー

プロジェクト内で読み取りたい画像を参照してください。 それはPictureBoxで開かれます。 「scan code」をクリックしてください。 テキストはテキストボックスに表示されます。

以下は、画像を開くための「browse」ボタンのコードです:

// 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 picture box
    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 picture box
    pictureBox1.Image = new Bitmap(open.FileName); 
    // store image file path in class data member. Initialize it as string ImageFileName;
    ImageFileName = open.FileName; 
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

「scan code」ボタンのコード:

BarcodeResult Result = BarcodeReader.Read(ImageFileName);
textBox1.Text = Result.Text;
BarcodeResult Result = BarcodeReader.Read(ImageFileName);
textBox1.Text = Result.Text;
Dim Result As BarcodeResult = BarcodeReader.Read(ImageFileName)
textBox1.Text = Result.Text
$vbLabelText   $csharpLabel

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

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

Barcode イメージを C# でスキャンする

QRコードスキャナー

このセクションでは、IronBarcodeライブラリが傾いたQRコードに関する実際の状況を効果的に処理します。 斜めのQRコードはRead メソッドで処理および読み取りが可能ですが、解決により多くの時間がかかることがあります。 IronBarcodeライブラリは、このような画像入力を処理するための追加のパラメーターとしてBarcodeReaderOptionsを使用するカスタマイズされた方法を提供します。 コードは次のようになります:

// Choose which filters are to be applied (in order);
var filtersToApply = new ImageFilterCollection() {
    new SharpenFilter(),
    new InvertFilter(),
    new ContrastFilter(),
    new BrightnessFilter(),
    new AdaptiveThresholdFilter(),
    new BinaryThresholdFilter()
};

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Set chosen filters in BarcodeReaderOptions:
    ImageFilters = filtersToApply,

    ExpectBarcodeTypes = BarcodeEncoding.QRCode 
 BarcodeEncoding.Code128,
};
BarcodeResult Result = BarcodeReader.Read(ImageFileName, myOptionsExample);
textBox1.Text = Result.Text;
// Choose which filters are to be applied (in order);
var filtersToApply = new ImageFilterCollection() {
    new SharpenFilter(),
    new InvertFilter(),
    new ContrastFilter(),
    new BrightnessFilter(),
    new AdaptiveThresholdFilter(),
    new BinaryThresholdFilter()
};

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Set chosen filters in BarcodeReaderOptions:
    ImageFilters = filtersToApply,

    ExpectBarcodeTypes = BarcodeEncoding.QRCode 
 BarcodeEncoding.Code128,
};
BarcodeResult Result = BarcodeReader.Read(ImageFileName, myOptionsExample);
textBox1.Text = Result.Text;
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

傾いたQRコード画像を開いた後、出力は次のようになります:

C# Windowsアプリケーションでバーコードスキャナーを使う方法, 図 4: 歪んだQRコード画像

傾斜したQrCode画像

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

PDFドキュメント

バーコード画像はPDFファイルからスキャンでき、その結果は望むように適切に表示することができます。 以下のサンプルコードは、PDFファイルから複数のバーコードを読み取ることができます。

// Multiple barcodes may be scanned up from a single document or image.  A PDF document may also used as the input 
imagePagedBarcodeResult [] PDFResults = BarcodeReader.ReadPdf("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);
}
// Multiple barcodes may be scanned up from a single document or image.  A PDF document may also used as the input 
imagePagedBarcodeResult [] PDFResults = BarcodeReader.ReadPdf("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);
}
' Multiple barcodes may be scanned up from a single document or image.  A PDF document may also used as the input 
Dim PDFResults() As imagePagedBarcodeResult = BarcodeReader.ReadPdf("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
$vbLabelText   $csharpLabel

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

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

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

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

現実のユースケースでは、バーコードはしばしば画像、スキャン、サムネイル、または写真に欠陥があり、デジタルノイズを含んでいたり、傾いていたりすることがあります。 このセクションでは、サムネイルからバーコードデータを読み取る方法を示します。

サムネイル

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

C# Windows アプリケーションでのバーコードスキャナーの使用方法, 図5: 自動バーコードサムネイルサイズ修正。 IronBarcodeを使用してC#で読み取るファイル

自動バーコードサムネイルサイズ補正。 IronBarcodeを使用して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.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は、多様な.NETソフトウェアライブラリであり、C# QRコードジェネレーターであり、広範なバーコード画像形式をスキャンおよび読み取ることができます。また、これらのバーコードが完全なスクリーンキャプチャであろうとなかろうと、あるいは写真、スキャン、その他の不完全な実世界の画像であっても処理できます。 さらに、IronBarcode は、バーコード読み取り速度を向上させるために、切り取り領域マルチスレッドML モデルの精度など、幅広いカスタマイズオプションを提供します。 IronBarcodeに関する詳細情報は公式ドキュメントページをご覧ください。

現在、完全なIron Suiteを購入すると、2つの価格で5つのライブラリを手に入れることができます。

ジョルディ・バルディア
ソフトウェアエンジニア
ジョルディは、Iron Softwareでのスキルを活かしていないときには、ゲームプログラミングをしており、Python、C#、C++に最も堪能です。彼は製品テスト、製品開発、研究の責任を共有しており、継続的な製品改善に大きな価値をもたらしています。この多様な経験は彼を常に挑戦的で魅力的に保ち、彼はIron Softwareで働く一番好きな側面の一つだと言っています。ジョルディはフロリダ州マイアミで育ち、フロリダ大学でコンピューターサイエンスと統計学を学びました。
< 以前
バーコードジェネレーター .NET チュートリアル
次へ >
ASP.NETでC#を使用してバーコードを生成する方法