Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Title: Adjust .NET ThreadPool settings for barcode reading
// Description: Demonstrates how to set ThreadPool minimum and maximum threads before generating and reading a barcode image using Aspose.BarCode.
// Category-Description: This example belongs to the Aspose.BarCode .NET barcode generation and recognition category. It showcases the use of BarcodeGenerator for creating a Code128 barcode and BarCodeReader for decoding it, while configuring ThreadPool limits to optimize multithreaded performance. Developers often need to adjust thread pool settings when processing many images concurrently in high‑throughput applications.
// Title: Adjust .NET ThreadPool settings and read a generated Code128 barcode
// Description: This example generates a Code128 barcode image, configures the .NET ThreadPool limits, then reads the barcode using Aspose.BarCode.
// Category-Description: Demonstrates basic Aspose.BarCode operations including barcode generation (BarcodeGenerator) and recognition (BarCodeReader) with thread pool tuning. Useful for developers needing to control concurrency while processing barcodes in high‑throughput scenarios. Covers common use cases such as creating PNG images and decoding all supported symbologies.
// Prompt: Adjust .NET ThreadPool minimum threads to 2 and maximum threads to 8 before creating BarCodeReader instances.
// Tags: barcode symbology, generation, recognition, code128, threadpool, aspnet, aspose.barcode
// Tags: code128, barcode-generation, barcode-recognition, png, threadpool, aspose.barcode

using System;
using System.IO;
Expand All @@ -12,63 +12,53 @@
using Aspose.BarCode.BarCodeRecognition;

/// <summary>
/// Demonstrates adjusting .NET ThreadPool settings and using Aspose.BarCode to generate and read a Code128 barcode.
/// Demonstrates generating a barcode, configuring ThreadPool limits, and reading the barcode using Aspose.BarCode.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the example. Configures ThreadPool limits, creates a barcode image, reads it, and cleans up.
/// Entry point of the example. Generates a Code128 barcode, sets ThreadPool thread counts, reads the barcode, and cleans up.
/// </summary>
static void Main()
{
// --------------------------------------------------------------------
// Adjust ThreadPool settings before any barcode operations are performed
// --------------------------------------------------------------------
ThreadPool.GetMinThreads(out int minWorker, out int minIOC);
ThreadPool.SetMinThreads(2, minIOC); // Set minimum worker threads to 2
ThreadPool.GetMaxThreads(out int maxWorker, out int maxIOC);
ThreadPool.SetMaxThreads(8, maxIOC); // Set maximum worker threads to 8
// Define the temporary file path for the generated barcode image
string barcodePath = "sample_barcode.png";

// -------------------------------------------------
// Generate a sample barcode image using Code128 symbology
// -------------------------------------------------
string imagePath = "sample_barcode.png";
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample123"))
// Generate a simple Code128 barcode and save it as a PNG file
using (BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "123456789"))
{
generator.Save(imagePath, BarCodeImageFormat.Png);
generator.Save(barcodePath, BarCodeImageFormat.Png);
}

// -------------------------------------------------
// Verify that the barcode image was successfully created
// -------------------------------------------------
if (!File.Exists(imagePath))
{
Console.WriteLine("Failed to create barcode image.");
return;
}
// Adjust ThreadPool settings: set minimum to 2 worker threads and maximum to 8 worker threads
int workerThreads, completionPortThreads;
ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads);
ThreadPool.SetMinThreads(2, completionPortThreads);
ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
ThreadPool.SetMaxThreads(8, completionPortThreads);

// -------------------------------------------------
// Read the barcode from the generated image using BarCodeReader
// -------------------------------------------------
using (var reader = new BarCodeReader(imagePath, DecodeType.Code128))
// Read the barcode using BarCodeReader with all supported decode types
using (BarCodeReader reader = new BarCodeReader(barcodePath, DecodeType.AllSupportedTypes))
{
foreach (var result in reader.ReadBarCodes())
BarCodeResult[] results = reader.ReadBarCodes();
foreach (BarCodeResult result in results)
{
Console.WriteLine($"Detected Type: {result.CodeTypeName}");
Console.WriteLine($"Detected Text: {result.CodeText}");
}
}

// -------------------------------------------------
// Clean up the sample image file (optional)
// -------------------------------------------------
try
{
File.Delete(imagePath);
}
catch
// Clean up the temporary barcode image file
if (File.Exists(barcodePath))
{
// Ignore any cleanup errors
try
{
File.Delete(barcodePath);
}
catch (Exception ex)
{
Console.WriteLine($"Failed to delete temporary file: {ex.Message}");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,59 @@
// Title: Barcode Recognition Using All CPU Cores
// Description: Demonstrates how to enable multi‑core processing for barcode recognition with Aspose.BarCode and reads a generated Code128 barcode.
// Category-Description: This example belongs to the Aspose.BarCode barcode recognition category, illustrating the use of BarCodeReader and its ProcessorSettings to leverage all available CPU cores for faster decoding. Typical use cases include high‑throughput scanning applications where performance is critical. Developers often need to configure ProcessorSettings, select DecodeType, and retrieve barcode metadata such as type, text, and region.
// Title: Using Aspose.BarCode to generate and recognize a Code128 barcode with multi‑core processing
// Description: Demonstrates creating a Code128 barcode image, saving it, and then recognizing it while configuring the processor to utilize all CPU cores.
// Category-Description: This example belongs to the Aspose.BarCode generation and recognition category. It shows how to use BarcodeGenerator to create barcodes and BarCodeReader with ProcessorSettings to perform high‑performance recognition. Developers often need to generate barcodes for labeling and then read them in batch scenarios, where enabling multi‑core processing improves throughput.
// Prompt: Configure ProcessorSettings.UseAllCores true to allocate all CPU cores automatically for barcode recognition.
// Tags: barcode, recognition, multithreading, useallcores, code128, aspnet, aspnetcore, aspose.barcode, image processing
// Tags: code128, barcode-generation, barcode-recognition, multithreading, useallcores, aspose-barcodes, png

using System;
using System.IO;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.BarCode.BarCodeRecognition;
using Aspose.Drawing;

/// <summary>
/// Demonstrates configuring Aspose.BarCode to use all CPU cores for barcode recognition and reading a Code128 barcode.
/// Example program that generates a Code128 barcode, saves it as PNG,
/// configures the recognition processor to use all CPU cores, reads the barcode,
/// and cleans up the temporary image file.
/// </summary>
class Program
{
/// <summary>
/// Entry point that generates a sample barcode if missing, enables multi‑core processing, and reads the barcode information.
/// Entry point of the example. Executes barcode generation, multi‑core recognition,
/// and cleanup operations.
/// </summary>
static void Main()
{
// Enable utilization of all CPU cores for barcode recognition
BarCodeReader.ProcessorSettings.UseAllCores = true;

// Path to the barcode image file
string imagePath = "barcode.png";
// Define the temporary file path for the generated barcode image.
string imagePath = "sample.png";

// Generate a sample Code128 barcode image if it does not already exist
if (!File.Exists(imagePath))
// Generate a simple Code128 barcode and save it as a PNG file.
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "12345"))
{
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "123456789"))
{
generator.Save(imagePath, BarCodeImageFormat.Png);
}
// Set barcode foreground and background colors (optional).
generator.Parameters.Barcode.BarColor = Color.Black;
generator.Parameters.BackColor = Color.White;

// Save the barcode image to the specified path.
generator.Save(imagePath, BarCodeImageFormat.Png);
}

// Initialize the reader to decode all supported barcode types from the image
using (var reader = new BarCodeReader(imagePath, DecodeType.AllSupportedTypes))
// Enable multi‑core processing for barcode recognition to improve performance.
BarCodeReader.ProcessorSettings.UseAllCores = true;

// Read and display barcode information from the saved image.
using (var reader = new BarCodeReader(imagePath))
{
// Iterate through all detected barcodes and output their details
foreach (var result in reader.ReadBarCodes())
{
Console.WriteLine($"Barcode Type: {result.CodeTypeName}");
Console.WriteLine($"Barcode Text: {result.CodeText}");

var bounds = result.Region.Rectangle;
Console.WriteLine($"Region - X:{bounds.X}, Y:{bounds.Y}, Width:{bounds.Width}, Height:{bounds.Height}");
Console.WriteLine($"Detected Type: {result.CodeTypeName}");
Console.WriteLine($"Detected Text: {result.CodeText}");
}
}

// Delete the temporary image file to clean up resources.
if (File.Exists(imagePath))
{
File.Delete(imagePath);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,88 +1,113 @@
// Title: Background Worker Barcode Reader from Video Stream
// Description: Demonstrates reading barcodes from simulated video frames using a BackgroundWorker and ProcessorSettings to control core usage.
// Category-Description: This example belongs to the Aspose.BarCode generation and recognition category, showcasing how to generate barcodes, process them in a background thread, and fine‑tune multi‑core utilization via ProcessorSettings. Developers often need to handle high‑throughput image streams (e.g., video) and require optimal CPU usage while recognizing multiple symbologies using BarCodeReader, BarcodeGenerator, and QualitySettings.
// Title: Background worker barcode reading with processor settings
// Description: Demonstrates generating sample barcode images, configuring Aspose.BarCode processor settings for multi‑core usage, and reading the barcodes asynchronously using a BackgroundWorker.
// Category-Description: This example belongs to the Aspose.BarCode generation and recognition category. It showcases key API classes such as BarcodeGenerator, BarCodeReader, and ProcessorSettings, illustrating typical scenarios where developers need to generate barcodes, optimize recognition performance across CPU cores, and process images in a background thread for responsive applications.
// Prompt: Create a background worker that reads barcodes from a video stream using ProcessorSettings for optimal core usage.
// Tags: code128, read, console, barcodegenerator, barcodereader, processorsettings, qualitysettings
// Tags: code128, qr, generation, reading, png, barcodegenerator, barcodereader, backgroundworker

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.ComponentModel;
using System.Threading;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.BarCode.BarCodeRecognition;
using Aspose.Drawing;
using Aspose.Drawing.Imaging;

/// <summary>
/// Example program that generates barcode images, simulates video frames,
/// and reads them in a background worker using Aspose.BarCode APIs.
/// Demonstrates generating sample barcodes, configuring multi‑core processor settings,
/// and reading the barcodes asynchronously using a BackgroundWorker.
/// </summary>
class Program
{
/// <summary>
/// Entry point. Generates sample frames, configures processor settings,
/// and processes frames asynchronously.
/// Entry point. Sets processor settings, creates sample barcodes, runs a background
/// worker to read them, and cleans up temporary files.
/// </summary>
static void Main()
static void Main(string[] args)
{
// Generate a few barcode images to simulate video frames
var frames = new List<byte[]>();
for (int i = 0; i < 3; i++)
// Enable use of all CPU cores for barcode processing
BarCodeReader.ProcessorSettings.UseAllCores = true;
// Allow additional threads proportional to processor count for better throughput
BarCodeReader.ProcessorSettings.MaxAdditionalAllowedThreads = Environment.ProcessorCount * 2;

// Create a temporary folder to store generated barcode images
string tempFolder = Path.Combine(Path.GetTempPath(), "AsposeBarcodeSample");
Directory.CreateDirectory(tempFolder);
GenerateSampleBarcodes(tempFolder);

// Set up a BackgroundWorker to process the images without blocking the main thread
using (var worker = new BackgroundWorker())
{
// Create a barcode generator for Code128 with unique text
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, $"Sample{i + 1}"))
var completedEvent = new ManualResetEventSlim(false);

// Define the work to be performed in the background thread
worker.DoWork += (sender, e) => ProcessImages(tempFolder);
// Signal completion when the background work finishes
worker.RunWorkerCompleted += (sender, e) => completedEvent.Set();

// Start the background operation
worker.RunWorkerAsync();

// Wait for the background worker to finish, but limit wait time to avoid hanging
if (!completedEvent.Wait(TimeSpan.FromSeconds(30)))
{
// Set a simple visual dimension
generator.Parameters.Barcode.XDimension.Point = 2f;
// Render the barcode to a bitmap
using (var bitmap = generator.GenerateBarCodeImage())
{
// Save bitmap to memory stream as PNG
using (var ms = new MemoryStream())
{
bitmap.Save(ms, ImageFormat.Png);
frames.Add(ms.ToArray());
}
}
Console.WriteLine("Processing timed out.");
}
}

// Synchronization primitive to wait for background work completion
var doneEvent = new ManualResetEventSlim(false);
// Attempt to delete the temporary folder and its contents; ignore any errors in CI environments
try
{
Directory.Delete(tempFolder, true);
}
catch
{
// Suppress cleanup exceptions
}
}

// BackgroundWorker that processes the simulated video frames
var worker = new BackgroundWorker();
worker.DoWork += (sender, args) =>
// Generates a few barcode images for demonstration purposes
private static void GenerateSampleBarcodes(string folder)
{
const int sampleCount = 5;
for (int i = 0; i < sampleCount; i++)
{
// Configure processor settings for optimal core usage
BarCodeReader.ProcessorSettings.UseAllCores = false;
BarCodeReader.ProcessorSettings.UseOnlyThisCoresCount = Math.Max(1, Environment.ProcessorCount / 2);
string text = $"Sample{i}";
string filePath = Path.Combine(folder, $"barcode{i}.png");
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, text))
{
// Simple generation; default settings are sufficient
generator.Save(filePath);
}
}
}

// Iterate over each simulated frame
foreach (var frameData in frames)
// Reads barcodes from all PNG files in the specified folder
private static void ProcessImages(string folder)
{
string[] files = Directory.GetFiles(folder, "*.png");
// Iterate over each image file
for (int i = 0; i < files.Length; i++)
{
string file = files[i];
try
{
// Create a memory stream from the frame bytes
using (var stream = new MemoryStream(frameData))
// Initialize the barcode reader for all supported symbologies
using (var reader = new BarCodeReader(stream, DecodeType.AllSupportedTypes))
// Initialize the reader for Code128 and QR symbologies
using (var reader = new BarCodeReader(file, DecodeType.Code128, DecodeType.QR))
{
// Apply a high‑performance quality preset
reader.QualitySettings = QualitySettings.HighPerformance;

// Read and output all detected barcodes
// Read and output each detected barcode
foreach (var result in reader.ReadBarCodes())
{
Console.WriteLine($"Detected: {result.CodeTypeName} - {result.CodeText}");
Console.WriteLine($"File: {Path.GetFileName(file)} | Type: {result.CodeTypeName} | Text: {result.CodeText}");
}
}
}
};
// Signal completion when background work finishes
worker.RunWorkerCompleted += (s, e) => doneEvent.Set();

// Start processing and wait until it finishes
worker.RunWorkerAsync();
doneEvent.Wait();
catch (Exception ex)
{
Console.WriteLine($"Error processing '{Path.GetFileName(file)}': {ex.Message}");
}
}
}
}
Loading
Loading