How to Set a Barcode Crop Region in C#

如何在 C# 中定义条形码裁剪区域以加快读取速度

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

IronBarcode 最重要的功能之一是允许用户指定作物区域。 此功能的目的是使 IronBarcode 能够仅读取图像中由裁剪区域指定的特定条形码或区域,方法是使用Iron Software.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 应用于图像并读取其中的条形码。

常见问题解答

定义裁剪区域如何提高 C# 中的条码读取效果?

使用 IronBarcode 库在 C# 中定义裁剪区域可以专注于图像的特定区域,通过减少不必要的数据处理提高条码读取的准确性和速度。

设置条码读取裁剪区域涉及哪些步骤?

要设置条码读取的裁剪区域,您需要使用图像编辑器确定区域的坐标,使用 Iron Software.Drawing.Rectangle 对象定义区域,并通过 BarcodeReaderOptions 应用于 BarcodeReader.Read() 方法。

如何在图像中确定裁剪区域的坐标?

您可以使用图像编辑工具如 'Paint' 选择所需矩形的左上角和右下角,并记下 x, y 坐标来确定裁剪区域的坐标。

Rectangle 对象在定义裁剪区域中的作用是什么?

Iron Software.Drawing.Rectangle 对象用于指定裁剪区域的坐标和尺寸,这有助于将条码读取过程集中在图像的特定区域。

在 C# 中设置裁剪区域后可以进行修改吗?

是的,您可以通过调整 Rectangle 对象的坐标和尺寸来修改裁剪区域,然后再将其应用于 BarcodeReaderOptions

使用 IronBarcode 进行条码读取时必须使用裁剪区域吗?

使用条码读取时不必使用裁剪区域,但这样做可以显著提高过程的效率和准确性,将读取集中在特定图像区域上。

BarcodeReaderOptions 对象如何利用裁剪区域?

BarcodeReaderOptions 对象通过将裁剪区域设置为 CropArea 属性来利用其功能,然后作为参数传递给 BarcodeReader.Read() 方法,以便将条码读取集中在定义的区域。

在条码检测中使用裁剪区域有什么优势?

在条码检测中使用裁剪区域的优势包括减少读取错误、提高速度以及专注于图像的特定区域,从而提升整体条码读取性能。

Hairil Hasyimi Bin Omar
软件工程师
如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。
准备开始了吗?
Nuget 下载 1,979,979 | Version: 2025.11 刚刚发布