C# でバーコードデータ形式を出力する方法

How to Output Data Formats

This article was translated from English: Does it need improvement?
Translated
View the article in English

単にバーコードを読み取ってコンソールに値を表示するのではなく、IronBarcodeはもっと多くの機能を提供します。 読み取った結果をさらに処理するためのいくつかの出力形式を提供します。 これらの形式には、バーコード画像、バーコードタイプ、BinaryValue、座標、高さ、幅、ページ番号、バーコード、ページの向き、テキスト、および値といったプロパティが含まれます。

ユーザーはプログラム内でこれらのプロパティをさらに操作できます。 これらのプロパティの使用方法とそれらが役立つケースについて探ってみましょう。

クイックスタート: 一行でバーコードの値とタイプを読み取る

この例は、画像からIronBarcodeを使ってバーコードを読み取るのがいかに簡単であるかを示しています - 一行でロードし、その後すぐにバーコードの値とタイプを印刷します。 迅速に始めるのに最適です。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    var result = IronBarCode.BarcodeReader.Read("input.png");
    Console.WriteLine($"Value: {result[0].Value}, Type: {result[0].BarcodeType}");
  3. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小限のワークフロー(5ステップ)

  1. バーコード読み取りのためのC#ライブラリをダウンロード
  2. バーコード検出用にPDFと画像を準備
  3. 検出されたバーコードタイプと画像にアクセス
  4. バーコードのx座標とy座標、さらにその高さと幅を取得
  5. バーコードのテキストと値を読み取る

出力形式とユースケース

BarcodeResult はさまざまな有用なプロパティを保持します。 これらのプロパティは以下に列挙されます:

  • BarcodeImage
  • BarcodeType
  • BinaryValue
  • 座標、高さと幅
  • PageNumber
  • BarcodePageOrientation
  • テキストと値

バーコード画像

IronBarcodeが画像上で読み取りプロセスを行うと、画像内で見つかったバーコードはBarcodeResultBarcodeImageプロパティとしてAnyBitmapタイプで保存されます。 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")
$vbLabelText   $csharpLabel

上記のコードスニペットは、この出力形式のユースケースの1つを示しています。 具体的には、PDFドキュメント内で検出されたバーコードから多ページTIFF画像を作成するために設計されています。 最初に、サンプルPDFでバーコードをスキャンまたは検出します。 次に、BarcodeImageプロパティからの情報を保存するAnyBitmapのリストを作成します。 最後に、このリストを使用してCreateMultiFrameTiffメソッドを使用してマルチページのTIFFを生成します。

ご注意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
$vbLabelText   $csharpLabel

上記のコードスニペットから、入力画像に対して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
$vbLabelText   $csharpLabel

上記のコードスニペットを観察すると、画像内の複数のバーコードを新しいバイナリエンコードファイルに変換する直感的なプログラムを作成しました。 最初に、サンプルのPNG画像でバーコードをスキャンします。 これらのバーコードを検出した後、それらを繰り返し処理し、BinaryValueプロパティにアクセスして、新しいバイナリファイルを作成します。

バーコードの座標、高さと幅

ユーザーがアクセスできるBarcodeResultオブジェクトの別のプロパティとして、画像ファイルやドキュメント内のバーコードの座標 (X1, Y1, および X2, Y2) と、その高さおよび幅があります。 これらのプロパティは、ユーザーがバーコードの位置と寸法に関する情報を取得する必要がある場合に非常に便利です。

バーコードの位置と寸法を強調するためのイラストを使用しましょう。

: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
$vbLabelText   $csharpLabel
class="competitors-section__wrapper-even-1">
Before redaction
Redacted image

上記のコードスニペットは、画像ファイルに見つかった複数のバーコードに対して赤フィルターを使用するために使用されます。これを実現するために、IronBarcodeとIronDrawingの2つのライブラリを組み合わせて使用します。 BarcodeResultオブジェクトを取得してそこからプロパティを抽出するために、最初にBarcodeReader.Read()メソッドを使用して画像ファイルにあるバーコードを読み取ります。 Redactメソッドを画像に適用するためには、入力画像ファイルを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
$vbLabelText   $csharpLabel

上記の簡単なコードスニペットは、ユーザーがプログラムにマルチページPDFドキュメントで見つかったバーコードの値とそれぞれのページ番号を返すよう要求するユースケースの一例を示しています。 上記のコードスニペットでは、BarcodeReader.ReadPdf()メソッドを使用してマルチページPDFドキュメント内のバーコードを読み取り、ドキュメント内で見つかったすべてのBarcodeResultを保存するBarcodeResultsオブジェクトを返します。 それぞれのオブジェクト内の項目を繰り返し処理して、バーコードの値とそのバーコードが見つかったページ番号を取得します。 このユースケース以外にも、ユーザーはこのプロパティを使用して、ドキュメント内のすべてのバーコードが正しく読み取られたかどうかをデバッグすることができます。

ご注意このプロパティから返される値は1ベースです。つまり、最初のページは常に1であり、0ではありません。

バーコードの回転とページの向き

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
$vbLabelText   $csharpLabel

上記の簡単なコードスニペットは、ユーザーがBarcodeResult.PageOrientationBarcodeResult.Rotationの値を取得することでページの向きとバーコードの回転を取得できることを証明するために添付されたサンプルPDF入力で実行されました。 この機能は主にデバッグ目的で役立ちます。

ご注意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
$vbLabelText   $csharpLabel

上記のコードスニペットから、ユーザーは数行のコードを使用するだけでIronBarcodeを使用して画像内のバーコードを読み取ることができます。 BarcodeReader.Read()メソッドによって返されたBarcodeResultsを繰り返し処理した後、コンソールに値とテキストプロパティを取得した結果を出力し、すべてのこれらが同じ値を返したことを示すBarcodeResult.ToString()メソッドを呼び出します。

要するに、IronBarcodeはユーザーがバーコードに関するさまざまな操作を行うための完璧なAPIです。書き込みやデコードに限りません。 さまざまな出力データ形式に対応しており、ユーザーはIronBarcodeによって返されるBarcodeResultオブジェクトでさらに多くのことができます。

よくある質問

.NET C#ではバーコード処理のためにどのような出力データ形式が利用できますか?

IronBarcodeは、バーコードの画像、種類、`BinaryValue`、座標、高さ、幅、ページ番号、方向、テキスト、値など、さまざまな出力データ形式を.NET C#で提供します。これらの形式は多様なバーコード関連の操作を可能にします。

C#を使用してバーコード画像をキャプチャし保存する方法は?

IronBarcodeの`BarcodeImage`プロパティを使用することで、バーコード画像をキャプチャし保存することができ、検出されたバーコードから複数ページのTIFFファイルを作成するようなタスクに役立ちます。

C#で画像内のバーコードの種類を識別する方法は?

IronBarcodeの`BarcodeType`プロパティを使用することで、画像内のバーコードの種類を識別でき、サポートされるバーコードタイプの処理がしやすくなります。

バーコードデータをバイト配列に変換する方法は?

IronBarcodeの`BinaryValue`プロパティにアクセスすることで、バーコードデータをバイト配列に変換でき、データのさらなる操作や保存に使用できます。

画像内のバーコードの座標と寸法を取得する方法は?

IronBarcodeは、`BarcodeResult`オブジェクト内で座標、高さ、幅のプロパティを提供し、画像内のバーコードの位置と寸法に関する詳細を取得できます。

バーコードデータを使用して複数ページのドキュメントを管理する方法は?

IronBarcodeの`PageNumber`および`PageOrientation`プロパティは、各バーコードの位置とその方向を識別することで、複数ページのドキュメントを効率的に処理するのに役立ちます。

C#のバーコードにおける`text`と`value`プロパティの違いは何ですか?

IronBarcodeでは、`text`と`value`プロパティはバーコードに対して同じ出力を返します。`BarcodeResult.ToString()`メソッドを使用して同様の結果を得られます。

IronBarcodeでバーコードの回転検出に制限はありますか?

はい、IronBarcodeは0度、90度、180度、および270度の回転を持つバーコードを読み取ることができます。その他の回転角度のバーコード検出はサポートしていません。

Hairil Hasyimi Bin Omar
ソフトウェアエンジニア
すべての優れたエンジニアのように、ハイリルは熱心な学習者です。彼はC#、Python、およびJavaの知識を磨いており、その知識を利用してIron Software全体のチームメンバーに価値を追加しています。ハイリルはマレーシアのマラ工科大学からIron Softwareチームに参加し、化学およびプロセス工学の学士号を取得しました。
準備はいいですか?
Nuget ダウンロード 1,935,276 | バージョン: 2025.11 ただ今リリースされました