如何在 C# 中設定 BarCode Crop Region

如何在 C# 中定義條碼裁切區域以加快讀取速度

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronBarcode 最重要的功能之一是允許使用者指定作物區域。 此功能的目的是使 IronBarcode 能夠僅讀取影像中由裁切區域指定的特定條碼或區域,方法是使用IronSoftware.Drawing.Rectangle物件。 使用此功能不僅可以減少閱讀錯誤,還可以提高閱讀效率。

快速入門:定義和應用作物區域以更快地讀取條碼

只需幾秒鐘即可建立裁剪矩形並將其匯入 IronBarcode—無需額外設置,操作便捷。 了解使用BarcodeReaderOptions將掃描範圍限制在特定影像區域是多麼容易。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronBarcode

    PM > Install-Package BarCode

  2. 複製並運行這段程式碼。

    var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { CropArea = new IronSoftware.Drawing.Rectangle(x: 50, y: 100, width: 300, height: 150) });
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronBarcode,免費試用!
    arrow pointer

在圖像中尋找作物區域的座標和大小

使用者可以透過多種方式找到圖像中某一點的座標。 一種方法是使用電腦上的"畫圖"應用程式載入圖片。 若要取得裁切區域的第一個座標,請將遊標移到首選的第一個位置,即Rectangle的左上角,然後從螢幕左下角的應用程式取得 x、y 座標。 然後,找出第二個點,即Rectangle的右下角。 請參考下圖以便更清楚地理解。

CropRegion reference

然後可以將座標值用作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);
Imports IronBarCode

Private x1 As Integer = 62
Private y1 As Integer = 29
Private x2 As Integer = 345
Private y2 As Integer = 522

Private crop1 As New IronSoftware.Drawing.Rectangle(x:= x1, y:= y1, width:= x2-x1, height:= y2-y1)
$vbLabelText   $csharpLabel

應用作物區域並讀取條碼

一旦我們定義了 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);
}
Imports IronBarCode
Imports System

Private x1 As Integer = 62
Private y1 As Integer = 29
Private x2 As Integer = 345
Private y2 As Integer = 522

Private crop1 As New IronSoftware.Drawing.Rectangle(x:= x1, y:= y1, width:= x2 - x1, height:= y2 - y1)

Private options As New BarcodeReaderOptions() With {.CropArea = crop1}

Private result = BarcodeReader.Read("sample.png", options)
For Each item In result
	Console.WriteLine(item.Value)
Next item
$vbLabelText   $csharpLabel

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

常見問題解答

定義 Crop Region 如何改善 C# 中的 BarCode 讀取?

使用 IronBarcode 函式庫在 C# 中定義 Crop Region,可讓您專注於影像的特定區域,藉由減少不必要的資料處理,提升條碼讀取的精確度與速度。

設定條碼讀取的 Crop Region 涉及哪些步驟?

要為條碼讀取設定裁剪區域,您需要使用影像編輯器來確定區域的座標,使用 IronSoftware.Drawing.Rectangle 物件來定義區域,並透過 BarcodeReader.Read() 方法中的 BarcodeReaderOptions 來套用。

如何確定影像中作物區域的座標?

您可以使用「繪圖」等影像編輯工具,選取所需矩形的左上角和右下角,並記下 x、y 座標,以確定裁剪區域的座標。

在定義 Crop Region 時,Rectangle 物件的作用是什麼?

IronSoftware.Drawing.Rectangle 物件用於指定裁剪區域的座標和尺寸,這有助於將條碼讀取過程集中在影像的特定區域上。

C# 中設定了 Crop Region 之後,還可以修改嗎?

是的,您可以在套用 BarcodeReaderOptions 之前,透過調整 Rectangle 物件的座標和尺寸來修改 Crop Region。

使用 IronBarcode 讀取條碼是否必須使用 Crop Region?

並非必須使用裁切區域來讀取 BarCode,但這樣做可以將讀取集中在特定的影像區域,大幅提升處理的效率和精確度。

BarcodeReaderOptions 物件如何利用 Crop Region?

BarcodeReaderOptions 物件透過將其設定為 CropArea 屬性來利用 Crop Region,然後將其作為參數傳送給 BarcodeReader.Read() 方法,以將條碼讀取集中在所定義的區域上。

在 BarCode 檢測中使用 Crop Region 有哪些優點?

在條碼偵測中使用裁切區域具有減少讀取錯誤、提高速度、聚焦於影像的特定區域等優點,可提升整體條碼讀取效能。

Hairil Hasyimi Bin Omar
軟體工程師
和所有优秀的工程师一样,Hairil 是个努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识应用于 Iron Software 各个团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。
準備好開始了嗎?
Nuget 下載 1,979,979 | Version: 2025.11 剛發表