C#およびVB.NETアプリケーションでのバーコードとQRコード
C#や他のすべての.NET言語でバーコードを読み書きすることは、IronBarcodeソフトウェアライブラリを使用すれば簡単なプロセスです。
IronBarcode のインストール
旅の最初のステップは、IronBarcodeをインストールすることであり、これはNuGetからダウンロードすることによって実行できます。または DLLをダウンロードする.
IronBarcodeのNuGetパッケージをインストールするには、Visual StudioのNuGetパッケージマネージャーを使用できます。
Install-Package BarCode
または、dotnet CLIを使用してインストールすることもできます。
dotnet add package BarCode
NuGetパッケージに関する詳細情報は、で見つけることができます。 NuGetウェブサイト.
バーコードやQRコードの読み取り
IronBarcodeを使用すると、バーコードを読み取るのにコード1行だけで済みます。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-1.cs
using IronBarCode;
BarcodeResults results = BarcodeReader.Read("QuickStart.jpg");
if (results != null)
{
foreach (BarcodeResult result in results)
{
Console.WriteLine(result.Text);
}
}
Imports IronBarCode
Private results As BarcodeResults = BarcodeReader.Read("QuickStart.jpg")
If results IsNot Nothing Then
For Each result As BarcodeResult In results
Console.WriteLine(result.Text)
Next result
End If
この1行のコードで、入力ドキュメントからあらゆる種類のバーコードを検出しスキャンすることができ、卓越したパフォーマンスを発揮します。必要なすべてが1ステップに集約されています。!このメソッドは、JPEG、PNG、BMPなどの画像、およびPDFやGIF、TIFFなどのマルチフレーム形式を含む、幅広い画像形式をサポートしています。 さらに高速化を求める方、最適化されたメモリ使用、または読み取り精度の向上を必要とする方には、特定のニーズに合わせてパフォーマンスを細かく調整できるカスタマイズ可能な設定オプションが用意されています。
一つには、読み取り速度を向上させるために、Speed
設定が設定された BarcodeReaderOptions
オブジェクトを追加することができます。 デフォルトでは「Balanced」に設定されていますが、逆色やバーコードの回転のチェックを省略する「Faster」オプションもあります。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-2.cs
using IronBarCode;
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
Speed = ReadingSpeed.Faster
};
BarcodeResults result = BarcodeReader.Read("QuickStart.jpg", myOptionsExample);
if (result != null)
{
Console.WriteLine(result.First().Text);
}
Imports IronBarCode
Private myOptionsExample As New BarcodeReaderOptions() With {.Speed = ReadingSpeed.Faster}
Private result As BarcodeResults = BarcodeReader.Read("QuickStart.jpg", myOptionsExample)
If result IsNot Nothing Then
Console.WriteLine(result.First().Text)
End If
ScanMode
を OnlyBasicScan
に設定すると、バーコード検出アルゴリズムがドキュメントをスキャンするのをスキップできます。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-3.cs
using IronBarCode;
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
ScanMode = BarcodeScanMode.OnlyBasicScan
};
BarcodeResults result = BarcodeReader.Read("QuickStart.jpg", myOptionsExample);
if (result != null)
{
Console.WriteLine(result.First().Text);
}
Imports IronBarCode
Private myOptionsExample As New BarcodeReaderOptions() With {.ScanMode = BarcodeScanMode.OnlyBasicScan}
Private result As BarcodeResults = BarcodeReader.Read("QuickStart.jpg", myOptionsExample)
If result IsNot Nothing Then
Console.WriteLine(result.First().Text)
End If
試してみる他の設定としては、IronBarcodeがすべてのバーコードタイプをスキャンする必要がないようにスキャンするバーコード形式を指定することや、画像の特定の領域をトリミングすることで、IronBarcodeが処理する画像データを減らすことが挙げられます。 複数のバーコードを期待するオプションもあり、IronBarcodeは1つだけを期待する場合には、複数のバーコードの検出およびデコードのプロセスを省略することができます。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-4.cs
using IronBarCode;
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
CropArea = new System.Drawing.Rectangle(100, 200, 300, 400),
};
BarcodeResults result = BarcodeReader.Read("QuickStart.jpg", myOptionsExample);
if (result != null)
{
Console.WriteLine(result.First().Text);
}
Imports IronBarCode
Private myOptionsExample As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
.CropArea = New System.Drawing.Rectangle(100, 200, 300, 400)
}
Private result As BarcodeResults = BarcodeReader.Read("QuickStart.jpg", myOptionsExample)
If result IsNot Nothing Then
Console.WriteLine(result.First().Text)
End If
バコードの作成
IronBarcodeを使用してバーコードを作成するには、BarcodeWriter
クラスを使用します。 BarcodeWriter
を使用すると、バーコードの作成もシンプルな1行のコードでできます。入力データとバーコード形式を入力するだけで完了です。! 次に、バーコード画像データをメモリまたはファイルとして保存できます。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-5.cs
using IronBarCode;
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.SaveAsImage("myBarcode.png");
Imports IronBarCode
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.SaveAsImage("myBarcode.png")
バーコードのスタイリング
IronBarcodeは、バーコードの視覚的表現を操作するためのいくつかのオプションを提供しています。 バーコードのスタイリングに対応した機能には、リサイズ、出力画像のマージン設定、前景色と背景色の変更、注釈の追加が含まれます。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-7.cs
using IronBarCode;
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.AddAnnotationTextAboveBarcode("Product URL:");
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.SetMargins(100);
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple);
// All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png");
Imports IronBarCode
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.AddAnnotationTextAboveBarcode("Product URL:")
myBarcode.AddBarcodeValueTextBelowBarcode()
myBarcode.SetMargins(100)
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple)
' All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png")
バーコードのHTMLとしてのエクスポート
生成されたバーコードからHTMLをエクスポートする便利な機能もあります。 バーコードを外部アセットを持たない独立したHTMLドキュメント、独立したHTMLタグ、またはデータURIとしてエクスポートすることができます。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-8.cs
using IronBarcode;
myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8);
myBarcode.SaveAsHtmlFile("myBarcode.html");
Imports IronBarcode
myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8)
myBarcode.SaveAsHtmlFile("myBarcode.html")
QRコードの生成
IronBarcodeを使用してQRコードを生成する際には、代わりにQRCodeWriter
クラスを使用することもできます。 このクラスは、QRコード専用のカスタマイズ設定をさらに提供します。例えば、エラー訂正レベルを設定したり、QRの中央に画像を追加したりすることができます。
:path=/static-assets/barcode/content-code-examples/get-started/get-started-9.cs
using IronBarCode;
using IronSoftware.Drawing;
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo);
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf");
Imports IronBarCode
Imports IronSoftware.Drawing
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo)
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf")
サポートされるバーコード形式
IronBarcodeは、読み取りと書き込みの両方において、広く使用されている多様なバーコード形式をサポートしています。
- QR、マイクロQR、および長方形マイクロQR(rMQR)** コード。
- Aztec、Data Matrix、MaxiCode、PDF417などの他の二次元バーコード。
- Databarのような積層線形バーコード。
- 従来の1次元バーコード形式としては、UPC-A、UPC-E、EAN-8、EAN-13、Codabar、ITF、MSI、およびPlesseyがあります。
なぜIronBarcodeを選ぶのか?
IronBarcodeは、.NET用のバーコードを読み書きするための開発者向けに、使いやすく親しみやすいAPIを提供しており、実際のユースケースにおいて精度、正確性、速度を最適化します。
たとえば、BarcodeWriter
クラスは UPCA と UPCE のバーコードの「チェックサム」を自動的に検証および修正します。 また、特定の数値フォーマットに対して短すぎる数字を「ゼロ埋め」することもできます。 データが指定されたデータ形式に適していない場合、IronBarcodeは、使用できるより適切なバーコード形式について開発者に指示します。
IronBarcodeは、ドキュメントからのバーコード読み取りと、現実世界で撮影された画像からのバーコード読み取りの両分野で優れています。 ライブラリには、バーコードが読み取られる確率を最大化するための画像前処理技術がいくつか含まれており、自動回転や画像ノイズ除去などがあります。
これから進む
IronBarcodeを最大限に活用するために、このドキュメントセクション内のチュートリアルを読むことをお勧めします。また、当社のウェブサイトをご覧ください。GitHub(ギットハブ).