This technical tip shows how to read the image and get all the barcode regions, for all the recogniz

This tip submitted by sherazam on 2013-03-18 23:26:08. It has been viewed 5438 times.
Rating of 6.6 with 34 votes



This technical tip shows how to read the image and get all the barcode regions, for all the recognized barcodes in the image. The barcode region is the part of the image that only contains the barcode itself. In a large image, it is possible that there are other texts or images along with the barcode. Getting the barcode region will separate the barcodes from other text/objects in the image by detecting their edges. First, we will read the BarCodes in the image using the BarCodeReader.Read() method. Then, we will get the region of the barcode using BarCodeReader.GetRegion() method, which will return an instance of type BarCodeRegion. We can then get the X and Y coordinates of the barcode using BarCodeRegion.Points property.

[C#]

// read code39 barcode from image
string image = "code39Extended.jpg";
BarCodeReader reader = new BarCodeReader(image, BarCodeReadType.Code39Standard);
// try to recognize all possible barcodes in the image
while (reader.Read()) {
// get the region information
BarCodeRegion region = reader.GetRegion();
if (region != null)
{
// display x and y coordinates of barcode detected
System.Drawing.Point[] point = region.Points;
Console.WriteLine("Top left coordinates: X = " + point[0].X + ", Y = " + point[0].Y);
Console.WriteLine("Bottom left coordinates: X = " + point[1].X + ", Y = " + point[1].Y);
Console.WriteLine("Bottom right coordinates: X = " + point[2].X + ", Y = " + point[2].Y);
Console.WriteLine("Top right coordinates: X = " + point[3].X + ", Y = " + point[3].Y);
}
Console.WriteLine("Codetext: " + reader.GetCodeText());
}
// close reader
reader.Close();




More tips

Help your fellow programmers! Add a tip!