如何在C#中設定條碼裁剪區域

How to Define Barcode Crop Region for Faster Read

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

IronBarcode中的一個重要功能是用戶可以指定裁切區域。 這個功能的目的是讓IronBarcode透過使用IronSoftware.Drawing.Rectangle對象,只讀取圖像中由裁切區域指定的特定條碼或區域。 使用此功能不僅能減少讀取錯誤,還能提升讀取效能。

快速入門:定義並應用裁切區域以更快地讀取條碼

創建一個裁切矩形並在數秒內將它輸入到IronBarcode中—無需額外配置,無阻礙。 看看如何輕鬆限制掃描至特定圖像區域,使用BarcodeReaderOptions

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { CropArea = new IronSoftware.Drawing.Rectangle(x: 50, y: 100, width: 300, height: 150) });
  3. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小化工作流程(5步)

  1. 下載C#條碼讀取庫
  2. 在圖像中找到裁切區域的坐標和大小
  3. 從座標創建裁切區域
  4. 使用Read方法檢測並讀取條碼
  5. 將裁切區域對象傳入方法中

在圖像中找到裁切區域的坐標和大小

用戶可以通過多種方法找到圖像中某點的坐標。 一種方法是使用電腦上的'畫圖'應用程序加載圖像。 要獲取裁切區域的第一個坐標,將游標移動到首選的第一個點,這將是Rectangle的左上角,並於屏幕左下方查看應用程序提供的x、y坐標。 接著,確定第二個點,這將是Rectangle的右下角。 請參考下圖以獲取更清晰的理解。

class="content-img-align-center">
class="center-image-wrapper"> 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讀取的裁切區域,我們可以將該對象應用於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()方法中的參數,將裁切區域應用於圖像並讀取其中的條碼。

常見問題解答

定義裁剪區域如何改善 C# 中的條碼讀取?

使用 IronBarcode 程式庫在 C# 中定義裁剪區域,允許您專注於影像的特定區域,透過減少不必要的資料處理來提高條碼讀取的準確性和速度。

設定條碼讀取的裁剪區域需要哪些步驟?

要設定條碼讀取的裁剪區域,您需要使用影像編輯器確定區域的座標,並使用 IronSoftware.Drawing.Rectangle 物件定義該區域,然後透過 BarcodeReaderOptionsBarcodeReader.Read() 方法中應用它。

如何確定影像中裁剪區域的座標?

您可以利用像 'Paint' 這樣的影像編輯工具選擇所需矩形的左上角和右下角,並記錄 x, y 座標來確定裁剪區域的座標。

Rectangle 物件在定義裁剪區域中的作用是什麼?

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

C# 中設置完裁剪區域後是否可以修改?

可以,您可以在將它應用於 BarcodeReaderOptions 之前調整 Rectangle 物件的座標和尺寸來修改裁剪區域。

使用 IronBarcode 進行條碼讀取是否必須使用裁剪區域?

進行條碼讀取不一定必須使用裁剪區域,但這樣做可以通過集中於特定影像區域來顯著提高過程的效率和準確性。

BarcodeReaderOptions 物件如何利用裁剪區域?

BarcodeReaderOptions 物件藉由將裁剪區域設置為 CropArea 屬性來利用裁剪區域,然後該屬性作為參數傳遞給 BarcodeReader.Read() 方法以將條碼讀取聚焦在定義區域。

在條碼檢測中使用裁剪區域有哪些優勢?

在條碼檢測中使用裁剪區域提供了減少讀取錯誤、提升速度、專注於影像特定區域等優勢,從而提升整體條碼讀取效能。

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