Read multiple barcodes from an image with symbologies from a specified group and options.
public [Leadtools.Barcode.BarcodeData[]](barcodedata.html) ReadBarcodes(Leadtools.RasterImage image,Leadtools.Forms.LogicalRectangle searchBounds,int maximumBarcodes,Leadtools.Barcode.BarcodeSymbology[] symbologies,Leadtools.Barcode.BarcodeReadOptions[] options)
Public Overloads Function ReadBarcodes( _ByVal image As Leadtools.RasterImage, _ByVal searchBounds As Leadtools.Forms.LogicalRectangle, _ByVal maximumBarcodes As Integer, _ByVal symbologies() As Leadtools.Barcode.BarcodeSymbology, _ByVal options() As Leadtools.Barcode.BarcodeReadOptions _) As Leadtools.Barcode.BarcodeData()
public [Leadtools.Barcode.BarcodeData[]](barcodedata.html) ReadBarcodes(Leadtools.RasterImage image,Leadtools.Forms.LogicalRectangle searchBounds,int maximumBarcodes,Leadtools.Barcode.BarcodeSymbology[] symbologies,Leadtools.Barcode.BarcodeReadOptions[] options)
- (nullable NSArray<LTBarcodeData *> *)readBarcodes:(LTRasterImage *)imagesearchBounds:(LeadRect)searchBoundsmaximumBarcodes:(NSUInteger)maximumBarcodessymbologies:(nullable NSArray<NSNumber *> *)symbologiesoptions:(nullable NSArray<LTBarcodeReadOptions *> *)optionserror:(NSError **)error
public BarcodeData[] readBarcodes(RasterImage image,LeadRect searchBounds,int maximumBarcodes,BarcodeSymbology[] symbologies,BarcodeReadOptions[] options)
function Leadtools.Barcode.BarcodeReader.ReadBarcodes(RasterImage,LogicalRectangle,Int32,BarcodeSymbology[],BarcodeReadOptions[])(image ,searchBounds ,maximumBarcodes ,symbologies ,options)
public:Leadtools.Barcode.array<BarcodeData^>^ ReadBarcodes(Leadtools.RasterImage^ image,Leadtools.Forms.LogicalRectangle searchBounds,int maximumBarcodes,Leadtools.Barcode.array<BarcodeSymbology>^ symbologies,Leadtools.Barcode.array<BarcodeReadOptions^>^ options)
image
A RasterImage object that contains the image data. Must not be null (Nothing in VB).
searchBounds
A LogicalRectangle that specifies the region of interest area in the image where the barcodes search and detection is performed. You can specify LogicalRectangle.Empty to indicate that the search must be performed on the whole image.
maximumBarcodes
An Int32 that specifies the maximum number of barcodes to return. Must be a value greater than or equal to 0. The value of 0 means all barcodes.
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 options 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.
An array of BarcodeData objects or derived classes that contains the symbology, data, location and any rotation angle for each barcode found. If no barcodes can be found, then this method will return an empty array (Length equals to 0).
Use this method if you want to read multiple barcodes of the same or multiple symbologies from an image. For example, to read all UPC and QR barcodes in the image, use an array of BarcodeSymbology.UPC-A, BarcodeSymbology.UPC-E and BarcodeSymbology.QR.
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 an empty array will be returned.
The BarcodeReader provides the following barcode read methods:
| Method | Description |
|---|---|
| ReadBarcode(RasterImage image, LogicalRectangle searchBounds, BarcodeSymbology symbology) and ReadBarcode(RasterImage image, LogicalRectangle 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, LogicalRectangle searchBounds, BarcodeSymbology[] symbologies) and ReadBarcode(RasterImage image, LogicalRectangle 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, LogicalRectangle searchBounds, int maximumBarcodes, BarcodeSymbology[] symbologies) and ReadBarcodes(RasterImage image, LogicalRectangle 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. |
This example creates two threads: One for reading horizontal barcodes and one for reading vertical barcodes. Then it uses the same BarcodeReader to try and read all the barcodes from an image using both threads
using Leadtools;using Leadtools.Codecs;using Leadtools.Forms;using Leadtools.Barcode;using Leadtools.ImageProcessing;// This class holds the data we will pass to each threadpublic class MyThreadData{// This worker thread namepublic string Name;// Barcode reader instancepublic BarcodeReader BarcodeReaderInstance;// Array of options to usepublic BarcodeReadOptions[] ReadOptions;// Image file containing the barcodespublic string ImageFileName;// This will hold the barcodes foundpublic BarcodeData[] Barcodes;// In case of errorspublic Exception Error;// Event to notify when the thread is finishedpublic ManualResetEvent FinishedEvent;}public void BarcodeReader_ReadBarcodeExample6(){string[] imageFileNames = PrepareImages();// Create a Barcode engineBarcodeEngine engine = new BarcodeEngine();// Get the Barcode reader instanceBarcodeReader reader = engine.Reader;// Get a version of all read options that support rotated barcodesBarcodeReadOptions[] verticalBarcodeReadOptions = GetVerticalReadBarcodeOptions(reader);// Synchronization eventsManualResetEvent[] finishedEvents = new ManualResetEvent[2];// Create the thread data objectsMyThreadData[] threadsData = new MyThreadData[2];for (int i = 0; i < threadsData.Length; i++){finishedEvents[i] = new ManualResetEvent(false);threadsData[i] = new MyThreadData();threadsData[i].BarcodeReaderInstance = reader;threadsData[i].ReadOptions = null;threadsData[i].FinishedEvent = finishedEvents[i];}// Setup the read options for the two threadsthreadsData[0].Name = "Read Default Options Thread";threadsData[0].ReadOptions = null; // Default, or horizontalthreadsData[1].Name = "Read Vertical Barcodes Thread";threadsData[1].ReadOptions = verticalBarcodeReadOptions;List<BarcodeData> totalBarcodes = new List<BarcodeData>();// Now loop through all the images and try to read the barcodes with both threads at the same timeforeach (string imageFileName in imageFileNames){Console.WriteLine("Reading barcodes from {0}", imageFileName);for (int i = 0; i < 2; i++){// Clear the previous results (if any), set up the file name and runthreadsData[i].Barcodes = null;threadsData[i].Error = null;threadsData[i].ImageFileName = imageFileName;ThreadPool.QueueUserWorkItem(new WaitCallback(ReadBarcodesInThread), threadsData[i]);}// Wait till all the threads finishesforeach (ManualResetEvent waitHandle in finishedEvents)waitHandle.WaitOne();// Add the barcodes found to our listfor (int i = 0; i < 2; i++){if (threadsData[i].Error != null){Console.WriteLine("{0} had error {1}", threadsData[i].Name, threadsData[i].Error);}else if (threadsData[i].Barcodes != null){totalBarcodes.AddRange(threadsData[i].Barcodes);}}}// We are done, show the total number of barcodes readConsole.WriteLine("Done. Total barcodes read: {0}", totalBarcodes.Count);}private static void ReadBarcodesInThread(object state){MyThreadData threadData = state as MyThreadData;Console.WriteLine("{0} is reading the barcodes", threadData.Name);try{// Load the imageusing (RasterCodecs codecs = new RasterCodecs()){using (RasterImage image = codecs.Load(threadData.ImageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)){// Read the barcodes using our optionsthreadData.Barcodes = threadData.BarcodeReaderInstance.ReadBarcodes(image, LogicalRectangle.Empty, 0, null, threadData.ReadOptions);}}Console.WriteLine("{0} has read {1} barcodes", threadData.Name, threadData.Barcodes.Length);}catch (Exception ex){// Return the error to mainthreadData.Error = ex;}finally{// Tell main we are donethreadData.FinishedEvent.Set();}}private static string[] PrepareImages(){// Create a 90 degrees rotated version of Barcode1.tifstring originalImageFileName = Path.Combine(LEAD_VARS.ImagesDir, "Barcode1.tif");string rotatedImageFileName = Path.Combine(LEAD_VARS.ImagesDir, "Barcode1_Rotated90.tif");using (RasterCodecs codecs = new RasterCodecs()){using (RasterImage image = codecs.Load(originalImageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)){RotateCommand rotate = new RotateCommand(90 * 100, RotateCommandFlags.Resize, RasterColor.FromKnownColor(RasterKnownColor.White));rotate.Run(image);codecs.Save(image, rotatedImageFileName, RasterImageFormat.CcittGroup4, 1);}}return new string[] { originalImageFileName, rotatedImageFileName };}private static BarcodeReadOptions[] GetVerticalReadBarcodeOptions(BarcodeReader reader){// By default, the options read horizontal barcodes only, create an array of options capable of reading vertical barcodes// Notice, we cloned the default options in reader so we will not change the original optionsOneDBarcodeReadOptions oneDReadOptions = reader.GetDefaultOptions(BarcodeSymbology.UPCA).Clone() as OneDBarcodeReadOptions;oneDReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;FourStateBarcodeReadOptions fourStateReadOptions = reader.GetDefaultOptions(BarcodeSymbology.USPS4State).Clone() as FourStateBarcodeReadOptions;fourStateReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;PostNetPlanetBarcodeReadOptions postNetPlanetReadOptions = reader.GetDefaultOptions(BarcodeSymbology.PostNet).Clone() as PostNetPlanetBarcodeReadOptions;postNetPlanetReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;GS1DatabarStackedBarcodeReadOptions gs1StackedReadOptions = reader.GetDefaultOptions(BarcodeSymbology.GS1DatabarStacked).Clone() as GS1DatabarStackedBarcodeReadOptions;gs1StackedReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;PatchCodeBarcodeReadOptions patchCodeReadOptions = reader.GetDefaultOptions(BarcodeSymbology.PatchCode).Clone() as PatchCodeBarcodeReadOptions;patchCodeReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;PDF417BarcodeReadOptions pdf417ReadOptions = reader.GetDefaultOptions(BarcodeSymbology.PDF417).Clone() as PDF417BarcodeReadOptions;pdf417ReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;MicroPDF417BarcodeReadOptions microPdf417ReadOptions = reader.GetDefaultOptions(BarcodeSymbology.MicroPDF417).Clone() as MicroPDF417BarcodeReadOptions;microPdf417ReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;// Even though this array will not contain all options, it should be enough to read all barcodes, since the version of ReadBarcodes we will use// will use the default options if an overriden is not passedBarcodeReadOptions[] readOptions ={oneDReadOptions, fourStateReadOptions, postNetPlanetReadOptions, gs1StackedReadOptions, patchCodeReadOptions, pdf417ReadOptions, microPdf417ReadOptions};return readOptions;}static class LEAD_VARS{public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.FormsImports Leadtools.BarcodeImports Leadtools.ImageProcessing' This class holds the data we will pass to each threadPublic Class MyThreadData' This worker thread namePublic Name As String' Barcode reader instancePublic BarcodeReaderInstance As BarcodeReader' Array of options to usePublic ReadOptions As BarcodeReadOptions()' Image file containing the barcodesPublic ImageFileName As String' This will hold the barcodes foundPublic Barcodes As BarcodeData()' In case of errorsPublic [Error] As Exception' Event to notify when the thread is finishedPublic FinishedEvent As ManualResetEventEnd Class<TestMethod>Public Sub BarcodeReader_ReadBarcodeExample6()Dim imageFileNames As String() = PrepareImages()' Create a Barcode engineDim engine As New BarcodeEngine()' Get the Barcode reader instanceDim reader As BarcodeReader = engine.Reader' Get a version of all read options that support rotated barcodesDim verticalBarcodeReadOptions As BarcodeReadOptions() = GetVerticalReadBarcodeOptions(reader)' Synchronization eventsDim finishedEvents As ManualResetEvent() = New ManualResetEvent(1) {}' Create the thread data objectsDim threadsData As MyThreadData() = New MyThreadData(1) {}For i As Integer = 0 To threadsData.Length - 1finishedEvents(i) = New ManualResetEvent(False)threadsData(i) = New MyThreadData()threadsData(i).BarcodeReaderInstance = readerthreadsData(i).ReadOptions = NothingthreadsData(i).FinishedEvent = finishedEvents(i)Next' Setup the read options for the two threadsthreadsData(0).Name = "Read Default Options Thread"threadsData(0).ReadOptions = Nothing' Default, or horizontalthreadsData(1).Name = "Read Vertical Barcodes Thread"threadsData(1).ReadOptions = verticalBarcodeReadOptionsDim totalBarcodes As New List(Of BarcodeData)()' Now loop through all the images and try to read the barcodes with both threads at the same timeFor Each imageFileName As String In imageFileNamesConsole.WriteLine("Reading barcodes from {0}", imageFileName)For i As Integer = 0 To 1' Clear the previous results (if any), set up the file name and runthreadsData(i).Barcodes = NothingthreadsData(i).[Error] = NothingthreadsData(i).ImageFileName = imageFileNameThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf ReadBarcodesInThread), threadsData(i))Next' Wait till all the threads finishesFor Each waitHandle As ManualResetEvent In finishedEventswaitHandle.WaitOne()Next' Add the barcodes found to our listFor i As Integer = 0 To 1If threadsData(i).[Error] IsNot Nothing ThenConsole.WriteLine("{0} had error {1}", threadsData(i).Name, threadsData(i).[Error])ElseIf threadsData(i).Barcodes IsNot Nothing ThentotalBarcodes.AddRange(threadsData(i).Barcodes)End IfNextNext' We are done, show the total number of barcodes readConsole.WriteLine("Done. Total barcodes read: {0}", totalBarcodes.Count)End SubPrivate Shared Sub ReadBarcodesInThread(state As Object)Dim threadData As MyThreadData = TryCast(state, MyThreadData)Console.WriteLine("{0} is reading the barcodes", threadData.Name)Try' Load the imageUsing codecs As New RasterCodecs()Using image As RasterImage = codecs.Load(threadData.ImageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)' Read the barcodes using our optionsthreadData.Barcodes = threadData.BarcodeReaderInstance.ReadBarcodes(image, LogicalRectangle.Empty, 0, Nothing, threadData.ReadOptions)End UsingEnd UsingConsole.WriteLine("{0} has read {1} barcodes", threadData.Name, threadData.Barcodes.Length)Catch ex As Exception' Return the error to mainthreadData.[Error] = exFinally' Tell main we are donethreadData.FinishedEvent.[Set]()End TryEnd SubPrivate Shared Function PrepareImages() As String()' Create a 90 degrees rotated version of Barcode1.tifDim originalImageFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Barcode1.tif")Dim rotatedImageFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Barcode1_Rotated90.tif")Using codecs As New RasterCodecs()Using image As RasterImage = codecs.Load(originalImageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)Dim rotate As New RotateCommand(90 * 100, RotateCommandFlags.Resize, RasterColor.FromKnownColor(RasterKnownColor.White))rotate.Run(image)codecs.Save(image, rotatedImageFileName, RasterImageFormat.CcittGroup4, 1)End UsingEnd UsingReturn New String() {originalImageFileName, rotatedImageFileName}End FunctionPrivate Shared Function GetVerticalReadBarcodeOptions(reader As BarcodeReader) As BarcodeReadOptions()' By default, the options read horizontal barcodes only, create an array of options capable of reading vertical barcodes' Notice, we cloned the default options in reader so we will not change the original optionsDim oneDReadOptions As OneDBarcodeReadOptions = TryCast(reader.GetDefaultOptions(BarcodeSymbology.UPCA).Clone(), OneDBarcodeReadOptions)oneDReadOptions.SearchDirection = BarcodeSearchDirection.VerticalDim fourStateReadOptions As FourStateBarcodeReadOptions = TryCast(reader.GetDefaultOptions(BarcodeSymbology.USPS4State).Clone(), FourStateBarcodeReadOptions)fourStateReadOptions.SearchDirection = BarcodeSearchDirection.VerticalDim postNetPlanetReadOptions As PostNetPlanetBarcodeReadOptions = TryCast(reader.GetDefaultOptions(BarcodeSymbology.PostNet).Clone(), PostNetPlanetBarcodeReadOptions)postNetPlanetReadOptions.SearchDirection = BarcodeSearchDirection.VerticalDim gs1StackedReadOptions As GS1DatabarStackedBarcodeReadOptions = TryCast(reader.GetDefaultOptions(BarcodeSymbology.GS1DatabarStacked).Clone(), GS1DatabarStackedBarcodeReadOptions)gs1StackedReadOptions.SearchDirection = BarcodeSearchDirection.VerticalDim patchCodeReadOptions As PatchCodeBarcodeReadOptions = TryCast(reader.GetDefaultOptions(BarcodeSymbology.PatchCode).Clone(), PatchCodeBarcodeReadOptions)patchCodeReadOptions.SearchDirection = BarcodeSearchDirection.VerticalDim pdf417ReadOptions As PDF417BarcodeReadOptions = TryCast(reader.GetDefaultOptions(BarcodeSymbology.PDF417).Clone(), PDF417BarcodeReadOptions)pdf417ReadOptions.SearchDirection = BarcodeSearchDirection.VerticalDim microPdf417ReadOptions As MicroPDF417BarcodeReadOptions = TryCast(reader.GetDefaultOptions(BarcodeSymbology.MicroPDF417).Clone(), MicroPDF417BarcodeReadOptions)microPdf417ReadOptions.SearchDirection = BarcodeSearchDirection.Vertical' Even though this array will not contain all options, it should be enough to read all barcodes, since the version of ReadBarcodes we will use' will use the default options if an overriden is not passedDim readOptions As BarcodeReadOptions() = {oneDReadOptions, fourStateReadOptions, postNetPlanetReadOptions, gs1StackedReadOptions, patchCodeReadOptions, pdf417ReadOptions,microPdf417ReadOptions}Return readOptionsEnd FunctionPublic NotInheritable Class LEAD_VARSPublic Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"End Class
using Leadtools;using Leadtools.Codecs;using Leadtools.Forms;using Leadtools.Barcode;using Leadtools.ImageProcessing;using Leadtools.Examples;// This class holds the data we will pass to each threadpublic class MyThreadData{// This worker thread namepublic string Name;// Barcode reader instancepublic BarcodeReader BarcodeReaderInstance;// Array of options to usepublic BarcodeReadOptions[] ReadOptions;// Image file name containing the barcodespublic string ImageFileName;// Image containing the barcodespublic RasterImage Image;// This will hold the barcodes foundpublic BarcodeData[] Barcodes;// In case of errorspublic Exception Error;// Event to notify when the thread is finishedpublic AutoResetEvent FinishedEvent;}[Asynchronous]public void BarcodeReaderReadBarcodeExample6(){BarcodeReader_ReadBarcodeExample6();EnqueueTestComplete();}public void BarcodeReader_ReadBarcodeExample6(){RasterImage[] images = new RasterImage[2];string originalImageFileName = "Barcode1.tif";string rotatedImageFileName = "Barcode1_Rotated90.tif";string[] imageFileNames = new string[] { originalImageFileName, rotatedImageFileName };using (SampleImageStream outputStream = new SampleImageStream("Barcode1_Rotated90.tif")){images[0] = SampleImage.Get(SampleImageNames.Barcode1_tif).Clone();images[1] = SampleImage.Get(SampleImageNames.Barcode1_tif).Clone();RotateImage(images[1]);// Create a Barcode engineBarcodeEngine engine = new BarcodeEngine();// Get the Barcode reader instanceBarcodeReader reader = engine.Reader;// Get a version of all read options that support rotated barcodesBarcodeReadOptions[] verticalBarcodeReadOptions = GetVerticalReadBarcodeOptions(reader);// Synchronization eventsAutoResetEvent[] finishedEvents = new AutoResetEvent[2];// Create the thread data objectsMyThreadData[] threadsData = new MyThreadData[2];for (int i = 0; i < threadsData.Length; i++){finishedEvents[i] = new AutoResetEvent(false);threadsData[i] = new MyThreadData();threadsData[i].BarcodeReaderInstance = reader;threadsData[i].ReadOptions = null;threadsData[i].FinishedEvent = finishedEvents[i];}// Setup the read options for the two threadsthreadsData[0].Name = "Read Default Options Thread";threadsData[0].ReadOptions = null; // Default, or horizontalthreadsData[1].Name = "Read Vertical Barcodes Thread";threadsData[1].ReadOptions = verticalBarcodeReadOptions;List<BarcodeData> totalBarcodes = new List<BarcodeData>();// Now loop through all the images and try to read the barcodes with both threads at the same timeint index = 0;foreach (RasterImage image in images){Console.WriteLine("Reading barcodes from {0}", imageFileNames[index]);for (int i = 0; i < 2; i++){// Clear the previous results (if any), set up the file name and runthreadsData[i].Barcodes = null;threadsData[i].Error = null;threadsData[i].Image = image;threadsData[i].ImageFileName = imageFileNames[index];ThreadPool.QueueUserWorkItem(new WaitCallback(ReadBarcodesInThread), threadsData[i]);}// Wait till all the threads finishesWaitHandle.WaitAll(finishedEvents);// Add the barcodes found to our listfor (int i = 0; i < 2; i++){if (threadsData[i].Error != null){Console.WriteLine("{0} had error {1}", threadsData[i].Name, threadsData[i].Error);}else if (threadsData[i].Barcodes != null){totalBarcodes.AddRange(threadsData[i].Barcodes);}}index++;}// We are done, show the total number of barcodes readConsole.WriteLine("Done. Total barcodes read: {0}", totalBarcodes.Count);}}private static void ReadBarcodesInThread(object state){MyThreadData threadData = state as MyThreadData;Console.WriteLine("{0} is reading the barcodes", threadData.Name);try{// Load the imageRasterCodecs codecs = new RasterCodecs();using (RasterImage image = codecs.Load(threadData.ImageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)){// Read the barcodes using our optionsthreadData.Barcodes = threadData.BarcodeReaderInstance.ReadBarcodes(image, LogicalRectangle.Empty, 0, null, threadData.ReadOptions);}Console.WriteLine("{0} has read {1} barcodes", threadData.Name, threadData.Barcodes.Length);}catch (Exception ex){// Return the error to mainthreadData.Error = ex;}finally{// Tell main we are donethreadData.FinishedEvent.Set();}}private static void RotateImage(RasterImage image){// Create a 90 degrees rotated version of Barcode1.tifRasterCodecs codecs = new RasterCodecs();RotateCommand rotate = new RotateCommand(90 * 100, RotateCommandFlags.Resize, RasterColor.FromKnownColor(RasterKnownColor.White));rotate.Run(image);}private static BarcodeReadOptions[] GetVerticalReadBarcodeOptions(BarcodeReader reader){// By default, the options read horizontal barcodes only, create an array of options capable of reading vertical barcodes// Notice, we cloned the default options in reader so we will not change the original optionsOneDBarcodeReadOptions oneDReadOptions = reader.GetDefaultOptions(BarcodeSymbology.UPCA).Clone() as OneDBarcodeReadOptions;oneDReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;FourStateBarcodeReadOptions fourStateReadOptions = reader.GetDefaultOptions(BarcodeSymbology.USPS4State).Clone() as FourStateBarcodeReadOptions;fourStateReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;PostNetPlanetBarcodeReadOptions postNetPlanetReadOptions = reader.GetDefaultOptions(BarcodeSymbology.PostNet).Clone() as PostNetPlanetBarcodeReadOptions;postNetPlanetReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;GS1DatabarStackedBarcodeReadOptions gs1StackedReadOptions = reader.GetDefaultOptions(BarcodeSymbology.GS1DatabarStacked).Clone() as GS1DatabarStackedBarcodeReadOptions;gs1StackedReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;PatchCodeBarcodeReadOptions patchCodeReadOptions = reader.GetDefaultOptions(BarcodeSymbology.PatchCode).Clone() as PatchCodeBarcodeReadOptions;patchCodeReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;PDF417BarcodeReadOptions pdf417ReadOptions = reader.GetDefaultOptions(BarcodeSymbology.PDF417).Clone() as PDF417BarcodeReadOptions;pdf417ReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;MicroPDF417BarcodeReadOptions microPdf417ReadOptions = reader.GetDefaultOptions(BarcodeSymbology.MicroPDF417).Clone() as MicroPDF417BarcodeReadOptions;microPdf417ReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;// Even though this array will not contain all options, it should be enough to read all barcodes, since the version of ReadBarcodes we will use// will use the default options if an overriden is not passedBarcodeReadOptions[] readOptions ={oneDReadOptions, fourStateReadOptions, postNetPlanetReadOptions, gs1StackedReadOptions, patchCodeReadOptions, pdf417ReadOptions, microPdf417ReadOptions};return readOptions;}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.FormsImports Leadtools.BarcodeImports Leadtools.ImageProcessingImports Leadtools.ImageProcessing.Color' This class holds the data we will pass to each threadPublic Class MyThreadData' This worker thread namePublic Name As String' Barcode reader instancePublic BarcodeReaderInstance As BarcodeReader' Array of options to usePublic ReadOptions As BarcodeReadOptions()' Image file name containing the barcodesPublic ImageFileName As String' Image containing the barcodesPublic Image As RasterImage' This will hold the barcodes foundPublic Barcodes As BarcodeData()' In case of errorsPublic [Error] As Exception' Event to notify when the thread is finishedPublic FinishedEvent As AutoResetEventEnd Class<TestMethod(), Asynchronous()>Public Sub BarcodeReaderReadBarcodeExample6()BarcodeReader_ReadBarcodeExample6()EnqueueTestComplete()End SubPublic Sub BarcodeReader_ReadBarcodeExample6()Dim images As RasterImage() = New RasterImage(1) {}Dim originalImageFileName As String = "Barcode1.tif"Dim rotatedImageFileName As String = "Barcode1_Rotated90.tif"Dim imageFileNames As String() = New String() {originalImageFileName, rotatedImageFileName}Using outputStream As SampleImageStream = New SampleImageStream("Barcode1_Rotated90.tif")images(0) = SampleImage.Get(SampleImageNames.Barcode1_tif).Clone()images(1) = SampleImage.Get(SampleImageNames.Barcode1_tif).Clone()RotateImage(images(1))' Create a Barcode engineDim engine As BarcodeEngine = New BarcodeEngine()' Get the Barcode reader instanceDim reader As BarcodeReader = engine.Reader' Get a version of all read options that support rotated barcodesDim verticalBarcodeReadOptions As BarcodeReadOptions() = GetVerticalReadBarcodeOptions(reader)' Synchronization eventsDim finishedEvents As AutoResetEvent() = New AutoResetEvent(1) {}' Create the thread data objectsDim threadsData As MyThreadData() = New MyThreadData(1) {}Dim i As Integer = 0Do While i < threadsData.LengthfinishedEvents(i) = New AutoResetEvent(False)threadsData(i) = New MyThreadData()threadsData(i).BarcodeReaderInstance = readerthreadsData(i).ReadOptions = NothingthreadsData(i).FinishedEvent = finishedEvents(i)i += 1Loop' Setup the read options for the two threadsthreadsData(0).Name = "Read Default Options Thread"threadsData(0).ReadOptions = Nothing ' Default, or horizontalthreadsData(1).Name = "Read Vertical Barcodes Thread"threadsData(1).ReadOptions = verticalBarcodeReadOptionsDim totalBarcodes As List(Of BarcodeData) = New List(Of BarcodeData)()' Now loop through all the images and try to read the barcodes with both threads at the same timeDim index As Integer = 0For Each image As RasterImage In imagesConsole.WriteLine("Reading barcodes from {0}", imageFileNames(index))For i = 0 To 1' Clear the previous results (if any), set up the file name and runthreadsData(i).Barcodes = NothingthreadsData(i).Error = NothingthreadsData(i).Image = imagethreadsData(i).ImageFileName = imageFileNames(index)ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf ReadBarcodesInThread), threadsData(i))Next i' Wait till all the threads finishesWaitHandle.WaitAll(finishedEvents)' Add the barcodes found to our listFor i = 0 To 1If Not threadsData(i).Error Is Nothing ThenConsole.WriteLine("{0} had error {1}", threadsData(i).Name, threadsData(i).Error)ElseIf Not threadsData(i).Barcodes Is Nothing ThentotalBarcodes.AddRange(threadsData(i).Barcodes)End IfNext iindex += 1Next image' We are done, show the total number of barcodes readConsole.WriteLine("Done. Total barcodes read: {0}", totalBarcodes.Count)End UsingEnd SubPrivate Shared Sub ReadBarcodesInThread(ByVal state As Object)Dim threadData As MyThreadData = TryCast(state, MyThreadData)Console.WriteLine("{0} is reading the barcodes", threadData.Name)Try' Load the imageDim codecs As RasterCodecs = New RasterCodecs()Using image As RasterImage = codecs.Load(threadData.ImageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)' Read the barcodes using our optionsthreadData.Barcodes = threadData.BarcodeReaderInstance.ReadBarcodes(image, LogicalRectangle.Empty, 0, Nothing, threadData.ReadOptions)End UsingConsole.WriteLine("{0} has read {1} barcodes", threadData.Name, threadData.Barcodes.Length)Catch ex As Exception' Return the error to mainthreadData.Error = exFinally' Tell main we are donethreadData.FinishedEvent.Set()End TryEnd SubPrivate Shared Sub RotateImage(ByVal image As RasterImage)' Create a 90 degrees rotated version of Barcode1.tifDim codecs As RasterCodecs = New RasterCodecs()Dim rotate As RotateCommand = New RotateCommand(90 * 100, RotateCommandFlags.Resize, RasterColor.FromKnownColor(RasterKnownColor.White))rotate.Run(image)End SubPrivate Shared Function GetVerticalReadBarcodeOptions(ByVal reader As BarcodeReader) As BarcodeReadOptions()' By default, the options read horizontal barcodes only, create an array of options capable of reading vertical barcodes' Notice, we cloned the default options in reader so we will not change the original optionsDim oneDReadOptions As OneDBarcodeReadOptions = TryCast(reader.GetDefaultOptions(BarcodeSymbology.UPCA).Clone(), OneDBarcodeReadOptions)oneDReadOptions.SearchDirection = BarcodeSearchDirection.VerticalDim fourStateReadOptions As FourStateBarcodeReadOptions = TryCast(reader.GetDefaultOptions(BarcodeSymbology.USPS4State).Clone(), FourStateBarcodeReadOptions)fourStateReadOptions.SearchDirection = BarcodeSearchDirection.VerticalDim postNetPlanetReadOptions As PostNetPlanetBarcodeReadOptions = TryCast(reader.GetDefaultOptions(BarcodeSymbology.PostNet).Clone(), PostNetPlanetBarcodeReadOptions)postNetPlanetReadOptions.SearchDirection = BarcodeSearchDirection.VerticalDim gs1StackedReadOptions As GS1DatabarStackedBarcodeReadOptions = TryCast(reader.GetDefaultOptions(BarcodeSymbology.GS1DatabarStacked).Clone(), GS1DatabarStackedBarcodeReadOptions)gs1StackedReadOptions.SearchDirection = BarcodeSearchDirection.VerticalDim patchCodeReadOptions As PatchCodeBarcodeReadOptions = TryCast(reader.GetDefaultOptions(BarcodeSymbology.PatchCode).Clone(), PatchCodeBarcodeReadOptions)patchCodeReadOptions.SearchDirection = BarcodeSearchDirection.VerticalDim pdf417ReadOptions As PDF417BarcodeReadOptions = TryCast(reader.GetDefaultOptions(BarcodeSymbology.PDF417).Clone(), PDF417BarcodeReadOptions)pdf417ReadOptions.SearchDirection = BarcodeSearchDirection.VerticalDim microPdf417ReadOptions As MicroPDF417BarcodeReadOptions = TryCast(reader.GetDefaultOptions(BarcodeSymbology.MicroPDF417).Clone(), MicroPDF417BarcodeReadOptions)microPdf417ReadOptions.SearchDirection = BarcodeSearchDirection.Vertical' Even though this array will not contain all options, it should be enough to read all barcodes, since the version of ReadBarcodes we will use' will use the default options if an overriden is not passedDim readOptions As BarcodeReadOptions() = {oneDReadOptions, fourStateReadOptions, postNetPlanetReadOptions, gs1StackedReadOptions, patchCodeReadOptions, pdf417ReadOptions, microPdf417ReadOptions}Return readOptionsEnd Function
ReadBarcode(RasterImage image, LogicalRectangle searchBounds, BarcodeSymbology symbology)
ReadBarcode(RasterImage image, LogicalRectangle searchBounds, BarcodeSymbology[] symbologies)
|
Products |
Support |
Feedback: ReadBarcodes(RasterImage,LogicalRectangle,Int32,BarcodeSymbology[],BarcodeReadOptions[]) Method - Leadtools.Barcode |
Introduction |
Help Version 19.0.2017.6.21
|

Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
Your email has been sent to support! Someone should be in touch! If your matter is urgent please come back into chat.
Chat Hours:
Monday - Friday, 8:30am to 6pm ET
Thank you for your feedback!
Please fill out the form again to start a new chat.
All agents are currently offline.
Chat Hours:
Monday - Friday
8:30AM - 6PM EST
To contact us please fill out this form and we will contact you via email.