データ形式を出力する方法
バーコードを単に読み取ってコンソールに値を表示するだけでなく、IronBarcodeはそれ以上の機能を提供します。 それは、読んだ結果をさらに処理するための複数の出力形式を提供します。 これらの形式には、バーコード画像、バーコードタイプ、BinaryValue
、座標、高さ、幅、ページ番号、バーコード、ページの向き、テキスト、および値といったプロパティが含まれます。
ユーザーはプログラム内でこれらのプロパティをさらに操作することができます。 これらのプロパティの使用方法と、それらが役立つユースケースについて探ってみましょう。
データ形式を出力する方法
- バーコードを読み取るためのC#ライブラリをダウンロード
- PDFおよび画像をバーコード検出のために準備する
- 検出されたバーコードのタイプと画像にアクセスする
- バーコードの x 座標と y 座標、ならびに高さと幅を取得します。
- バーコードのテキストと値を読み取ります。
IronBarcodeを始める
今日から無料トライアルでIronBarcodeをあなたのプロジェクトで使い始めましょう。
出力形式と使用ケース
BarcodeResult
は、さまざまな便利なプロパティを保存します。 以下のプロパティは次のとおりです:
- バーコードイメージ
-
BarcodeType(バーコードタイプ)
バイナリ値
- 座標、高さと幅
ページ番号
- 「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
を呼び出してバーコードの読み取りを行いました。()入力画像のmethod
メソッド。 これは、画像内のすべてのバーコードを読み取った結果を保持する 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つのライブラリを組み合わせて使用しました。 まず、画像ファイルに含まれるバーコードを読み取るために BarcodeReader.Read
メソッドを使用して、その後 BarcodeResult
オブジェクトを取得し、そのプロパティを抽出します。()メソッド 同時に、入力画像ファイルもAnyBitmap
オブジェクトに変換する必要があり、画像に対して秘匿(redaction)メソッドを使用し適用できるようにします。 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
の値を取得することで、ページの向きとバーコードの回転を取得できることを証明します。 この機能は主にデバッグ目的で有用です。
次の内容にご注意ください。
PageOrientation
は を返しますPageOrientation
オブジェクトには、PortraitまたはLandscapeが含まれます。テキストと値
もちろん、IronBarcodeを使用する際にユーザーが取得したい主なプロパティは、そのvalueとtextを知ることです。 これら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
オブジェクトを使用して、はるかに多くのことができます。