システム描画オブジェクトからバーコードを読み取る方法

ハイリル ハシミ ビン オマル
ハイリル ハシミ ビン オマル
2023年9月13日
更新済み 2024年12月10日
共有:
This article was translated from English: Does it need improvement?
Translated
View the article in English

System.Drawing オブジェクトは、画像処理に関連するタスクにおいて .NET 開発者によって広く使用されています。 しかし、MicrosoftはSystem.Drawingのサポートを終了しました。他のオペレーティングシステム、例えばMacOSLinuxではサポートが停止され、現在はWindowsのみをサポートしています。 この重要な変更は、Windows以外のオペレーティングシステムでIronBarcodeを使用している開発者にとって多くの問題を引き起こしました。 これは、バーコードを扱うことが通常、グラフィックス画像、およびフォントのようなオブジェクトを使用することを含むためです。

この問題に対処するために、代替ソリューションとしてIronDrawingを導入しました。 この無料かつオープンソースのライブラリは、IronSoftwareによって開始され、Windows以外のオペレーティングシステムで機能させるプロセスを簡素化することを目的としています。こちらを参照してください。 これは我々のユーザーに対して使いやすい体験を提供します。 NuGetからIronBarcodeをインストールすると、IronDrawingが自動的にプロジェクトに含まれます。

IronBarcodeを始める

今日から無料トライアルでIronBarcodeをあなたのプロジェクトで使い始めましょう。

最初のステップ:
green arrow pointer


System.DrawingをAnyBitmapにキャスト

System.Drawingからバーコードを読み取るには、オブジェクトをAnyBitmapにキャストするだけです。IronDrawingは使いやすさを念頭に置いて設計されました。 その結果、IronDrawingは、System.DrawingからIronSoftware.DrawingAnyBitmapと呼ばれる画像オブジェクトへの画像オブジェクトの暗黙的キャストをサポートしています。

System.Drawingオブジェクトに加えて、他のタイプのオブジェクトからのキャストもサポートしています。対象オブジェクトには以下が含まれます:

  • System.Drawing.Bitmap
  • System.Drawing.Image
  • SkiaSharp.SKBitmap
  • SkiaSharp.SKImage
  • SixLabors.ImageSharp

    ユーザーは、上記のオブジェクトをキャストするために次のコード例を参照できます。 以下は、System.Drawing オブジェクトからIronSoftware.Drawing.AnyBitmapにバーコードの画像をキャストする方法を示すコードスニペットです。

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-cast-to-anybitmap.cs
using IronSoftware.Drawing;
using System.Collections.Generic;

List<AnyBitmap> barcodes = new List<AnyBitmap>();

// Instantiate System.Drawing.Bitmap
System.Drawing.Bitmap bitmapFromBitmap = new System.Drawing.Bitmap("test1.jpg");

// Cast from System.Drawing.Bitmap to AnyBitmap
AnyBitmap barcode1 = bitmapFromBitmap;

barcodes.Add(barcode1);

// Instantiate System.Drawing.Bitmap
System.Drawing.Image bitmapFromFile = System.Drawing.Image.FromFile("test2.png");

// Cast from System.Drawing.Image to AnyBitmap
AnyBitmap barcode2 = bitmapFromFile;

barcodes.Add(barcode2);
Imports IronSoftware.Drawing
Imports System.Collections.Generic

Private barcodes As New List(Of AnyBitmap)()

' Instantiate System.Drawing.Bitmap
Private bitmapFromBitmap As New System.Drawing.Bitmap("test1.jpg")

' Cast from System.Drawing.Bitmap to AnyBitmap
Private barcode1 As AnyBitmap = bitmapFromBitmap

barcodes.Add(barcode1)

' Instantiate System.Drawing.Bitmap
Dim bitmapFromFile As System.Drawing.Image = System.Drawing.Image.FromFile("test2.png")

' Cast from System.Drawing.Image to AnyBitmap
Dim barcode2 As AnyBitmap = bitmapFromFile

barcodes.Add(barcode2)
$vbLabelText   $csharpLabel

上記のコードスニペットから、バーコード画像を2つSystem.Drawing.BitmapおよびSystem.Drawing.Imageとして読み込みました。 それから、AnyBitmap オブジェクトに単に割り当てることによって、それらを暗黙的に AnyBitmap にキャストします。 その後、これらのオブジェクトをAnyBitmapリストに追加しました。

任意のビットマップからバーコードを読み取る

IronBarcodeは、すべてのメソッドでIronSoftware.Drawing.AnyBitmapオブジェクトを追加の設定を必要とせずに容易に受け入れることができます。 これは、Windows以外のオペレーティングシステムではサポートされていないSystem.Drawingオブジェクトを使用するIronBarcodeを使用する開発者に、使いやすさを提供します。 以下のコードスニペットはその方法を示しています。

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-read-anybitmap.cs
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Collections.Generic;

List<AnyBitmap> barcodes = new List<AnyBitmap>();

System.Drawing.Bitmap bitmapFromBitmap = new System.Drawing.Bitmap("test1.jpg");
AnyBitmap barcode1 = bitmapFromBitmap;
barcodes.Add(barcode1);

System.Drawing.Image bitmapFromFile = System.Drawing.Image.FromFile("test2.png");
AnyBitmap barcode2 = bitmapFromFile;
barcodes.Add(barcode2);

foreach (var barcode in barcodes)
{
    // Read the barcode
    var results = BarcodeReader.Read(barcode);
    foreach (var result in results)
    {
        // Output the detected barcode value
        Console.WriteLine(result.Value);
    }
}
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Collections.Generic

Private barcodes As New List(Of AnyBitmap)()

Private bitmapFromBitmap As New System.Drawing.Bitmap("test1.jpg")
Private barcode1 As AnyBitmap = bitmapFromBitmap
barcodes.Add(barcode1)

Dim bitmapFromFile As System.Drawing.Image = System.Drawing.Image.FromFile("test2.png")
Dim barcode2 As AnyBitmap = bitmapFromFile
barcodes.Add(barcode2)

For Each barcode In barcodes
	' Read the barcode
	Dim results = BarcodeReader.Read(barcode)
	For Each result In results
		' Output the detected barcode value
		Console.WriteLine(result.Value)
	Next result
Next barcode
$vbLabelText   $csharpLabel

上記のコードスニペットは、前回のコードを拡張したものです。 一度AnyBitmapリストを作成したら、そのリストを反復処理し、それぞれのAnyBitmapオブジェクトをパラメーターとしてReadメソッドを呼び出しました。これにより、IronBarcode.BarcodeResultsが返されました。 次に、返されたオブジェクトを反復処理して、バーコードの値をコンソールに出力しました。

IronSoftware.Drawing の機能領域は、画像のキャスティングだけに限定されません。 また、バーコードやQRコードのスタイリングに役立つフォントなどの他の画像処理の側面でも多用されています。 ユーザーは、QRコードをカスタマイズし、ロゴを追加する方法についてIronDrawingの活用方法を探ることができます。

ハイリル ハシミ ビン オマル
ハイリル ハシミ ビン オマル
ソフトウェアエンジニア
すべての優れたエンジニアと同じように、Hairilは熱心な学習者です。C#、Python、およびJavaの知識を洗練させ、その知識を活かしてIron Softwareのチームメンバーに価値を提供しています。Hairilはマレーシアのマラ工科大学(Universiti Teknologi MARA)で化学およびプロセス工学の学士号を取得し、Iron Softwareチームに加わりました。