データ形式を出力する方法
バーコードを単に読み取ってコンソールに値を表示するだけでなく、IronBarcodeはそれ以上の機能を提供します。 それは、読んだ結果をさらに処理するための複数の出力形式を提供します。 これらのフォーマットには、バーコード画像、バーコードタイプ、BinaryValue
、座標、高さ、幅、ページ番号、バーコード、ページの向き、テキスト、値などのプロパティが含まれます。
ユーザーはプログラム内でこれらのプロパティをさらに操作することができます。 これらのプロパティの使用方法と、それらが役立つユースケースについて探ってみましょう。
データ形式を出力する方法
- バーコードを読み取るためのC#ライブラリをダウンロード
- PDFおよび画像をバーコード検出のために準備する
- 検出されたバーコードの種類と画像にアクセス
- バーコードの x および y 座標、さらにその高さと幅を取得する
- バーコードのテキストと値を読む
IronBarcodeを始める
今日から無料トライアルでIronBarcodeをあなたのプロジェクトで使い始めましょう。
出力形式と使用ケース
BarcodeResult
は、さまざまな有用なプロパティを保存します。 以下のプロパティは次のとおりです:
- バーコード画像
- バーコードタイプ
- バイナリ値
- 座標、高さと幅
ページ番号
BarCode
とPageOrientation
- テキストと値
バーコード画像
IronBarcodeが画像に対して読み取りプロセスを行うと、画像内で見つかったバーコードはBarcodeResult
にAnyBitmap
型のBarcodeImage
プロパティとして保存されます。 BarcodeImage
プロパティは、検出されたバーコード画像を格納します。 ユーザーはこのオブジェクトを取得して、画像をさらに処理したり、恒久的なコピーとして保存したりすることができます。 これにより、画像からバーコード画像を抽出するために追加のコードを書く必要がなく、効率的で使いやすい方法が提供されます。
以下のコードスニペットは、この出力形式の使用例を示していますので、見てみましょう。
: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 ドキュメント内で検出されたバーコードからマルチページ TIFF 画像を作成するように設計されています。 まず、サンプルPDF内のバーコードをスキャンまたは検出します。 次に、AnyBitmap
リストを作成し、BarcodeImageプロパティからの情報を保存します。 最後に、このリストを利用して、CreateMultiFrameTiff
メソッドを使用してマルチページTIFFを生成します。
次の内容にご注意ください。
BarcodeResult
のBarcodeImage
プロパティは、読み取り中に見つかったバーコードの画像のみを保存し、入力画像全体を保存するわけではありません。バーコードタイプ
このプロパティは、入力画像または文書に存在するバーコードの種類を特定するのに役立ちます。 ただし、この機能の制限は、画像内のバーコードの種類が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
を取得し、バーコードの値とバーコードの種類をコンソールに出力します。
バイナリ値
IronBarcodeを使用すると、ユーザーはBarcodeResult
オブジェクトからBinaryValue
プロパティを取得することで、バーコード値のバイト配列も取得できます。 これにより、ユーザーはプログラム内でバーコードの値をさらに操作することができます。
以下のコードスニペットは、バイナリデータとしてバーコードの値を取得するユースケースの一例を示しています。
: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
上記のコードスニペットを観察すると、画像内の複数のバーコードを別々の新しいQRコード画像に変換するシンプルなプログラムを作成しました。 初めに、サンプルPNG画像にあるバーコードをスキャンします。 これらのバーコードを検出したら、それらを反復処理し、BinaryValueプロパティにアクセスして、新しいQRコードバーコードを作成するために使用します。
バーコードの座標、高さ、幅
ユーザーがアクセスできるBarcodeResult
オブジェクトの別のプロパティは、画像ファイルまたはドキュメント内のバーコードの座標です。これには、X1、Y1、X2、Y2、およびHeight、Widthが含まれます。 これらのプロパティは、ユーザーがバーコードの位置や寸法に関する情報を取得する必要がある場合に非常に便利です。 バーコードの位置と寸法を強調するために図を使用しましょう。
: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

削除前

修正後
上記のコードスニペットは、画像ファイルに含まれる複数のバーコードを編集するために使用されます。これを実現するために、IronBarcodeとIronDrawingの2つのライブラリを組み合わせて使用しました。 BarcodeResult
オブジェクトを取得し、そのプロパティを抽出するには、まずBarcodeReader.Read()
メソッドを使用して画像ファイル内の利用可能なバーコードを読み取ります。 同時に、入力画像ファイルもAnyBitmap
オブジェクトに変換する必要があります。これは画像に対しマスキングメソッドを使用し適用するためです。 BarcodeResults
オブジェクトを取得したら、ループを適用してそれを反復処理し、画像内のすべてのバーコードの X1、Y1、Width、および Height を取得し、それらを AnyBitmap.Redact()
メソッドの CropRectangle
プロパティとして使用することができます。
ページ番号
ユーザーはバーコードが見つかったページ番号を取得することもできます。 これは、複数のバーコードを含む多ページ文書を使用しているユーザーにとって便利な機能であり、文書内で見つかったバーコードの位置を確認してさらなる処理を行う必要がある場合に役立ちます。
以下のコードスニペットを見てみましょう:
: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ドキュメントで見つかったバーコードの値とそのページ番号をプログラムが返す必要がある場合の一例を示しています。 上記のコードスニペットでは、BarcodeReader.ReadPdf()
メソッドを使用して複数ページのPDFドキュメント内のバーコードを読み取り、ドキュメント内で見つかったすべての BarcodeResult
を格納する BarcodeResults
オブジェクトを返しました。 ループを適用し、オブジェクト内の各項目を反復処理して、バーコードの値とバーコードが見つかったページ番号を取得します。 この使用例以外でも、ユーザーはこのプロパティを使用して、ドキュメント内のすべてのバーコードが読み取れるかどうかをデバッグすることができます。
次の内容にご注意ください。
バーコードの回転とページの向き
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
の値を取得することで、ページの向きとバーコードの回転を確認できることを証明しました。 この機能は主にデバッグ目的で有用です。
[{i:(IronBarcodeはバーコードを0、90、180、270度の回転でのみ読み取ることができます。 IronBarcodeは、バーコードに指定された値以外の回転値がある場合、値を返しません。 PageOrientation
はPageOrientation
オブジェクトを返します。それはPortraitまたはLandscapeで構成されています。
テキストと値
もちろん、ユーザーが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
を反復処理した後、値とテキストプロパティを取得した結果をコンソールに出力し、BarcodeResult.ToString()
メソッドを呼び出して、これらすべてが同じ値を返したことを示します。
要するに、IronBarcodeはバーコードに関する複数の操作を実行するための完璧なAPIであり、バーコードの作成やデコードに限定されません。 さまざまな出力データ形式がサポートされているため、ユーザーはIronBarcodeによって返されるBarcodeResult
オブジェクトでさらに多くのことができます。