Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To perform barcode recognition, Aspose.BarCode for .NET provides class BarCodeReader that relies on machine vision algorithms. Such algorithms are suitable for parallel execution, and as such, allow speeding up the recognition process by utilizing several CPU cores simultaneously.
To perform multithread barcode reading, class BarCodeReader provides a group of properties called ProcessorSettings that can be used to set all necessary parameters to optimize CPU core usage. Such parameters are initialized globally for all BarCodeReader objects and in most cases do not require additional tuning.
In Aspose.BarCode for .NET, multithreading is based on the system parameter called ThreadPool; therefore, to enable efficient use of multithreading, it is necessary to set minimal and maximal values of available cores through system methods SetMinThreads and SetMaxThreads, respectively.
To define multithread recognition settings manually, the following parameters can be used:
In cases when there is a reason not to use additional CPU cores and to perform single-thread barcode recognition, this can be implemented by setting corresponding parameters UseAllCores, UseOnlyThisCoresCount, and MaxAdditionalAllowedThreads of class ProcessorSettings as demonstrated in the code sample provided below.
Console.WriteLine("ReadMTSingleCore:");
//Set single-thread recognition
BarCodeReader.ProcessorSettings.UseAllCores = false;
BarCodeReader.ProcessorSettings.UseOnlyThisCoresCount = 1;
BarCodeReader.ProcessorSettings.MaxAdditionalAllowedThreads = 0;
//read barcode image
using (BarCodeReader read = new BarCodeReader($"{path}many_pdf417.png", DecodeType.Pdf417))
{
Stopwatch watch = Stopwatch.StartNew();
read.ReadBarCodes();
watch.Stop();
Console.WriteLine($"Barcodes read: {read.FoundCount}, Recognition time:{(int)watch.ElapsedMilliseconds} ms");
foreach (BarCodeResult result in read.FoundBarCodes)
Console.WriteLine($"{result.CodeTypeName}:{result.CodeText}");
}
ReadMTSingleCore:
Barcodes read: 6,
Recognition time:604 ms
Pdf417:Aspose PDF417 Diag 02
Pdf417:Aspose PDF417 Diag 01
Pdf417:Aspose PDF417 02
Pdf417:Aspose PDF417 01
Pdf417:Aspose PDF417 03
Pdf417:Aspose PDF417 04
To fix the number of CPU cores that can be allocated for barcode recognition, it is required to apply specific settings to multithreading parameters UseAllCores, UseOnlyThisCoresCount, and MaxAdditionalAllowedThreads of class ProcessorSettings as explained in the code sample given below.
Console.WriteLine("ReadMTRestrictedCores:");
//allowed cores
int AllowedCores = 2;
//Init ThreadPool options
int workerThreads;
int completionPortThreads;
ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
ThreadPool.SetMaxThreads(Math.Max(AllowedCores * 4, workerThreads), completionPortThreads);
ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads);
ThreadPool.SetMinThreads(Math.Max(AllowedCores * 4, workerThreads), completionPortThreads);
//Set multithread recognition using all available cores
BarCodeReader.ProcessorSettings.UseAllCores = false;
BarCodeReader.ProcessorSettings.UseOnlyThisCoresCount = AllowedCores;
BarCodeReader.ProcessorSettings.MaxAdditionalAllowedThreads = AllowedCores;
//read barcode image
using (BarCodeReader read = new BarCodeReader($"{path}many_pdf417.png", DecodeType.Pdf417))
{
Stopwatch watch = Stopwatch.StartNew();
read.ReadBarCodes();
watch.Stop();
Console.WriteLine($"Barcodes read: {read.FoundCount}, Recognition time:{(int)watch.ElapsedMilliseconds} ms");
foreach (BarCodeResult result in read.FoundBarCodes)
Console.WriteLine($"{result.CodeTypeName}:{result.CodeText}");
}
ReadMTRestrictedCores:
Barcodes read: 6,
Recognition time:400 ms
Pdf417:Aspose PDF417 Diag 02
Pdf417:Aspose PDF417 Diag 01
Pdf417:Aspose PDF417 02
Pdf417:Aspose PDF417 01
Pdf417:Aspose PDF417 03
Pdf417:Aspose PDF417 04
To set automated allocation of the maximal possible number of cores for barcode recognition, the following settings need to be applied as shown in the code sample below. In this case, class BarCodeReader defines the number of required cores without the need for explicit manual instructions.
Console.WriteLine("ReadMTAllCores:");
//Init ThreadPool options
int workerThreads;
int completionPortThreads;
ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
ThreadPool.SetMaxThreads(Math.Max(Environment.ProcessorCount * 4, workerThreads), completionPortThreads);
ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads);
ThreadPool.SetMinThreads(Math.Max(Environment.ProcessorCount * 4, workerThreads), completionPortThreads);
//Set multithread recognition using all available cores
BarCodeReader.ProcessorSettings.UseAllCores = true;
BarCodeReader.ProcessorSettings.MaxAdditionalAllowedThreads = Environment.ProcessorCount * 2;
//Read barcode image
using (BarCodeReader read = new BarCodeReader($"{path}many_pdf417.png", DecodeType.Pdf417))
{
Stopwatch watch = Stopwatch.StartNew();
read.ReadBarCodes();
watch.Stop();
Console.WriteLine($"Barcodes read: {read.FoundCount}, Recognition time:{(int)watch.ElapsedMilliseconds} ms");
foreach (BarCodeResult result in read.FoundBarCodes)
Console.WriteLine($"{result.CodeTypeName}:{result.CodeText}");
}
ReadMTAllCores:
Barcodes read: 6,
Recognition time:244 ms
Pdf417:Aspose PDF417 Diag 02
Pdf417:Aspose PDF417 Diag 01
Pdf417:Aspose PDF417 02
Pdf417:Aspose PDF417 01
Pdf417:Aspose PDF417 03
Pdf417:Aspose PDF417 04
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.