C#でバーコードの向きを修正する方法
IronBarcodeは、内蔵のAutoRotate機能によりバーコードの向きを自動的に補正し、手動で画像を回転させることなくあらゆる角度のバーコードを検出して読み取るため、傾いたり回転したりした画像からでもバーコードを正確に読み取ることができます。
バーコードの向きとは、製品や文書に印刷または表示される際の角度を指します。 さまざまなレイアウトやデザイン要件に合わせて、異なる角度に調整できます。 最も一般的な向きは水平で、これはバーコードが左から右に配置されるという標準で最も広く使われている形式です。 非ゼロの向きの角度は、ライブラリが値を検出して取得するための課題を引き起こします。 IronBarcodeは、バーコードとQRコードのために非ゼロの向きを自動検出して修正します。
クイックスタート: 1行で画像の自動回転補正
IronBarcodeのAutoRotateオプション(デフォルトで有効)を使用することで、画像が回転してもバーコードを正確に読み取ることができます。
最小限のワークフロー(5ステップ)
- バーコードの向きを修正するためのC#ライブラリをダウンロードする
- Set the **`AutoRotate`** property to true
- ターゲットのバーコードとQRコードをインポート
- 読み取りオプションでバーコードとQRコードを読む
- 結果のバーコードの値を取得
アプリケーションの BarCode の向きを修正するにはどうすればよいですか?
自動方向補正を適用するには、 BarcodeReaderOptions の AutoRotateプロパティを true に設定します。 このプロパティはデフォルトでtrueに設定されているため、何もする必要はありません。 非ゼロ向きのバーコード画像の読み取りは自動で機能するはずです。
AutoRotate 機能は、QR コード、データマトリックス、従来のリニアバーコードなど、さまざまなバーコード形式を扱う場合に特に便利です。 画像から BarCode を読み取る場合でも、PDFドキュメントからスキャンする場合でも、向きの補正は信頼できる結果を保証します。
以下の画像をサンプルとして使用しましょう。 Download the following 20° rotation and 45° rotation sample images.
20°回転
45°回転
自動回転を実装するにはどのようなコードが必要ですか?
:path=/static-assets/barcode/content-code-examples/how-to/image-orientation-correct-autorotate.cs
using IronBarCode;
using System;
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
// Turn on auto rotation in ML detection
AutoRotate = true,
};
var results = BarcodeReader.Read("rotate20.png", myOptionsExample);
// Print out the value
Console.WriteLine(results[0].Value);
Imports IronBarCode
Imports System
Private myOptionsExample As New BarcodeReaderOptions() With {.AutoRotate = True}
Private results = BarcodeReader.Read("rotate20.png", myOptionsExample)
' Print out the value
Console.WriteLine(results(0).Value)
AutoRotate機能は、高度な機械学習アルゴリズムを活用し、バーコードの向きを自動的に検出します。 これは、単一の画像内の複数の BarCode を扱う場合や、向きが異なる画像のバッチを処理する場合に特に価値があります。
さまざまな回転角度を扱う
IronBarcodeの方向補正は様々な回転角度をシームレスに処理します。 異なる回転角度で BarCode を読み取る例を示します:
using IronBarCode;
using System;
using System.Collections.Generic;
// Process multiple rotated images
var rotatedImages = new List<string> { "rotate20.png", "rotate45.png", "rotate90.png" };
var options = new BarcodeReaderOptions
{
AutoRotate = true,
// Combine with other reading optimizations
Speed = ReadingSpeed.Balanced,
ExpectMultipleBarcodes = false
};
foreach (var imagePath in rotatedImages)
{
var results = BarcodeReader.Read(imagePath, options);
if (results.Length > 0)
{
Console.WriteLine($"Image: {imagePath} - Barcode Value: {results[0].Value}");
Console.WriteLine($"Barcode Type: {results[0].BarcodeType}");
Console.WriteLine($"Rotation Applied: {results[0].WasRotated}");
}
}
using IronBarCode;
using System;
using System.Collections.Generic;
// Process multiple rotated images
var rotatedImages = new List<string> { "rotate20.png", "rotate45.png", "rotate90.png" };
var options = new BarcodeReaderOptions
{
AutoRotate = true,
// Combine with other reading optimizations
Speed = ReadingSpeed.Balanced,
ExpectMultipleBarcodes = false
};
foreach (var imagePath in rotatedImages)
{
var results = BarcodeReader.Read(imagePath, options);
if (results.Length > 0)
{
Console.WriteLine($"Image: {imagePath} - Barcode Value: {results[0].Value}");
Console.WriteLine($"Barcode Type: {results[0].BarcodeType}");
Console.WriteLine($"Rotation Applied: {results[0].WasRotated}");
}
}
Imports IronBarCode
Imports System
Imports System.Collections.Generic
' Process multiple rotated images
Dim rotatedImages As New List(Of String) From {"rotate20.png", "rotate45.png", "rotate90.png"}
Dim options As New BarcodeReaderOptions With {
.AutoRotate = True,
' Combine with other reading optimizations
.Speed = ReadingSpeed.Balanced,
.ExpectMultipleBarcodes = False
}
For Each imagePath In rotatedImages
Dim results = BarcodeReader.Read(imagePath, options)
If results.Length > 0 Then
Console.WriteLine($"Image: {imagePath} - Barcode Value: {results(0).Value}")
Console.WriteLine($"Barcode Type: {results(0).BarcodeType}")
Console.WriteLine($"Rotation Applied: {results(0).WasRotated}")
End If
Next
パフォーマンスの考慮事項
AutoRotate はデフォルトで有効になっていますが、そのパフォーマンスへの影響を理解することで、バーコード読み取りワークフローを最適化するのに役立ちます。 この機能はIronBarcodeの読み取り速度オプションと効率的に連動し、アプリケーションのニーズに応じて精度とパフォーマンスのバランスをとることができます。
高速処理を必要とするアプリケーションの場合、AutoRotate を他の最適化手法と組み合わせることができます。
var fastReadOptions = new BarcodeReaderOptions
{
AutoRotate = true,
Speed = ReadingSpeed.Faster,
// Specify expected barcode types to improve performance
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
// Define crop region if barcode location is predictable
CropArea = new System.Drawing.Rectangle(100, 100, 300, 300)
};
var fastReadOptions = new BarcodeReaderOptions
{
AutoRotate = true,
Speed = ReadingSpeed.Faster,
// Specify expected barcode types to improve performance
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
// Define crop region if barcode location is predictable
CropArea = new System.Drawing.Rectangle(100, 100, 300, 300)
};
Dim fastReadOptions As New BarcodeReaderOptions With {
.AutoRotate = True,
.Speed = ReadingSpeed.Faster,
' Specify expected barcode types to improve performance
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
' Define crop region if barcode location is predictable
.CropArea = New System.Drawing.Rectangle(100, 100, 300, 300)
}
画像補正機能との統合
AutoRotate は IronBarcode の画像補正フィルターとシームレスに動作します。 回転している画質の悪い画像を扱う場合、複数の補正をかけることができます:
var advancedOptions = new BarcodeReaderOptions
{
AutoRotate = true,
// Apply additional image corrections
ImageFilters = new ImageFilterCollection
{
new AdaptiveThresholdFilter(),
new BrightnessFilter(1.2f),
new ContrastFilter(1.5f)
}
};
var results = BarcodeReader.Read("low-quality-rotated-barcode.jpg", advancedOptions);
var advancedOptions = new BarcodeReaderOptions
{
AutoRotate = true,
// Apply additional image corrections
ImageFilters = new ImageFilterCollection
{
new AdaptiveThresholdFilter(),
new BrightnessFilter(1.2f),
new ContrastFilter(1.5f)
}
};
var results = BarcodeReader.Read("low-quality-rotated-barcode.jpg", advancedOptions);
Imports System
Dim advancedOptions As New BarcodeReaderOptions With {
.AutoRotate = True,
' Apply additional image corrections
.ImageFilters = New ImageFilterCollection From {
New AdaptiveThresholdFilter(),
New BrightnessFilter(1.2F),
New ContrastFilter(1.5F)
}
}
Dim results = BarcodeReader.Read("low-quality-rotated-barcode.jpg", advancedOptions)
方向修正のベストプラクティス
1.デフォルトの動作: AutoRotate はデフォルトで有効になっているため、以前に無効にした場合やアクティブであることを確認したい場合を除き、通常は明示的に設定する必要はありません。
2.クロップリージョンとの組み合わせ:パフォーマンスを向上させるためにクロップ領域を使用する場合、クロップ領域が回転した BarCode を収容するのに十分な大きさであることを確認してください。
3.マルチスレッド処理:AutoRotateはスレッドセーフであり、非同期処理やマルチスレッド処理に適しているため、大量のバーコード処理アプリケーションに適しています。
4.形式固有の考慮事項: AutoRotate はサポートされているすべてのバーコード形式で動作しますが、PDF417 や Data Matrix などの一部の形式では、追加の形式固有のオプションを使用するとメリットが得られる場合があります。
多くの場合、回転の修正は十分ではなく、フィルタが必要となることがあります。 以下の記事で、画像フィルタの使い方を学んでください:"画像補正フィルターの使い方".
よくある質問
C# アプリケーションで回転した BarCode 画像を修正するにはどうすればよいですか?
IronBarcodeは内蔵のAutoRotate機能を使って回転したバーコード画像を自動的に修正します。BarCodeReaderOptionsでAutoRotateをtrueに設定するだけで(デフォルトで有効になっています)、ライブラリは手動で回転させることなく、あらゆる角度のバーコードを検出し読み取ります。
どのようなバーコードの向きを自動的に修正できますか?
IronBarcodeのAutoRotate機能は、20°、45°、90°、180°、270°の回転を含む、0度以外の向きを検出して修正することができます。この機能は、QRコード、データマトリックス、従来のリニアバーコードを含む様々なバーコードフォーマットで動作します。
傾いた BarCode を処理するために特別なコードを書く必要がありますか?
特別なコードは必要ありません。IronBarcodeのAutoRotateプロパティはデフォルトで有効になっているため、すぐに向きの修正が可能です。必要なコードは1行だけです: var result = IronBarCode.BarcodeReader.Read("rotatedImage.png");
オリエンテーション補正はPDF文書でも可能ですか?
IronBarcodeの自動回転機能は、画像だけでなくPDFドキュメントからバーコードをスキャンする際にもシームレスに機能します。向きの補正は、ソース形式に関係なく信頼性の高い結果を保証します。
自動オリエンテーション検出の原動力となる技術は何ですか?
IronBarcodeは高度な機械学習アルゴリズムを使用して、バーコードの向きを自動的に検出します。このインテリジェントなアプローチにより、傾いたり回転したりした画像からでも、手動で操作することなくバーコードを正確に読み取ることができます。

