←Select platform

ReadBarcode(RasterImage,LeadRect,BarcodeSymbology[],BarcodeReadOptions[]) Method

Summary
Read one barcode from an image with a symbology from a specified group and options.
Syntax
C#
Objective-C
C++/CLI
Java
Python
- (nullable LTBarcodeData *)readBarcode:(LTRasterImage *)image searchBounds:(LeadRect)searchBounds symbologies:(nullable NSArray<NSNumber *> *)symbologies options:(nullable NSArray<LTBarcodeReadOptions *> *)options error:(NSError **)error NS_SWIFT_NOTHROW; 
public BarcodeData readBarcode( 
  RasterImage image,  
  LeadRect searchBounds,  
  BarcodeSymbology[] symbologies,  
  BarcodeReadOptions[] options 
) 
def ReadBarcode(self,image,searchBounds,] symbologies,] options): 

Parameters

image
A RasterImage object that contains the image data. Must not be null (Nothing in VB).

searchBounds
A LeadRect that specifies the region of interest area in the image where the barcode search and detection is performed. You can specify LeadRect.Empty to indicate that the search must be performed on the whole image.

symbologies
An array of BarcodeSymbology enumeration members that specifies the barcode symbologies (types) to search for.

options
An array of BarcodeReadOptions that specifies the options to use. This can be null (Nothing in VB), where the current default options is used. Otherwise, the method will use the option that corresponds to the symbologies being read, if the array does not contain specific options for a symbologies being read, then the default version will be used.

Return Value

An instance of BarcodeData or one of its derived classes that contains the symbology, data, location and any rotation angle of the barcode found. If no barcodes can be found, then this method will return null (Nothing in VB).

Remarks

For more information on barcode reading, refer to BarcodeReader.

Use this method if you want to read a single barcode from a known group. For example, to read a barcode that can be of any UPC type, pass an array of BarcodeSymbology.UPCA and BarcodeSymbology.UPCE.

LEADTOOLS barcode read engine is optimized for speed and can search for multiple similar symbologies at the same time. This method simply returns the first barcode that is detected correctly using the symbologies and current options.

The ReadSymbology event will occur before and after attempting to read any symbology. The read options being used whether the default or specified will be set in the BarcodeReadSymbologyEventArgs.Options property of the event data.

If  symbologies is null (Nothing in VB), then this method will use all the currently available symbologies. If this parameter contains an empty array, then no barcode will be detected and null (Nothing in VB) will be returned.

The BarcodeReader provides the following barcode read methods:

Method Description
ReadBarcode(RasterImage image, LeadRect searchBounds, BarcodeSymbology symbology) and ReadBarcode(RasterImage image, LeadRect searchBounds, BarcodeSymbology symbology, BarcodeReadOptions options)

Read one barcode from an image with specified symbology and default or specific options. Use these methods if you want to read a single barcode from the image, for example, a QR symbol by specifying BarcodeSymbology.QR or if you want to read any barcode found regardless of its type by using BarcodeSymbology.Unknown.

ReadBarcode(RasterImage image, LeadRect searchBounds, BarcodeSymbology[] symbologies) and ReadBarcode(RasterImage image, LeadRect searchBounds, BarcodeSymbology[] symbologies, BarcodeReadOptions[] options)

Read one barcode from an image with a symbology from a specified group and default or specific options. Use these methods if you want to read a single barcode from a known group. For example, to read a barcode that can be of any UPC type, pass an array of BarcodeSymbology.UPCA and BarcodeSymbology.UPCE.

ReadBarcodes(RasterImage image, LeadRect searchBounds, int maximumBarcodes, BarcodeSymbology[] symbologies) and ReadBarcodes(RasterImage image, LeadRect searchBounds, int maximumBarcodes, BarcodeSymbology[] symbologies, BarcodeReadOptions[] options)

Read multiple barcodes from an image with symbologies from a specified group and default or specific options. Use these methods if you want to read multiple barcodes of the same or multiple symbologies.

Example

This example shows how to use this method to read any UPC barcode used to identify products from a rotated image.

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Barcode; 
using Leadtools.ImageProcessing; 
 
 
public void BarcodeReader_ReadBarcodeExample4() 
{ 
   string imageFileName = Path.Combine(LEAD_VARS.ImagesDir, "Barcode1.tif"); 
 
   // Create a Barcode engine 
   BarcodeEngine engine = new BarcodeEngine(); 
 
   // Get the Barcode reader instance 
   BarcodeReader reader = engine.Reader; 
 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      using (RasterImage image = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)) 
      { 
         // Rotate the image by 90, so default option of reading horizonal barcodes will not work 
         Console.WriteLine("Rotating the image by 90 degrees"); 
         RotateCommand rotate = new RotateCommand(90 * 100, RotateCommandFlags.Resize, RasterColor.FromKnownColor(RasterKnownColor.White)); 
         rotate.Run(image); 
 
         // In the US, UPC barcodes are used to identity products. So, create an array of UPC symbologies 
         BarcodeSymbology[] upcSymbologies = 
         { 
        BarcodeSymbology.UPCA, 
        BarcodeSymbology.UPCE 
      }; 
 
         // Read the first UPC barcode from the image using default options 
         Console.WriteLine("Reading barcodes using default options"); 
         BarcodeData barcode = reader.ReadBarcode(image, LeadRect.Empty, upcSymbologies, null); 
 
         // Show its location and data if found 
         // This will print out "Not found" 
         if (barcode != null) 
         { 
            Console.WriteLine("Found a {0} barcode at {1}, data:\n{2}", barcode.Symbology, barcode.Bounds, barcode.Value); 
         } 
         else 
         { 
            Console.WriteLine("Not found"); 
         } 
 
         // Now get the options we used previously, and change the 1D linear barcode read options 
         // to search for barcodes in both horizontal and vertical directions 
         // Note: Same options are used for UPCA and UPCE, so we only need one options class 
         OneDBarcodeReadOptions oneDReadOptions = reader.GetDefaultOptions(BarcodeSymbology.UPCA) as OneDBarcodeReadOptions; 
 
         // Change the read direction 
         oneDReadOptions.SearchDirection = BarcodeSearchDirection.HorizontalAndVertical; 
 
         // Try again using our options 
         Console.WriteLine("Reading barcodes using our options"); 
         barcode = reader.ReadBarcode(image, LeadRect.Empty, upcSymbologies, new BarcodeReadOptions[] { oneDReadOptions }); 
 
         // Show its location and data if found 
         // This will find the barcode and print its information now 
         if (barcode != null) 
         { 
            Console.WriteLine("Found a {0} barcode at {1}, data:\n{2}", barcode.Symbology, barcode.Bounds, barcode.Value); 
         } 
         else 
         { 
            Console.WriteLine("Not found"); 
         } 
      } 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.File; 
import java.io.IOException; 
import java.util.*; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
 
import org.junit.*; 
import org.junit.Test; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.barcode.*; 
import leadtools.codecs.*; 
import leadtools.imageprocessing.*; 
import leadtools.internal.ManualResetEvent; 
 
 
public void barcodeReaderReadBarcodeExample4() { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String imageFileName = combine(LEAD_VARS_IMAGES_DIR, "barcode1.tif"); 
 
   // Create a Barcode engine 
   BarcodeEngine engine = new BarcodeEngine(); 
 
   // Get the Barcode reader instance 
   BarcodeReader reader = engine.getReader(); 
   RasterCodecs codecs = new RasterCodecs(); 
   RasterImage image = codecs.load(imageFileName, 0, CodecsLoadByteOrder.BGR_OR_GRAY, 1, 1); 
 
   // Rotate the image by 90, so default option of reading horizonal barcodes will 
   // not work 
   System.out.println("Rotating the image by 90 degrees"); 
   RotateCommand rotate = new RotateCommand(90 * 100, RotateCommandFlags.RESIZE.getValue(), 
         RasterColor.fromKnownColor(RasterKnownColor.WHITE)); 
   rotate.run(image); 
 
   // In the US, UPC barcodes are used to identity products. So, create an array of 
   // UPC symbologies 
   BarcodeSymbology[] upcSymbologies = { BarcodeSymbology.UPC_A, BarcodeSymbology.UPC_E }; 
 
   // Read the first UPC barcode from the image using default options 
   System.out.println("Reading barcodes using default options"); 
   BarcodeData barcode = reader.readBarcode(image, LeadRect.getEmpty(), upcSymbologies, null); 
 
   // Show its location and data if found 
   // This will print out "Not found" 
   if (barcode != null) { 
      System.out.printf("Found a %1s barcode at %2s, data:%n%3s%n", barcode.getSymbology(), barcode.getBounds(), 
            barcode.getValue()); 
   } else { 
      System.out.println("Not found"); 
   } 
 
   // Now get the options we used previously, and change the 1D linear barcode read 
   // options 
   // to search for barcodes in both horizontal and vertical directions 
   // Note: Same options are used for UPCA and UPCE, so we only need one options 
   // class 
   OneDBarcodeReadOptions oneDReadOptions = (OneDBarcodeReadOptions) reader 
         .getDefaultOptions(BarcodeSymbology.UPC_A); 
 
   // Change the read direction 
   oneDReadOptions.setSearchDirection(BarcodeSearchDirection.HORIZONTAL_AND_VERTICAL); 
 
   // Try again using our options 
   System.out.println("Reading barcodes using our options"); 
   barcode = reader.readBarcode(image, LeadRect.getEmpty(), upcSymbologies, 
         new BarcodeReadOptions[] { oneDReadOptions }); 
 
   // Show its location and data if found 
   // This will find the barcode and print its information now 
   if (barcode != null) { 
      System.out.printf("Found a %1s barcode at %2s, data:%n%3s%n", barcode.getSymbology(), barcode.getBounds(), 
            barcode.getValue()); 
   } else { 
      System.out.println("Not found"); 
   } 
 
   String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "Result.jpg"); 
   codecs.save(image, outputFileName, RasterImageFormat.JPEG, 0); 
 
   assertTrue("Image unsuccessfully saved to " + outputFileName, (new File(outputFileName)).exists()); 
   System.out.printf("Image successfully saved to %s%n", outputFileName); 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Barcode Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.