Skip to content

Commit 6fdc36b

Browse files
Merge pull request #82 from aspose-barcode/agent/special-barcode-recognition-settings/2026-07-11-153332
feat: Add Aspose.BarCode .NET C# Examples — Special Barcode Recognition Settings
2 parents 86ec131 + a7d87cc commit 6fdc36b

48 files changed

Lines changed: 2225 additions & 2133 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,74 @@
1+
// Title: Adjust .NET ThreadPool settings for barcode reading
2+
// Description: Demonstrates how to set ThreadPool minimum and maximum threads before generating and reading a barcode image using Aspose.BarCode.
3+
// 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.
4+
// Prompt: Adjust .NET ThreadPool minimum threads to 2 and maximum threads to 8 before creating BarCodeReader instances.
5+
// Tags: barcode symbology, generation, recognition, code128, threadpool, aspnet, aspose.barcode
6+
17
using System;
28
using System.IO;
39
using System.Threading;
410
using Aspose.BarCode;
511
using Aspose.BarCode.Generation;
612
using Aspose.BarCode.BarCodeRecognition;
7-
using Aspose.Drawing;
813

914
/// <summary>
10-
/// Demonstrates generating a barcode, storing it in memory, and reading it back using Aspose.BarCode.
15+
/// Demonstrates adjusting .NET ThreadPool settings and using Aspose.BarCode to generate and read a Code128 barcode.
1116
/// </summary>
1217
class Program
1318
{
1419
/// <summary>
15-
/// Entry point of the application.
16-
/// Configures thread pool, creates a barcode image in memory, and reads it back.
20+
/// Entry point of the example. Configures ThreadPool limits, creates a barcode image, reads it, and cleans up.
1721
/// </summary>
1822
static void Main()
1923
{
20-
// Configure the thread pool to have a minimum of 2 worker and I/O threads,
21-
// and a maximum of 8 worker and I/O threads.
22-
ThreadPool.SetMinThreads(2, 2);
23-
ThreadPool.SetMaxThreads(8, 8);
24+
// --------------------------------------------------------------------
25+
// Adjust ThreadPool settings before any barcode operations are performed
26+
// --------------------------------------------------------------------
27+
ThreadPool.GetMinThreads(out int minWorker, out int minIOC);
28+
ThreadPool.SetMinThreads(2, minIOC); // Set minimum worker threads to 2
29+
ThreadPool.GetMaxThreads(out int maxWorker, out int maxIOC);
30+
ThreadPool.SetMaxThreads(8, maxIOC); // Set maximum worker threads to 8
2431

25-
// Use a memory stream to hold the generated barcode image.
26-
using (var ms = new MemoryStream())
32+
// -------------------------------------------------
33+
// Generate a sample barcode image using Code128 symbology
34+
// -------------------------------------------------
35+
string imagePath = "sample_barcode.png";
36+
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample123"))
2737
{
28-
// Create a barcode generator for Code128 with the data "123456".
29-
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "123456"))
30-
{
31-
// Save the generated barcode as a PNG image into the memory stream.
32-
generator.Save(ms, BarCodeImageFormat.Png);
33-
}
38+
generator.Save(imagePath, BarCodeImageFormat.Png);
39+
}
3440

35-
// Reset the stream position to the beginning before reading.
36-
ms.Position = 0;
41+
// -------------------------------------------------
42+
// Verify that the barcode image was successfully created
43+
// -------------------------------------------------
44+
if (!File.Exists(imagePath))
45+
{
46+
Console.WriteLine("Failed to create barcode image.");
47+
return;
48+
}
3749

38-
// Initialize a barcode reader that can decode all supported barcode types,
39-
// using the memory stream that contains the PNG image.
40-
using (var reader = new BarCodeReader(ms, DecodeType.AllSupportedTypes))
50+
// -------------------------------------------------
51+
// Read the barcode from the generated image using BarCodeReader
52+
// -------------------------------------------------
53+
using (var reader = new BarCodeReader(imagePath, DecodeType.Code128))
54+
{
55+
foreach (var result in reader.ReadBarCodes())
4156
{
42-
// Iterate through all detected barcodes and output their type and text.
43-
foreach (var result in reader.ReadBarCodes())
44-
{
45-
Console.WriteLine($"Type: {result.CodeTypeName}, Text: {result.CodeText}");
46-
}
57+
Console.WriteLine($"Detected Type: {result.CodeTypeName}");
58+
Console.WriteLine($"Detected Text: {result.CodeText}");
4759
}
4860
}
61+
62+
// -------------------------------------------------
63+
// Clean up the sample image file (optional)
64+
// -------------------------------------------------
65+
try
66+
{
67+
File.Delete(imagePath);
68+
}
69+
catch
70+
{
71+
// Ignore any cleanup errors
72+
}
4973
}
5074
}

special-barcode-recognition-settings/configure-processorsettingsuseallcores-true-to-allocate-all-cpu-cores-automatically-for-barcode-recognition.cs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,51 @@
1+
// Title: Barcode Recognition Using All CPU Cores
2+
// Description: Demonstrates how to enable multi‑core processing for barcode recognition with Aspose.BarCode and reads a generated Code128 barcode.
3+
// 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.
4+
// Prompt: Configure ProcessorSettings.UseAllCores true to allocate all CPU cores automatically for barcode recognition.
5+
// Tags: barcode, recognition, multithreading, useallcores, code128, aspnet, aspnetcore, aspose.barcode, image processing
6+
17
using System;
28
using System.IO;
39
using Aspose.BarCode;
410
using Aspose.BarCode.Generation;
511
using Aspose.BarCode.BarCodeRecognition;
6-
using Aspose.Drawing;
712

813
/// <summary>
9-
/// Demonstrates generating a Code128 barcode, reading it back, and cleaning up the temporary file.
14+
/// Demonstrates configuring Aspose.BarCode to use all CPU cores for barcode recognition and reading a Code128 barcode.
1015
/// </summary>
1116
class Program
1217
{
1318
/// <summary>
14-
/// Entry point of the application.
15-
/// Generates a barcode image, reads it, displays the results, and deletes the temporary file.
19+
/// Entry point that generates a sample barcode if missing, enables multi‑core processing, and reads the barcode information.
1620
/// </summary>
1721
static void Main()
1822
{
19-
// Define a temporary file path for the generated barcode image.
20-
string tempImagePath = Path.Combine(Path.GetTempPath(), "sample_barcode.png");
21-
22-
// Generate a simple Code128 barcode and save it to the temporary file.
23-
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890"))
24-
{
25-
generator.Save(tempImagePath, BarCodeImageFormat.Png);
26-
}
27-
28-
// Configure the barcode reader to utilize all CPU cores for faster recognition.
23+
// Enable utilization of all CPU cores for barcode recognition
2924
BarCodeReader.ProcessorSettings.UseAllCores = true;
3025

31-
// Read the barcode from the generated image file.
32-
using (var reader = new BarCodeReader(tempImagePath, DecodeType.AllSupportedTypes))
26+
// Path to the barcode image file
27+
string imagePath = "barcode.png";
28+
29+
// Generate a sample Code128 barcode image if it does not already exist
30+
if (!File.Exists(imagePath))
3331
{
34-
// Iterate through all detected barcodes and output their type and text.
35-
foreach (var result in reader.ReadBarCodes())
32+
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "123456789"))
3633
{
37-
Console.WriteLine($"Detected Type: {result.CodeTypeName}");
38-
Console.WriteLine($"Detected Text: {result.CodeText}");
34+
generator.Save(imagePath, BarCodeImageFormat.Png);
3935
}
4036
}
4137

42-
// Clean up the temporary image file if it still exists.
43-
if (File.Exists(tempImagePath))
38+
// Initialize the reader to decode all supported barcode types from the image
39+
using (var reader = new BarCodeReader(imagePath, DecodeType.AllSupportedTypes))
4440
{
45-
try
46-
{
47-
File.Delete(tempImagePath);
48-
}
49-
catch
41+
// Iterate through all detected barcodes and output their details
42+
foreach (var result in reader.ReadBarCodes())
5043
{
51-
// If deletion fails, ignore – the OS will eventually remove the file.
44+
Console.WriteLine($"Barcode Type: {result.CodeTypeName}");
45+
Console.WriteLine($"Barcode Text: {result.CodeText}");
46+
47+
var bounds = result.Region.Rectangle;
48+
Console.WriteLine($"Region - X:{bounds.X}, Y:{bounds.Y}, Width:{bounds.Width}, Height:{bounds.Height}");
5249
}
5350
}
5451
}
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,88 @@
1+
// Title: Background Worker Barcode Reader from Video Stream
2+
// Description: Demonstrates reading barcodes from simulated video frames using a BackgroundWorker and ProcessorSettings to control core usage.
3+
// 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.
4+
// Prompt: Create a background worker that reads barcodes from a video stream using ProcessorSettings for optimal core usage.
5+
// Tags: code128, read, console, barcodegenerator, barcodereader, processorsettings, qualitysettings
6+
17
using System;
28
using System.Collections.Generic;
9+
using System.ComponentModel;
310
using System.IO;
4-
using System.Threading.Tasks;
11+
using System.Threading;
512
using Aspose.BarCode.Generation;
613
using Aspose.BarCode.BarCodeRecognition;
714
using Aspose.Drawing;
15+
using Aspose.Drawing.Imaging;
816

917
/// <summary>
10-
/// Demonstrates generating barcode images, treating them as video frames,
11-
/// and processing each frame to recognize barcodes using Aspose.BarCode.
18+
/// Example program that generates barcode images, simulates video frames,
19+
/// and reads them in a background worker using Aspose.BarCode APIs.
1220
/// </summary>
1321
class Program
1422
{
1523
/// <summary>
16-
/// Entry point of the application.
17-
/// Configures processor settings, generates sample frames, and processes them asynchronously.
24+
/// Entry point. Generates sample frames, configures processor settings,
25+
/// and processes frames asynchronously.
1826
/// </summary>
1927
static void Main()
2028
{
21-
// Use all available CPU cores for barcode processing (optimal performance)
22-
BarCodeReader.ProcessorSettings.UseOnlyThisCoresCount = Environment.ProcessorCount;
23-
24-
// Generate a small collection of barcode images that simulate video frames
25-
List<MemoryStream> frameStreams = GenerateSampleFrames(5);
26-
27-
// Run the frame processing on a background task (simulating a background worker)
28-
Task processingTask = Task.Run(() => ProcessFrames(frameStreams));
29-
30-
// Wait for the background task to complete before exiting the program
31-
processingTask.Wait();
32-
}
33-
34-
/// <summary>
35-
/// Generates the specified number of barcode images and returns them as memory streams.
36-
/// </summary>
37-
/// <param name="count">Number of barcode frames to generate.</param>
38-
/// <returns>List of memory streams containing PNG barcode images.</returns>
39-
private static List<MemoryStream> GenerateSampleFrames(int count)
40-
{
41-
var frames = new List<MemoryStream>();
42-
43-
for (int i = 0; i < count; i++)
29+
// Generate a few barcode images to simulate video frames
30+
var frames = new List<byte[]>();
31+
for (int i = 0; i < 3; i++)
4432
{
45-
// Create a unique code text for each frame (e.g., FRAME01, FRAME02, ...)
46-
string codeText = $"FRAME{i + 1:D2}";
47-
48-
// Initialize a barcode generator for Code128 with the specified text
49-
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, codeText))
33+
// Create a barcode generator for Code128 with unique text
34+
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, $"Sample{i + 1}"))
5035
{
51-
// Set a consistent image size for all generated barcodes
52-
generator.Parameters.ImageWidth.Point = 300f;
53-
generator.Parameters.ImageHeight.Point = 150f;
54-
55-
// Save the generated barcode image to a memory stream in PNG format
56-
var ms = new MemoryStream();
57-
generator.Save(ms, BarCodeImageFormat.Png);
58-
ms.Position = 0; // Reset stream position for subsequent reading
59-
60-
// Add the memory stream to the collection of frames
61-
frames.Add(ms);
36+
// Set a simple visual dimension
37+
generator.Parameters.Barcode.XDimension.Point = 2f;
38+
// Render the barcode to a bitmap
39+
using (var bitmap = generator.GenerateBarCodeImage())
40+
{
41+
// Save bitmap to memory stream as PNG
42+
using (var ms = new MemoryStream())
43+
{
44+
bitmap.Save(ms, ImageFormat.Png);
45+
frames.Add(ms.ToArray());
46+
}
47+
}
6248
}
6349
}
6450

65-
return frames;
66-
}
51+
// Synchronization primitive to wait for background work completion
52+
var doneEvent = new ManualResetEventSlim(false);
6753

68-
/// <summary>
69-
/// Processes each barcode frame, reads any barcodes present, and writes the results to the console.
70-
/// </summary>
71-
/// <param name="frameStreams">List of memory streams representing barcode frames.</param>
72-
private static void ProcessFrames(List<MemoryStream> frameStreams)
73-
{
74-
int frameIndex = 0;
75-
76-
// Iterate over each frame stream
77-
foreach (var stream in frameStreams)
54+
// BackgroundWorker that processes the simulated video frames
55+
var worker = new BackgroundWorker();
56+
worker.DoWork += (sender, args) =>
7857
{
79-
frameIndex++;
58+
// Configure processor settings for optimal core usage
59+
BarCodeReader.ProcessorSettings.UseAllCores = false;
60+
BarCodeReader.ProcessorSettings.UseOnlyThisCoresCount = Math.Max(1, Environment.ProcessorCount / 2);
8061

81-
// Load the image from the memory stream
82-
using (var bitmap = new Bitmap(stream))
62+
// Iterate over each simulated frame
63+
foreach (var frameData in frames)
8364
{
84-
// Initialize a barcode reader that supports all barcode types
85-
using (var reader = new BarCodeReader(bitmap, DecodeType.AllSupportedTypes))
65+
// Create a memory stream from the frame bytes
66+
using (var stream = new MemoryStream(frameData))
67+
// Initialize the barcode reader for all supported symbologies
68+
using (var reader = new BarCodeReader(stream, DecodeType.AllSupportedTypes))
8669
{
87-
// Apply a high-performance quality preset for faster processing
70+
// Apply a highperformance quality preset
8871
reader.QualitySettings = QualitySettings.HighPerformance;
8972

90-
// Read all barcodes found in the current frame
73+
// Read and output all detected barcodes
9174
foreach (var result in reader.ReadBarCodes())
9275
{
93-
// Output the detected barcode type and its text value
94-
Console.WriteLine($"Frame {frameIndex}: Detected {result.CodeTypeName} - Text: {result.CodeText}");
76+
Console.WriteLine($"Detected: {result.CodeTypeName} - {result.CodeText}");
9577
}
9678
}
9779
}
80+
};
81+
// Signal completion when background work finishes
82+
worker.RunWorkerCompleted += (s, e) => doneEvent.Set();
9883

99-
// Release the memory stream resources after processing the frame
100-
stream.Dispose();
101-
}
84+
// Start processing and wait until it finishes
85+
worker.RunWorkerAsync();
86+
doneEvent.Wait();
10287
}
10388
}

0 commit comments

Comments
 (0)