←Select platform

ReadBarcode(RasterImage,LeadRect,BarcodeSymbology,BarcodeReadOptions) Method

Summary
Read one barcode from an image with specified symbology and options.
Syntax
C#
Objective-C
C++/CLI
Java
Python
- (nullable LTBarcodeData *)readBarcode:(LTRasterImage *)image searchBounds:(LeadRect)searchBounds symbology:(LTBarcodeSymbology)symbology options:(nullable LTBarcodeReadOptions *)options error:(NSError **)error NS_SWIFT_NOTHROW; 
public BarcodeData readBarcode( 
  RasterImage image,  
  LeadRect searchBounds,  
  BarcodeSymbology symbology,  
  BarcodeReadOptions options 
) 
def ReadBarcode(self,image,searchBounds,symbology,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.

symbology
An BarcodeSymbology enumeration member that specifies the barcode symbology (type) to search for. You can pass BarcodeSymbology.Unknown to search for all available symbologies in this BarcodeReader.

options
An instance of a BarcodeReadOptions derived class that specifies the options to use.

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

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.

For more information on barcode reading, refer to BarcodeReader.

This method will use the default read options set in this BarcodeReader that correspond to  symbology. If the value of this parameter is BarcodeSymbology.Unknown, then all the default read options might be used.

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 symbology and current options.

This method will use the options in  options when reading the barcode. If this parameter is null (Nothing in VB), then the default options for  symbology (or all default options if BarcodeSymbology.Unknown is specified) will be used instead.

If  options is not null (Nothing in VB) and  symbology is not BarcodeSymbology.Unknown, then it must contain a derived class of BarcodeReadOptions that supports  symbology.

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 symbology 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.

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 a single barcode from an image an inverted image with specific options.

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Barcode; 
using Leadtools.ImageProcessing; 
 
 
public void BarcodeReader_ReadBarcodeExample2() 
{ 
   string imageFileName = Path.Combine(LEAD_VARS.ImagesDir, "Barcode2.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)) 
      { 
         // Invert the image 
         Console.WriteLine("Inverting the image"); 
         Leadtools.ImageProcessing.Color.InvertCommand invert = new Leadtools.ImageProcessing.Color.InvertCommand(); 
         invert.Run(image); 
 
         // All barcodes have default options of black foreground color and white background color, so 
         // reading the barcode with default options should not return any barcodes right now 
 
         // Read the QR barcode from this image using default options 
         Console.WriteLine("Reading using default options"); 
         BarcodeData barcode = reader.ReadBarcode(image, LeadRect.Empty, BarcodeSymbology.QR, null); 
 
         // Show its location and data if found 
         // This will print out "Not found" 
         if (barcode != null) 
         { 
            Console.WriteLine("Found at {0}, data:\n{1}", barcode.Bounds, barcode.Value); 
         } 
         else 
         { 
            Console.WriteLine("Not found"); 
         } 
 
         // Now create QR read options to have white foreground color and black background color 
         QRBarcodeReadOptions qrReadOptions = new QRBarcodeReadOptions(); 
         qrReadOptions.ForeColor = RasterColor.FromKnownColor(RasterKnownColor.White); 
         qrReadOptions.BackColor = RasterColor.FromKnownColor(RasterKnownColor.Black); 
 
         // And use it to try to read the QR barcode again 
         Console.WriteLine("Reading using specific options that instruct the engine to look for white on black barcodes"); 
         barcode = reader.ReadBarcode(image, LeadRect.Empty, BarcodeSymbology.QR, qrReadOptions); 
 
         // Show its location and data if found 
         // This will find the barcode and print its information now 
         if (barcode != null) 
         { 
            Console.WriteLine("Found at {0}, data:\n{1}", 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 barcodeReaderReadBarcodeExample2() { 
 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String imageFileName = combine(LEAD_VARS_IMAGES_DIR, "Barcode2.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); 
 
   // Invert the image 
   System.out.println("Inverting the image"); 
   leadtools.imageprocessing.color.InvertCommand invert = new leadtools.imageprocessing.color.InvertCommand(); 
   invert.run(image); 
 
   // All barcodes have default options of black foreground color and white 
   // background color, so 
   // reading the barcode with default options should not return any barcodes right 
   // now 
   // Read the QR barcode from this image using default options 
   System.out.println("Reading using default options"); 
   BarcodeData barcode = reader.readBarcode(image, LeadRect.getEmpty(), BarcodeSymbology.QR, null); 
 
   // Show its location and data if found 
   // This will print out "Not found" 
   if (barcode != null) { 
      System.out.printf("Found at %1s, data:%n%2s%n", barcode.getBounds(), barcode.getValue()); 
   } else { 
      System.out.println("Not found"); 
   } 
 
   // Now create QR read options to have white foreground color and black 
   // background color 
   QRBarcodeReadOptions qrReadOptions = new QRBarcodeReadOptions(); 
   qrReadOptions.setForeColor(RasterColor.fromKnownColor(RasterKnownColor.WHITE)); 
   qrReadOptions.setBackColor(RasterColor.fromKnownColor(RasterKnownColor.BLACK)); 
 
   // And use it to try to read the QR barcode again 
   System.out.println("Reading using specific options that instruct the engine to look for white on black barcodes"); 
   barcode = reader.readBarcode(image, LeadRect.getEmpty(), BarcodeSymbology.QR, qrReadOptions); 
 
   // Show its location and data if found 
   // This will find the barcode and print its information now 
   if (barcode != null) { 
      System.out.printf("Found at %1s, data:%n%2s%n", 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.