Read Barcodes in Specific Order
Contents
[
Hide
]
When an image contains several barcodes, developers may want to read them in a specific order. This code example shows how to implement this feature using Aspose.BarCode for .NET.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void Run() | |
{ | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_ManageBarCodes(); | |
List<FoundBarCodes> found = new List<FoundBarCodes>(); | |
using (BarCodeReader reader = new BarCodeReader(dataDir + "tiffImage.tif", DecodeType.Code128)) | |
{ | |
foreach (BarCodeResult result in reader.ReadBarCodes()) | |
found.Add(new FoundBarCodes(result.CodeText, result.Region)); | |
} | |
found.Sort(new FoundComparator()); | |
int i = 1; | |
foreach (FoundBarCodes barcode in found) | |
{ | |
Console.WriteLine("Codetext ( " + i + " ): " + barcode.CodeText); | |
Console.WriteLine("Top left coordinates: X = " + barcode.region.Points[0].X + ", Y = " + barcode.region.Points[0].Y); | |
Console.WriteLine("Bottom left coordinates: X = " + barcode.region.Points[1].X + ", Y = " + barcode.region.Points[1].Y); | |
Console.WriteLine("Bottom right coordinates: X = " + barcode.region.Points[2].X + ", Y = " + barcode.region.Points[2].Y); | |
Console.WriteLine("Top right coordinates: X = " + barcode.region.Points[3].X + ", Y = " + barcode.region.Points[3].Y); | |
Console.WriteLine(); | |
i++; | |
} | |
} | |
public struct FoundBarCodes | |
{ | |
public readonly string CodeText; | |
public readonly BarCodeRegionParameters region; | |
public FoundBarCodes(string text, BarCodeRegionParameters reg) | |
{ | |
CodeText = text; | |
region = reg; | |
} | |
} | |
public class FoundComparator : IComparer<FoundBarCodes> | |
{ | |
public int Compare(FoundBarCodes found1, FoundBarCodes found2) | |
{ | |
Point[] found1Points = found1.region.Points; | |
Point[] found2Points = found2.region.Points; | |
Point found1XyMin = new Point(int.MaxValue, int.MaxValue); | |
Point found2XyMin = new Point(int.MaxValue, int.MaxValue); | |
foreach (Point p in found1Points) | |
if (p.X < found1XyMin.X && p.Y < found1XyMin.Y) | |
{ | |
found1XyMin.X = p.X; | |
found1XyMin.Y = p.Y; | |
} | |
foreach (Point p in found2Points) | |
if (p.X < found2XyMin.X && p.Y < found2XyMin.Y) | |
{ | |
found2XyMin.X = p.X; | |
found2XyMin.Y = p.Y; | |
} | |
if (found1XyMin.X < found2XyMin.X && found1XyMin.Y < found2XyMin.Y) | |
return -1; | |
if (found1XyMin.X > found2XyMin.X && found1XyMin.Y > found2XyMin.Y) | |
return 1; | |
if (found1XyMin.Y < found2XyMin.Y) | |
return -1; | |
if (found1XyMin.Y > found2XyMin.Y) | |
return 1; | |
return 0; | |
} | |
} |