如何從 System.Drawing 對象讀取條碼

Hairil related to 如何從 System.Drawing 對象讀取條碼
海里海西米·賓·奧馬
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 中被開發者廣泛用於與圖像處理相關的任務。 然而,微軟已經停止支援 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.Drawing 的影像物件,稱為 AnyBitmap

除了 System.Drawing 物件外,我們還支援從其他類型的物件轉換,包括:

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

    用戶可以參考以下程式碼範例來轉換上述物件。 下面是一段程式碼片段,展示了如何將System.Drawing objects的條碼影像轉換為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

從上面的程式碼片段中,我們將兩個條碼圖像加載為System.Drawing.BitmapSystem.Drawing.Image。 我們通過將它們賦值給AnyBitmap對象,隱式地將它們轉換為AnyBitmap。 隨後,我們將這些對象添加到 AnyBitmap 列表中。

從任何圖位圖讀取條碼

IronBarcode 可以在所有方法中直接接受 IronSoftware.Drawing.AnyBitmap 對象,無需任何額外配置。 這為使用 IronBarcode 且搭配不支援 Windows 以外操作系統的 System.Drawing 物件的開發人員提供了易用性。 以下程式碼片段示範了如何做到這一點。

: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 的功能範圍不僅僅限於轉換影像。 它也廣泛用於其他圖像處理方面,例如顏色字體,這些在美化條碼和二維碼時非常有用。 使用者可以探索我們如何運用IronDrawing來自訂和添加標誌到QR碼

Hairil related to 從任何圖位圖讀取條碼
海里海西米·賓·奧馬
軟體工程師
和所有優秀的工程師一樣,Hairil 是一位熱衷學習的人。他正在精進自己對 C#、Python 和 Java 的知識,利用這些知識為 Iron Software 團隊的成員創造價值。Hairil 從馬來西亞的馬來西亞工藝大學加入了 Iron Software 團隊,他在那裡獲得了化學和過程工程學士學位。