How to Define Barcode Crop Region for Faster Read
One of the most important feature in IronBarcode is the ability for the users to specify Crop Regions. The aim of this feature is to enable IronBarcode to only read specific barcodes or area specified by the Crop Region in the image by using IronSoftware.Drawing.Rectangle
object. Using this feature will not only reduce error in reading, but also improve the reading performance.
Get started with IronBarcode
Start using IronBarcode in your project today with a free trial.
How to Define Barcode Crop Region for Faster Read
- Download the C# library for reading barcodes
- Find Crop Region coordinates and size in an image
- Create the crop region from the coordinates
- Use the
Read
method to detect and read barcode - Pass the crop region object to the method
Find Crop Region coordinates and size in an image
There are many ways available for users to find the coordinates of a point in an image. One of it is to load the image using 'Paint' application in the computer. To get the first coordinate of the Crop Region, move the cursor to prefered first spot which will be the top left corner of the Rectangle
and take the x,y coordinate given by the app at the bottom left of the screen. Then, locate the second point which will be the bottom right corner of the Rectangle
. Refer to the image below for clearer understanding.
Setting CropRegion reference
The coordinate values can then be used as properties for the Rectangle
object. The width of the object can be defined as x2 - x1 while height can be defined as 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)
Apply CropRegion and Read Barcode
Once we have done the hard work of defining the CropRegions where we want IronBarcode to read, we can then apply the object into BarcodeReaderOptions
as one of the properties besides other settings as well, which can then be used as a parameter in the BarcodeReader.Read()
method. The code snippet below shows
: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
From the code snippet above, we used the instantiated Rectangle
in BarcodeReaderOptions
object as the CropArea
property. We then use this BarcodeReaderOptions
object as a parameter in the BarcodeReader.Read()
method to apply the CropArea in the image and read the barcodes inside.