article

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

Email
Submitted on: 8/19/2015 8:46:09 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 3215
 
     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.

 
				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.
//[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()
Overview: Aspose.BarCode for .NET
Aspose.BarCode is a .NET component for generation and recognition of Linear and 2D barcodes on all kinds of .NET applications. It supports WPF with 29+ Barcode symbologies like OneCode, QR, Aztec, MSI, EAN128, EAN14, SSCC18, Code128, Code39, Postnet, MarcoPDF417, Datamatrix, UPCA etc. Other features include barcode insertion in PDF, Word and Excel documents. Also take image output in BMP, GIF, JPEG, PNG and WMF formats. You can also control image styles such as background color, bar color etc. 


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.