IronBarcodeを使ってC#でデータフォーマットを出力する方法
IronBarcode は、BinaryValue、座標、寸法、ページ番号、方向、テキスト、値のプロパティなど、バーコード読み取りからの複数の出力形式を提供します。 これらのフォーマットは、さまざまな使用例に対してプログラムで BarCode データを処理することを可能にします。
IronBarcodeは単にバーコードを読み取り、コンソールに値を表示するだけでなく、それ以上のものを提供します。 ユーザーが読み取り結果を処理できるように、いくつかの出力形式が用意されています。 これらの形式には、バーコード イメージ、バーコード タイプ、BinaryValue、座標、高さ、幅、ページ番号、バーコード、ページの向き、テキスト、値などのプロパティが含まれます。
ユーザーはプログラム内でこれらのプロパティをさらに操作できます。 それでは、これらのプロパティの使用方法と、役立つユースケースを探ってみましょう。
クイックスタート: バーコードの値を読み取り、1行で入力する
この例では、IronBarcodeを使用して画像からバーコードを読み取る方法を示しています。 素早く始めるのに最適です。より包括的な例については、BarCodeクイックスタートガイドをご覧ください。
- バーコード読み取り用のC#ライブラリをダウンロードする
- バーコード検出のためにPDFと画像を準備します
- 検出されたバーコードの種類と画像にアクセスする
- バーコードのxおよびy座標、ならびにその高さと幅を取得する
- バーコードのテキストと値を読み取る
利用可能な出力形式とその使用例とは
BarcodeResult はさまざまな便利なプロパティを格納します。 これらのプロパティは以下に列挙されます:
BarcodeImageBarcodeTypeBinaryValue- 座標、高さと幅
PageNumberBarcodeおよびPageOrientation- テキストと値
各プロパティは、BarCode処理ワークフローにおいて特定の目的を果たします。 在庫管理システム、文書処理パイプライン、品質管理アプリケーションのいずれを構築する場合でも、これらのデータ形式は、さまざまなソースから BarCode を読み取るために必要な柔軟性を提供します。
バーコード画像を抽出して保存するにはどうすればよいですか?
IronBarcode が画像を読み取ると、画像内で見つかったバーコードは、タイプ AnyBitmap の BarcodeImage プロパティとして BarcodeResult に保存されます。 BarcodeImage プロパティには、見つかったバーコード画像が格納されます。 ユーザーは、このオブジェクトを取得して画像をさらに加工したり、永久コピーとして保存したりすることができます。 これは、画像から BarCode 画像を抽出するための追加コードを排除することで、効率性と使いやすさを提供します。
以下のコード・スニペットは、この出力形式の使用例を示しています:
:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-BarcodeImage.cs
using IronBarCode;
using IronSoftware.Drawing;
using System.Collections.Generic;
// Read barcode from PDF file
BarcodeResults result = BarcodeReader.ReadPdf("test.pdf");
// Create list for barcodes
List<AnyBitmap> barcodeList = new List<AnyBitmap>();
foreach (BarcodeResult barcode in result)
{
barcodeList.Add(barcode.BarcodeImage);
}
// Create multi-page TIFF
AnyBitmap.CreateMultiFrameTiff(barcodeList).SaveAs("barcodeImages.tif");
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System.Collections.Generic
' Read barcode from PDF file
Private result As BarcodeResults = BarcodeReader.ReadPdf("test.pdf")
' Create list for barcodes
Private barcodeList As New List(Of AnyBitmap)()
For Each barcode As BarcodeResult In result
barcodeList.Add(barcode.BarcodeImage)
Next barcode
' Create multi-page TIFF
AnyBitmap.CreateMultiFrameTiff(barcodeList).SaveAs("barcodeImages.tif")
上のコード・スニペットは、この出力形式の使用例を示しています。 具体的には、PDF文書内で検出されたBarCodeから複数ページのTIFF画像を作成します。 最初に、サンプルPDFでバーコードをスキャンまたは検出します。 次に、AnyBitmap のリストを作成し、そこに BarcodeImage プロパティからの情報を保存します。 最後に、このリストを使用して、CreateMultiFrameTiff メソッドで複数ページの TIFF を生成します。 このテクニックは、複数ページのGIFファイルやTIFFファイルを処理するときに特に役立ちます。
BarcodeImage プロパティは、BarcodeResult から、読み取り中に見つかったバーコードの画像のみを保存し、入力画像全体を保存しません。
異なる BarCode タイプをプログラムで識別するにはどうすればよいですか?
このプロパティは、入力画像または文書に存在するバーコードのタイプを判別するのに役立ちます。 ただし、画像内のバーコードタイプがIronBarcodeでサポートされ、読み取り可能でなければならないという制限があります。 IronBarcodeでサポートされているバーコードの種類については、こちらの記事をご参照ください。 さらに、サポートされているバーコード形式の完全なリストを調べ、特定の要件との互換性を確認してください。
以下のコードスニペットは、コンソールに値を出力することによって、画像内のバーコード値とバーコードタイプを取得する方法を示しています。
:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-BarcodeType.cs
using IronBarCode;
using System;
// Read barcode from PNG
BarcodeResults result = BarcodeReader.Read("bc3.png");
// Output barcode type to console
foreach (BarcodeResult barcode in result)
{
Console.WriteLine("The barcode value is " + barcode.ToString() + " and the barcode type is " + barcode.BarcodeType);
}
Imports IronBarCode
Imports System
' Read barcode from PNG
Private result As BarcodeResults = BarcodeReader.Read("bc3.png")
' Output barcode type to console
For Each barcode As BarcodeResult In result
Console.WriteLine("The barcode value is " & barcode.ToString() & " and the barcode type is " & barcode.BarcodeType)
Next barcode
上記のコード スニペットでは、入力画像に対して BarcodeReader.Read() メソッドを呼び出してバーコードの読み取りを実行します。 これは、イメージ内で使用可能なすべてのバーコードを読み取って得られたすべての BarcodeResult を格納する BarcodeResults オブジェクトを返します。 次に、BarcodeResults オブジェクトを反復処理して BarcodeResult を取得し、バーコードの値とタイプをコンソールに出力します。 このアプローチは、Code 39 barcodesのような特殊な形式を含む、さまざまなバーコードタイプでシームレスに動作します。
バイナリ値出力はいつ使うべきですか?
IronBarcodeを使用すると、ユーザーは BarcodeResult オブジェクトから BinaryValue プロパティにアクセスして、バーコード値のバイト配列を取得できます。 これにより、プログラム内でバーコード値をさらに操作できます。 バイナリ値出力は、暗号化されたデータ、BarCodeでエンコードされたファイル添付、またはバイトレベルのデータ処理を必要とするシステムとの統合を扱う場合に特に便利です。
以下のコードスニペットは、バーコード値をバイナリデータとして取得するユースケースを示しています:
:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-BinaryValue.cs
using IronBarCode;
// Read barcode from PNG
BarcodeResults result = BarcodeReader.Read("multiple-barcodes.png");
int i = 1;
foreach (BarcodeResult barcode in result)
{
var binaryValue = barcode.BinaryValue;
var barcodeType = IronBarCode.BarcodeEncoding.QRCode;
// Create QR code
GeneratedBarcode generatedBarcode = BarcodeWriter.CreateBarcode(binaryValue, barcodeType);
// Export QR code
generatedBarcode.SaveAsPng($"qrFromBinary{i}.png");
i++;
}
Imports IronBarCode
' Read barcode from PNG
Private result As BarcodeResults = BarcodeReader.Read("multiple-barcodes.png")
Private i As Integer = 1
For Each barcode As BarcodeResult In result
Dim binaryValue = barcode.BinaryValue
Dim barcodeType = IronBarCode.BarcodeEncoding.QRCode
' Create QR code
Dim generatedBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode(binaryValue, barcodeType)
' Export QR code
generatedBarcode.SaveAsPng($"qrFromBinary{i}.png")
i += 1
Next barcode
上記のコードスニペットを観察すると、画像内の複数のバーコードを新しいバイナリエンコードファイルに変換する直感的なプログラムを作成しました。 最初に、サンプルのPNG画像でバーコードをスキャンします。 これらのバーコードを検出したら、それらを反復処理し、BinaryValue プロパティにアクセスして、それを使用して新しいバイナリ ファイルを作成します。 このテクニックは、複数の BarCode を読み取り、それらのバイナリ データを個別に処理する必要がある場合に特に役立ちます。
バーコードの位置と寸法にアクセスするにはどうすればよいですか?
ユーザーがアクセスできる BarcodeResult オブジェクトの別のプロパティは、バーコードの座標 (Y2 など) と、イメージ ファイルまたはドキュメント内の Height および Width です。 これらのプロパティは、ユーザーがバーコードの位置や寸法に関する情報を取得する必要がある場合に役立ちます。 この空間情報は、自動文書処理、品質管理システム、最適化された BarCode スキャン用の crop regions を実装する場合など、正確な位置決めが必要なアプリケーションにとって非常に重要です。
バーコードの位置と寸法を示しましょう。
:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-height-width.cs
using IronBarCode;
using IronSoftware.Drawing;
using System.Linq;
// Read barcode from PNG
BarcodeResults result = BarcodeReader.Read("multiple-barcodes.png");
AnyBitmap bitmap = AnyBitmap.FromFile("multiple-barcodes.png");
foreach (BarcodeResult barcode in result)
{
PointF[] barcodePoints = barcode.Points;
float x1 = barcodePoints.Select(b => b.X).Min();
float y1 = barcodePoints.Select(b => b.Y).Min();
Rectangle rectangle = new Rectangle((int)x1, (int)y1, (int)barcode.Width!, (int)barcode.Height!);
bitmap = bitmap.Redact(rectangle, Color.Magenta);
// Save the image
bitmap.SaveAs("redacted.png", AnyBitmap.ImageFormat.Png);
}
Imports System
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System.Linq
' Read barcode from PNG
Private result As BarcodeResults = BarcodeReader.Read("multiple-barcodes.png")
Private bitmap As AnyBitmap = AnyBitmap.FromFile("multiple-barcodes.png")
For Each barcode As BarcodeResult In result
Dim barcodePoints() As PointF = barcode.Points
Dim x1 As Single = barcodePoints.Select(Function(b) b.X).Min()
Dim y1 As Single = barcodePoints.Select(Function(b) b.Y).Min()
'INSTANT VB TODO TASK: There is no VB equivalent to the C# 'null-forgiving operator':
'ORIGINAL LINE: Rectangle rectangle = new Rectangle((int)x1, (int)y1, (int)barcode.Width!, (int)barcode.Height!);
Dim rectangle As New Rectangle(CInt(Math.Truncate(x1)), CInt(Math.Truncate(y1)), CInt(barcode.Width), CInt(barcode.Height))
bitmap = bitmap.Redact(rectangle, Color.Magenta)
' Save the image
bitmap.SaveAs("redacted.png", AnyBitmap.ImageFormat.Png)
Next barcode
編集前
編集後
上記のコード・スニペットは、画像ファイル内の複数の BarCode を再編集しています。これを実現するために、IronBarcodeとIronDrawingという2つのライブラリを組み合わせて使用しています。 BarcodeResult オブジェクトを取得してそのプロパティを抽出するには、まず、BarcodeReader.Read() メソッドを使用して画像ファイル内の使用可能なバーコードを読み取ります。 同時に、入力画像ファイルを AnyBitmap オブジェクトに変換して、画像に編集方法を適用する必要があります。 BarcodeResults オブジェクトを取得したら、ループを適用して反復処理し、画像内で使用可能なすべてのバーコードの Width、および Height を取得し、それらを AnyBitmap.Redact() メソッドの CropRectangle プロパティで使用できます。
複数ページの文書でページ番号が重要なのはなぜですか?
ユーザーは、BarCodeが見つかったページ番号を取得することができます。 これは、複数の BarCode を含む複数ページのドキュメントを使用し、さらに処理するためにドキュメント内で見つかったバーコードの場所を知る必要があるユーザーに役立つ機能です。 この機能は、PDF文書からBarCodeを読み取ったり、企業アプリケーションでバッチ文書を処理したりするときに不可欠です。
下のコードを見てください:
:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-page-number.cs
using IronBarCode;
using System;
// Read barcode from PDF
BarcodeResults result = BarcodeReader.ReadPdf("test.pdf");
// Output page number to console
foreach (BarcodeResult barcode in result)
{
Console.WriteLine("The barcode value " + barcode.ToString() + " is found on page number " + barcode.PageNumber);
}
Imports IronBarCode
Imports System
' Read barcode from PDF
Private result As BarcodeResults = BarcodeReader.ReadPdf("test.pdf")
' Output page number to console
For Each barcode As BarcodeResult In result
Console.WriteLine("The barcode value " & barcode.ToString() & " is found on page number " & barcode.PageNumber)
Next barcode
上のコード・スニペットは、ユーザーが複数ページのPDF文書で見つかったBarCodeの値とそれぞれのページ番号を返すプログラムを必要とするユースケースの1つを示しています。 このコードは、複数ページの PDF ドキュメント内のバーコードを読み取り、ドキュメント内で見つかったすべてのバーコードを格納するオブジェクトを返します。 それぞれのオブジェクト内の項目を繰り返し処理して、バーコードの値とそのバーコードが見つかったページ番号を取得します。 この使用例とは別に、ユーザーはこのプロパティを使用して、ドキュメント内のすべての BarCode が読み取られたかどうかをデバッグできます。
バーコードの回転とページの向きを検出するには?
IronBarcodeを使用すると、ユーザーはバーコードの向きやバーコードが見つかったページの向きの情報を取得することができます。 これら 2 つの情報を抽出するには、BarcodeResult オブジェクトから Rotation プロパティと PageOrientation プロパティにアクセスします。 Rotation は、見つかったバーコードの回転角度を表す整数を返します。 この機能は、画像の向き補正機能と連動し、スキャンの角度に関係なくバーコードを正確に読み取ります。
下のコードを見てください:
:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-orientation.cs
using IronBarCode;
using System;
// Read barcode from PDF
BarcodeResults result = BarcodeReader.ReadPdf("test.pdf");
// Output page orientation and rotation to console
foreach (BarcodeResult barcode in result)
{
Console.WriteLine(barcode.Value);
Console.WriteLine(barcode.PageOrientation);
Console.WriteLine(barcode.Rotation);
}
Imports IronBarCode
Imports System
' Read barcode from PDF
Private result As BarcodeResults = BarcodeReader.ReadPdf("test.pdf")
' Output page orientation and rotation to console
For Each barcode As BarcodeResult In result
Console.WriteLine(barcode.Value)
Console.WriteLine(barcode.PageOrientation)
Console.WriteLine(barcode.Rotation)
Next barcode
上記のコード スニペットは、添付されたサンプル PDF 入力を使用して実行され、ユーザーがそれぞれ BarcodeResult.PageOrientation と BarcodeResult.Rotation の値を取得することで、ページの向きとバーコードの回転を取得できることを証明しました。 この機能は主にデバッグ目的で役立ちます。
180、および 270 度の回転のバーコードのみを読み取ることができます。 指定した以外の回転値がある場合、IronBarcodeは値を返しません。 PageOrientation は、 Portraitまたは Landscape で構成される PageOrientation オブジェクトを返します。テキスト プロパティと値プロパティの違いは何ですか?
もちろん、ユーザーがIronBarcodeを使用する際に取得したい主なプロパティは、その値とテキストです。 これらの2つのプロパティは、しばしば互換的に使用され、同じ値を返します。 それ以外にも、ユーザーは BarcodeResult.ToString() メソッドを使用して同じ結果を得ることができます。 特殊なアプリケーションで作業する場合や、バーコードデータをストリームとしてエクスポートする場合、これらのプロパティは、好みの形式でバーコードコンテンツにアクセスする柔軟な方法を提供します。
以下のコードスニペットはデモンストレーションを示しています:
:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-text-value.cs
using IronBarCode;
using System;
// Read barcode from PDF
BarcodeResults result = BarcodeReader.ReadPdf("barcodestamped3.pdf");
// Output text value to console
foreach (BarcodeResult barcode in result)
{
Console.WriteLine(barcode.Value);
Console.WriteLine(barcode.Text);
Console.WriteLine(barcode.ToString());
}
Imports IronBarCode
Imports System
' Read barcode from PDF
Private result As BarcodeResults = BarcodeReader.ReadPdf("barcodestamped3.pdf")
' Output text value to console
For Each barcode As BarcodeResult In result
Console.WriteLine(barcode.Value)
Console.WriteLine(barcode.Text)
Console.WriteLine(barcode.ToString())
Next barcode
上記のコードスニペットから、ユーザーはIronBarcodeを使用して画像内のバーコードを読み取るために必要なコードはわずか数行です。 BarcodeReader.Read() メソッドによって返された BarcodeResults を反復処理した後、Value プロパティと Text プロパティを取得し、さらに BarcodeResult.ToString() メソッドを呼び出して、これらすべてが同じ値を返すことを示す結果をコンソールに出力します。
要するに、IronBarcodeはユーザーがバーコードに関するさまざまな操作を行うための完璧なAPIです。書き込みやデコードに限りません。 さまざまな出力データ形式がサポートされているため、ユーザーはIronBarcodeによって返される BarcodeResult オブジェクトを使用してさらに多くのことを行うことができます。
よくある質問
C#バーコード読み取りはどのような出力形式をサポートしていますか?
IronBarcodeはBarcodeImage、BarcodeType、BinaryValue、座標、寸法、ページ番号、向き、テキスト、値のプロパティを含む複数の出力フォーマットを提供します。これらのフォーマットにより、様々な.NETアプリケーションでバーコードデータを包括的に処理することができます。
たった1行のコードでBarCodeの値を読み取るには?
IronBarcodeを使用すると、次のように1行でバーコードを読み取ることができます: var result = IronBarCode.BarcodeReader.Read('input.png'); これですぐにresult[0].Valueとresult[0].BarcodeTypeを通してバーコードの値とタイプにアクセスできます。
BarcodeResultで利用可能なプロパティは何ですか?
IronBarcodeのBarcodeResultオブジェクトは、BarcodeImage、BarcodeType、BinaryValue、座標、Height & Width、PageNumber、Barcode、PageOrientation、Text、Valueなどのプロパティを含み、バーコード処理ワークフローに包括的なデータを提供します。
読み取り後にバーコード画像を抽出して保存できますか?
はい、IronBarcodeは検出されたバーコードをAnyBitmapオブジェクトとしてBarcodeImageプロパティに格納します。このオブジェクトを取得して画像をさらに処理したり、永続的なコピーとして保存したりできるので、バーコード画像を抽出するための追加コードが不要になります。
バーコードの座標と寸法にアクセスするにはどうすればよいですか?
IronBarcodeは、検出された各バーコードの高さと幅の寸法だけでなく、xとyの位置を含む座標データを提供します。これらのプロパティはBarCodeResultオブジェクトを通してアクセスすることができ、正確なバーコード位置のトラッキングが可能です。
TextプロパティとValueプロパティの違いは何ですか?
IronBarcodeでは、TextプロパティとValueプロパティの両方にバーコードのデータ内容が含まれます。これらのプロパティはBarCodeResultオブジェクトの一部であり、デコードされたバーコード情報を取得するために互換的に使用することができます。
バーコードが見つかったページを特定できますか?
はい、IronBarcodeはBarCodeResultオブジェクトにPageNumberプロパティを含み、複数ページのドキュメントやPDFのどのページに検出されたバーコードが含まれているかを正確に識別することができます。
検出されたバーコードの種類を識別するにはどうすればよいですか?
IronBarcodeのBarcodeResultオブジェクトのBarcodeTypeプロパティは、検出された特定のバーコードフォーマット(QRコード、Code 128など)を識別し、アプリケーションでフォーマット固有の処理を可能にします。

