如何定義條碼裁剪區域以加快讀取
IronBarcode 最重要的功能之一是用戶可以指定裁剪區域。 此功能的目標是讓 IronBarcode 只讀取圖片中由 Crop Region 指定的特定條碼或區域,方法是使用 IronSoftware.Drawing.Rectangle
對象。 使用此功能不僅可以減少閱讀錯誤,還可以提高閱讀效率。
開始使用 IronBarcode
立即在您的專案中使用IronBarcode,並享受免費試用。
如何定義條碼裁剪區域以加快讀取
- Download the C# library for reading barcodes
- 尋找圖像中的裁剪區域座標及大小
- 從座標建立裁剪區域
- 使用
Read
方法來偵測和讀取條碼 - 將裁剪區域對象傳遞給方法
找到圖像中的裁剪區域座標和大小
有許多方法可以幫助使用者在圖片中找到一個點的坐標。 其中之一是使用電腦中的「畫圖」應用程序來加載圖像。 要獲取裁剪區域的第一個座標,將游標移動到希望的第一個位置,這將是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並讀取其中的條碼。