如何定義條碼裁剪區域以加快讀取

Hairil related to 如何定義條碼裁剪區域以加快讀取
海里海西米·賓·奧馬
2023年11月7日
已更新 2024年12月10日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

IronBarcode 最重要的功能之一是用戶可以指定裁剪區域。 此功能的目標是讓 IronBarcode 只讀取圖片中由 Crop Region 指定的特定條碼或區域,方法是使用 IronSoftware.Drawing.Rectangle 對象。 使用此功能不僅可以減少閱讀錯誤,還可以提高閱讀效率。

開始使用 IronBarcode

立即在您的專案中使用IronBarcode,並享受免費試用。

第一步:
green arrow pointer


找到圖像中的裁剪區域座標和大小

有許多方法可以幫助使用者在圖片中找到一個點的坐標。 其中之一是使用電腦中的「畫圖」應用程序來加載圖像。 要獲取裁剪區域的第一個座標,將游標移動到希望的第一個位置,這將是Rectangle的左上角,並記下應用程式在螢幕左下方提供的 x,y 座標。 然後,找到第二個點,這將是Rectangle的右下角。 請參考下面的圖片以獲得更清晰的理解。

CropRegion 參考

然後可以將坐標值用作Rectangle對象的屬性。 物體的寬度可以定義為x2 - x1,而高度可以定義為y2 - y1

:path=/static-assets/barcode/content-code-examples/how-to/set-crop-region-instantiate-CropRegion.cs
using IronBarCode;

int x1 = 62;
int y1 = 29;
int x2 = 345;
int y2 = 522;

IronSoftware.Drawing.Rectangle crop1 = new IronSoftware.Drawing.Rectangle(x: x1, y: y1, width: x2-x1, height: y2-y1);

應用 CropRegion 和讀取條碼

一旦我們完成了定義 IronBarcode 讀取的 CropRegions 的繁重工作後,我們就可以將該對象應用到 BarcodeReaderOptions 中,作為其他設置中的一個屬性,然後可以在 BarcodeReader.Read() 方法中用作參數。 下面的代碼片段顯示

:path=/static-assets/barcode/content-code-examples/how-to/set-crop-region-apply-CropRegion.cs
using IronBarCode;
using System;

int x1 = 62;
int y1 = 29;
int x2 = 345;
int y2 = 522;

IronSoftware.Drawing.Rectangle crop1 = new IronSoftware.Drawing.Rectangle(x: x1, y: y1, width: x2 - x1, height: y2 - y1);

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    CropArea = crop1
};

var result = BarcodeReader.Read("sample.png", options);
foreach (var item in result)
{
    Console.WriteLine(item.Value);
}

從上面的程式碼片段中,我們在BarcodeReaderOptions物件中,將實例化的Rectangle用作CropArea屬性。 然後,我們將此BarcodeReaderOptions物件作為BarcodeReader.Read()方法的參數,以在影像中套用CropArea並讀取其中的條碼。

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