VintaSoft Barcode .NET SDK v10.0
ReadBarcodes(BitmapSource) Method
Example 




Vintasoft.WpfBarcode Namespace > BarcodeReader Class > ReadBarcodes Method : ReadBarcodes(BitmapSource) Method
Image with barcodes.
Reads barcodes from the specified existing image.
Syntax
'Declaration
 
Public Overloads Function ReadBarcodes( _
   ByVal image As BitmapSource _
) As IBarcodeInfo()
'Usage
 
Dim instance As BarcodeReader
Dim image As BitmapSource
Dim value() As IBarcodeInfo
 
value = instance.ReadBarcodes(image)
public IBarcodeInfo[] ReadBarcodes( 
   BitmapSource image
)
public: IBarcodeInfo*[]* ReadBarcodes( 
   BitmapSource* image
) 
public:
array<IBarcodeInfo^>^ ReadBarcodes( 
   BitmapSource^ image
) 

Parameters

image
Image with barcodes.

Return Value

An array of IBarcodeInfo objects which contains information about recognized barcodes.
Example
Here is a simple example that demonstrates how to detect barcodes in an image.
Imports System.IO   
Imports System.Windows.Media.Imaging   
   
Imports Vintasoft.WpfBarcode   
Imports Vintasoft.WpfBarcode.SymbologySubsets   
   
''' <summary>   
''' Test that shows how read barcodes from image using the BarcodeReader class.   
''' </summary>   
Class ReadBarcodesFromImageExample   
   
	''' <summary>   
	''' Runs the test.   
	''' </summary>   
	Public Shared Sub Test()   
		' load image from file   
		Using imageFileStream As Stream = New FileStream("test1.jpg", FileMode.Open, FileAccess.Read)   
			Dim barcodeImage As New BitmapImage()   
			barcodeImage.BeginInit()   
			barcodeImage.StreamSource = imageFileStream   
			barcodeImage.EndInit()   
			' read barcodes from image   
			ReadBarcodes(barcodeImage)   
		End Using   
	End Sub   
   
	''' <summary>   
	''' Reads barcodes from image.   
	''' </summary>   
	Private Shared Sub ReadBarcodes(barcodeImage As BitmapSource)   
		' create barcode reader   
		Using reader As New BarcodeReader()   
			' specify that reader must search for Code39, Code39Extended,    
			' Code128, SSCC18 and DataMatrix barcodes    
			reader.Settings.ScanBarcodeTypes = BarcodeType.Code39 Or BarcodeType.Code128 Or BarcodeType.DataMatrix   
			reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.Code39Extended)   
			reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.SSCC18)   
   
			' specify that reader must search for horizontal and vertical barcodes only   
			reader.Settings.ScanDirection = ScanDirection.LeftToRight Or ScanDirection.RightToLeft   
   
			' use Automatic Recognition   
			reader.Settings.AutomaticRecognition = True   
   
			' read barcodes from image   
			Dim infos As IBarcodeInfo() = reader.ReadBarcodes(barcodeImage)   
   
			' show barcode recognition results   
   
			Console.WriteLine(String.Format("Recognition time {0} ms.", reader.RecognizeTime.TotalMilliseconds))   
   
			If infos.Length = 0 Then   
				Console.WriteLine("No barcodes found.")   
			Else   
				Console.WriteLine(String.Format("{0} barcodes found:", infos.Length))   
				Console.WriteLine()   
				For i As Integer = 0 To infos.Length - 1   
					Dim info As IBarcodeInfo = infos(i)   
					Console.WriteLine(String.Format("[{0}:{1}]", i, info.BarcodeType))   
					Console.WriteLine(String.Format("Value:      {0}", info.Value))   
					Console.WriteLine(String.Format("Confidence: {0}%", Math.Round(info.Confidence)))   
					Console.WriteLine(String.Format("Threshold:  {0}", info.Threshold))   
					Console.WriteLine(String.Format("Region:     {0}", info.Region))   
					Console.WriteLine()   
				Next   
			End If   
		End Using   
	End Sub   
End Class
using System;
using System.IO;
using System.Windows.Media.Imaging;
            
using Vintasoft.WpfBarcode;
using Vintasoft.WpfBarcode.SymbologySubsets;
            
/// <summary>
/// Test that shows how read barcodes from image using the BarcodeReader class.
/// </summary>
class ReadBarcodesFromImageExample
{
            
    /// <summary>
    /// Runs the test.
    /// </summary>
    public static void Test()
    {
        // load image from file
        using (Stream imageFileStream = new FileStream("test1.jpg", FileMode.Open, FileAccess.Read))
        {
            BitmapImage barcodeImage = new BitmapImage();
            barcodeImage.BeginInit();
            barcodeImage.StreamSource = imageFileStream;
            barcodeImage.EndInit();
            // read barcodes from image
            ReadBarcodes(barcodeImage);
        }
    }
            
    /// <summary>
    /// Reads barcodes from image.
    /// </summary>
    static void ReadBarcodes(BitmapSource barcodeImage)
    {
        // create barcode reader
        using (BarcodeReader reader = new BarcodeReader())
        {
            // specify that reader must search for Code39, Code39Extended, 
            // Code128, SSCC18 and DataMatrix barcodes 
            reader.Settings.ScanBarcodeTypes =
                BarcodeType.Code39 |
                BarcodeType.Code128 |
                BarcodeType.DataMatrix;
            reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.Code39Extended);
            reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.SSCC18);
            
            // specify that reader must search for horizontal and vertical barcodes only
            reader.Settings.ScanDirection = ScanDirection.LeftToRight | ScanDirection.RightToLeft;
            
            // use Automatic Recognition
            reader.Settings.AutomaticRecognition = true;
            
            // read barcodes from image
            IBarcodeInfo[] infos = reader.ReadBarcodes(barcodeImage);
            
            // show barcode recognition results
            
            Console.WriteLine(string.Format("Recognition time {0} ms.",
                reader.RecognizeTime.TotalMilliseconds));
            
            if (infos.Length == 0)
            {
                Console.WriteLine("No barcodes found.");
            }
            else
            {
                Console.WriteLine(string.Format("{0} barcodes found:", infos.Length));
                Console.WriteLine();
                for (int i = 0; i < infos.Length; i++)
                {
                    IBarcodeInfo info = infos[i];
                    Console.WriteLine(string.Format("[{0}:{1}]", i, info.BarcodeType));
                    Console.WriteLine(string.Format("Value:      {0}", info.Value));
                    Console.WriteLine(string.Format("Confidence: {0}%", Math.Round(info.Confidence)));
                    Console.WriteLine(string.Format("Threshold:  {0}", info.Threshold));
                    Console.WriteLine(string.Format("Region:     {0}", info.Region));
                    Console.WriteLine();
                }
            }
        }
    }
}
Requirements

Target Platforms: Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

See Also

Reference

BarcodeReader Class
BarcodeReader Members
Overload List