How to Read & Mark Barcode Regions in an Image inside .NET Applications

This Technical tip explains how to mark barcode regions in the Image inside .NET applications. In this section, we will read the image and mark 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 marking their edges or by filling with some color. 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 draw edges around the barcode using BarCodeRegion.DrawBarCodeEdges() method.
//your code here...//[C# Code Sample] // create an instance of BarCodeReader class and specify the image and symbology BarCodeReader reader; reader = new BarCodeReader(@"code39Extended.jpg", BarCodeReadType.Code39Extended); int counter = 0; // read all the barcodes from the images while (reader.Read()) { // display the symbology type Console.WriteLine("BarCode Type: " + reader.GetReadType()); // display the codetext Console.WriteLine("BarCode CodeText: " + reader.GetCodeText()); // get the barcode region Aspose.BarCodeRecognition.BarCodeRegion region = reader.GetRegion(); if (region != null) { // Initialize an object of type Image to get the Graphics object Image img = System.Drawing.Image.FromFile(@"code39Extended.jpg"); // initialize graphics object from the image Graphics g = Graphics.FromImage(img); // draw the barcode edges region.DrawBarCodeEdges(g, new Pen(Color.Red, 1f)); // save the image img.Save(string.Format(@"edge_{0}.png", counter++)); // fill the barcode area with some color region.FillBarCodeRegion(g, Brushes.Green); img.Save(string.Format(@"fill_{0}.png", counter++)); } } reader.Close(); //[VB.NET Code Sample] 'create an instance of BarCodeReader class and specify the image and symbology Dim reader As BarCodeReader reader = New BarCodeReader("c:\test.jpg", BarCodeReadType.Code39Standard) Dim counter As Integer 'read all the barcodes from the images While reader.Read() counter = counter + 1 'display the symbology type Console.WriteLine("BarCode Type: " + reader.GetReadType().ToString) 'display the code text Console.WriteLine("BarCode CodeText: " + reader.GetCodeText()) Dim region As BarCodeRegion 'get the barcode region region = reader.GetRegion() If Not region Is Nothing Then 'Initialize an object of type Image to get the Graphics object Dim img As Image = System.Drawing.Image.FromFile("c:\test.jpg") 'initialize graphics object from the image Dim g As Graphics = Graphics.FromImage(img) 'draw the barcode edges region.DrawBarCodeEdges(g, New Pen(Color.Red, 1.0F)) img.Save(String.Format(".\edge_{0}.png", counter)) 'fill the barcode area with some color region.FillBarCodeRegion(g, Brushes.Green) img.Save(String.Format(".\fill_{0}.png", counter)) End If End While reader.Close()

Url: http://www.aspose.com/.net/barcode-component.aspx

Language: C# | User: Sheraz Khan | Created: Aug 19, 2015 | Tags: Mark BarCode Regions in Image read Barcode image mark all barcode regions draw edges around the barcode .NET barcode API recognize barcodes in the image get the region of the barcode,