Skip to content
Closed
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,93 +1,83 @@
// Title: Generate GS1 Composite barcode with configurable linear component
// Description: Demonstrates reading a linear component type from a configuration file and creating a GS1 Composite barcode using Aspose.BarCode.
// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, focusing on GS1 Composite symbology. It showcases the use of BarcodeGenerator, EncodeTypes, and GS1CompositeBar parameters to customize linear and 2D components. Developers often need to dynamically select symbologies based on configuration or user input, and this snippet illustrates that pattern.
// Prompt: Allow users to select linear component type via configuration file and generate corresponding GS1 Composite barcode.
// Tags: barcode symbology, gs1 composite, configuration, aspose.barcode, generation, png output

using System;
using System.IO;
using System.Reflection;
using Aspose.BarCode;
using Aspose.BarCode.Generation;

/// <summary>
/// Demonstrates generating a GS1 Composite barcode using Aspose.BarCode,
/// with the linear component type configurable via a text file.
/// Example program that reads a linear component type from a configuration file
/// and generates a GS1 Composite barcode using Aspose.BarCode.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application.
/// Reads configuration, resolves the linear component type, and generates a GS1 Composite barcode image.
/// Application entry point. Generates a GS1 Composite barcode based on the
/// linear component type specified in a configuration file.
/// </summary>
static void Main()
/// <param name="args">Command‑line arguments; the first argument can specify the config file path.</param>
static void Main(string[] args)
{
// --------------------------------------------------------------------
// Configuration: read the linear component type name from a file.
// --------------------------------------------------------------------
const string configPath = "config.txt"; // Path to the configuration file.
BaseEncodeType defaultLinearType = EncodeTypes.GS1Code128; // Fallback type if config is missing/invalid.
// Determine configuration file path (first argument or default)
string configPath = args.Length > 0 ? args[0] : "config.txt";

// Default linear component type if configuration is missing or invalid
BaseEncodeType linearComponent = EncodeTypes.GS1Code128;

string linearTypeName = null;
// Attempt to read the configuration file and resolve the symbology name
if (File.Exists(configPath))
{
try
{
// Read the entire file content and trim whitespace.
linearTypeName = File.ReadAllText(configPath).Trim();
string symbologyName = File.ReadAllText(configPath).Trim();

// Resolve symbology name to EncodeTypes field via reflection
FieldInfo field = typeof(EncodeTypes).GetField(symbologyName);
if (field != null && typeof(BaseEncodeType).IsAssignableFrom(field.FieldType))
{
linearComponent = (BaseEncodeType)field.GetValue(null);
}
else
{
Console.WriteLine($"Warning: Unknown symbology '{symbologyName}'. Using default GS1Code128.");
}
}
catch (Exception ex)
{
// Report any I/O errors but continue with the default type.
Console.WriteLine($"Error reading config file: {ex.Message}");
Console.WriteLine($"Error reading configuration: {ex.Message}. Using default GS1Code128.");
}
}
else
{
// Inform the user that the config file was not found.
Console.WriteLine("Config file not found. Using default linear component type.");
Console.WriteLine($"Configuration file '{configPath}' not found. Using default GS1Code128.");
}

// --------------------------------------------------------------------
// Resolve the type name to a BaseEncodeType using reflection.
// --------------------------------------------------------------------
BaseEncodeType linearComponentType = defaultLinearType;
if (!string.IsNullOrEmpty(linearTypeName))
{
var field = typeof(EncodeTypes).GetField(linearTypeName);
if (field != null && typeof(BaseEncodeType).IsAssignableFrom(field.FieldType))
{
// Successful resolution – assign the configured type.
linearComponentType = (BaseEncodeType)field.GetValue(null);
}
else
{
// Unknown or unsupported type – fall back to default.
Console.WriteLine($"Unknown or unsupported linear component type '{linearTypeName}'. Using default.");
}
}
// Sample GS1 Composite barcode text (1D and 2D parts separated by '|')
string codeText = "(01)03212345678906|(21)A1B2C3D4E5F6G7H8";

// --------------------------------------------------------------------
// Barcode data and output settings.
// --------------------------------------------------------------------
const string codeText = "(01)03212345678906|(21)A12345678"; // GS1 Composite codetext (1D|2D parts).
const string outputPath = "gs1composite.png"; // Destination image file.

// --------------------------------------------------------------------
// Generate the barcode using Aspose.BarCode.
// --------------------------------------------------------------------
// Generate the barcode using the GS1 Composite symbology
using (var generator = new BarcodeGenerator(EncodeTypes.GS1CompositeBar, codeText))
{
// Apply the linear component type resolved from configuration.
generator.Parameters.Barcode.GS1CompositeBar.LinearComponentType = linearComponentType;
// Apply the linear component type read from configuration
generator.Parameters.Barcode.GS1CompositeBar.LinearComponentType = linearComponent;

// Choose a 2D component type (CC_A is a common choice for GS1 Composite).
// Choose a 2D component type (CC_A is a common choice)
generator.Parameters.Barcode.GS1CompositeBar.TwoDComponentType = TwoDComponentType.CC_A;

// Optional: adjust visual dimensions.
generator.Parameters.Barcode.XDimension.Pixels = 3f; // Width of a single module.
generator.Parameters.Barcode.BarHeight.Pixels = 100f; // Height of the linear component.
// Example visual settings
generator.Parameters.Barcode.Pdf417.AspectRatio = 3f;
generator.Parameters.Barcode.XDimension.Pixels = 3f;
generator.Parameters.Barcode.BarHeight.Pixels = 100f;

// Save the generated barcode image to the specified path.
// Save the barcode image to a PNG file
string outputPath = "gs1composite.png";
generator.Save(outputPath);
Console.WriteLine($"GS1 Composite barcode saved to '{outputPath}'.");
}

// --------------------------------------------------------------------
// Inform the user of successful generation.
// --------------------------------------------------------------------
Console.WriteLine($"GS1 Composite barcode generated with linear component '{linearComponentType.GetType().Name}'. Saved to '{outputPath}'.");
}
}
Original file line number Diff line number Diff line change
@@ -1,108 +1,92 @@
// Title: Benchmark DotCode barcode generation speed across encoding modes in parallel
// Description: Demonstrates measuring the time required to generate DotCode barcodes using various encoding modes, running each mode concurrently.
// Category-Description: This example belongs to the Aspose.BarCode generation performance category. It showcases the use of BarcodeGenerator, EncodeTypes, and DotCodeEncodeMode to create DotCode symbols. Developers often need to benchmark different encoding settings to choose the optimal configuration for high‑throughput applications, such as bulk label printing or real‑time scanning systems.
// Prompt: Benchmark generation speed of DotCode barcodes using different encoding modes in parallel.
// Tags: dotcode, benchmark, parallel, generation, aspnet, aspose.barcode, png

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

/// <summary>
/// Demonstrates benchmarking of different DotCode encoding modes using Aspose.BarCode.
/// Generates a small number of barcodes per mode and measures execution time.
/// Provides a benchmark for generating DotCode barcodes using various encoding modes in parallel.
/// </summary>
class Program
{
// Sample text to encode in each DotCode barcode.
private const string SampleCodeText = "DOTCODE123";

// Number of barcodes generated per encoding mode (kept low for quick execution).
private const int BarcodesPerMode = 5;

/// <summary>
/// Entry point of the application. Sets up encoding modes and runs benchmarks in parallel.
/// Entry point of the benchmark application.
/// </summary>
static void Main()
{
// Mapping of mode names to configuration actions for the BarcodeGenerator.
var modes = new Dictionary<string, Action<BarcodeGenerator>>
// Define the DotCode encoding modes to benchmark.
var modes = new Dictionary<string, DotCodeEncodeMode>
{
{
"Auto", generator =>
{
// Default mode; no additional configuration required.
}
},
{
"Binary", generator =>
{
// Configure generator for Binary encoding.
generator.Parameters.Barcode.DotCode.DotCodeEncodeMode = DotCodeEncodeMode.Binary;
}
},
{
"ECI", generator =>
{
// Configure generator for ECI encoding with UTF-8 character set.
generator.Parameters.Barcode.DotCode.DotCodeEncodeMode = DotCodeEncodeMode.ECI;
generator.Parameters.Barcode.DotCode.ECIEncoding = ECIEncodings.UTF8;
}
},
{
"Extended", generator =>
{
// Configure generator for Extended encoding.
generator.Parameters.Barcode.DotCode.DotCodeEncodeMode = DotCodeEncodeMode.Extended;
}
}
{ "Auto", DotCodeEncodeMode.Auto },
{ "Binary", DotCodeEncodeMode.Binary },
{ "ECI", DotCodeEncodeMode.ECI },
{ "Extended", DotCodeEncodeMode.Extended },
{ "ExtendedCodetext", DotCodeEncodeMode.ExtendedCodetext }
};

// Launch a benchmark task for each mode concurrently.
// List to store benchmark results (mode name and elapsed time in milliseconds).
var results = new List<(string Mode, long ElapsedMs)>();
// Collection of tasks that will run each mode concurrently.
var tasks = new List<Task>();

// Create a task for each encoding mode.
foreach (var kvp in modes)
{
string modeName = kvp.Key;
Action<BarcodeGenerator> configure = kvp.Value;
DotCodeEncodeMode mode = kvp.Value;

tasks.Add(Task.Run(() => BenchmarkMode(modeName, configure)));
}
tasks.Add(Task.Run(() =>
{
var stopwatch = Stopwatch.StartNew();

// Wait for all benchmark tasks to complete.
Task.WaitAll(tasks.ToArray());
// Generate a small number of barcodes for the current mode.
for (int i = 0; i < 5; i++)
{
using (var generator = new BarcodeGenerator(EncodeTypes.DotCode, "Sample"))
{
// Apply the specific DotCode encoding mode.
generator.Parameters.Barcode.DotCode.DotCodeEncodeMode = mode;

Console.WriteLine("Benchmark completed.");
}
// Set ECI encoding when the mode requires it.
if (mode == DotCodeEncodeMode.ECI)
{
generator.Parameters.Barcode.DotCode.ECIEncoding = ECIEncodings.UTF8;
}

/// <summary>
/// Generates a set of barcodes for a specific encoding mode and measures the time taken.
/// </summary>
/// <param name="modeName">Human‑readable name of the encoding mode.</param>
/// <param name="configure">Action that applies mode‑specific settings to a BarcodeGenerator instance.</param>
private static void BenchmarkMode(string modeName, Action<BarcodeGenerator> configure)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
// Save the barcode image to a memory stream (no file I/O).
using (var ms = new MemoryStream())
{
generator.Save(ms, BarCodeImageFormat.Png);
}
}
}

// Generate the defined number of barcodes for the given mode.
for (int i = 0; i < BarcodesPerMode; i++)
{
// Create a fresh generator for each barcode to avoid residual state.
using (var generator = new BarcodeGenerator(EncodeTypes.DotCode, SampleCodeText))
{
// Apply the mode‑specific configuration.
configure(generator);
stopwatch.Stop();

// Save the barcode image to a memory stream (no disk I/O required for benchmarking).
using (var ms = new MemoryStream())
// Record the elapsed time for this mode in a thread‑safe manner.
lock (results)
{
generator.Save(ms, BarCodeImageFormat.Png);
// The memory stream is discarded after this point; it could be used for further processing if needed.
results.Add((modeName, stopwatch.ElapsedMilliseconds));
}
}
}));
}

stopwatch.Stop();
Console.WriteLine($"{modeName} mode: Generated {BarcodesPerMode} barcodes in {stopwatch.ElapsedMilliseconds} ms");
// Wait for all parallel tasks to finish.
Task.WaitAll(tasks.ToArray());

// Output the benchmark results to the console.
Console.WriteLine("DotCode generation benchmark (5 barcodes per mode):");
foreach (var result in results)
{
Console.WriteLine($"{result.Mode}: {result.ElapsedMs} ms");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
// Title: Generate GS1 Composite barcode with CC_C component and custom column count
// Description: Demonstrates configuring the PDF417 (CC_C) component of a GS1 Composite barcode to use 30 columns, then saving the image.
// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, focusing on GS1 Composite symbology. It shows how to set linear and 2D component types, adjust PDF417 parameters, and customize common barcode settings. Developers creating composite barcodes for packaging, logistics, or retail can use these APIs to meet specification requirements.
// Prompt: Configure column count for CC_C 2D component to 30 columns when generating a GS1 Composite barcode.
// Tags: gs1 composite, pdf417, column count, barcode generation, aspose.barcode, csharp

using System;
using Aspose.BarCode;
using Aspose.BarCode.Generation;

/// <summary>
/// Demonstrates generation of a GS1 Composite barcode with linear and 2D components using Aspose.BarCode.
/// Demonstrates generating a GS1 Composite barcode with a CC_C (PDF417) 2D component
/// configured to 30 columns, and saving it as an image file.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application. Generates and saves a GS1 Composite barcode image.
/// Entry point of the example. Creates a BarcodeGenerator, configures parameters,
/// and saves the resulting barcode image.
/// </summary>
static void Main()
{
// Define the GS1 Composite barcode text.
// Linear part (GS1-128) and 2D part (PDF417) are separated by '|'.
// Define the GS1 Composite code text: linear part and 2D part separated by '|'
string codetext = "(01)03212345678906|(21)A12345678";

// Initialize the barcode generator for GS1 Composite Bar with the specified code text.
// Initialize the generator for GS1 Composite barcode with the specified code text
using (var generator = new BarcodeGenerator(EncodeTypes.GS1CompositeBar, codetext))
{
// Specify the linear component type (GS1 Code 128).
// Set the linear component to GS1 Code128
generator.Parameters.Barcode.GS1CompositeBar.LinearComponentType = EncodeTypes.GS1Code128;

// Specify the 2D component type (CC_C, which uses PDF417).
// Set the 2D component type to CC_C (PDF417)
generator.Parameters.Barcode.GS1CompositeBar.TwoDComponentType = TwoDComponentType.CC_C;

// Set the number of columns for the PDF417 component to 30.
// Configure PDF417 to use 30 columns, as required for the CC_C component
generator.Parameters.Barcode.Pdf417.Columns = 30;

// Save the generated barcode image to a PNG file.
generator.Save("gs1_composite_ccc.png");
}
// Optional: improve visual quality by adjusting size and resolution
generator.Parameters.Barcode.XDimension.Pixels = 2f; // module width
generator.Parameters.Barcode.BarHeight.Pixels = 100f; // linear component height
generator.Parameters.Resolution = 96; // image DPI

// Inform the user that the barcode has been generated.
Console.WriteLine("Barcode generated and saved as gs1_composite_ccc.png");
// Save the generated barcode to a PNG file
string outputPath = "gs1_composite_cc_c.png";
generator.Save(outputPath);
Console.WriteLine($"Barcode saved to {outputPath}");
}
}
}
Loading
Loading