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,86 +1,95 @@
// Title: Custom Background Color for MaxiCode Barcode
// Description: Demonstrates applying a custom background color to a MaxiCode barcode and confirming that it can still be decoded correctly.
// Category-Description: This example belongs to the Aspose.BarCode generation and recognition category, focusing on MaxiCode symbology. It showcases the use of ComplexBarcodeGenerator to create a MaxiCode with custom visual settings, and BarCodeReader with ComplexCodetextReader to decode the generated image. Developers working with shipping, logistics, or inventory systems often need to customize barcode appearance while ensuring reliable scanning.
// Prompt: Apply a custom background color to a MaxiCode barcode and verify that decoding remains successful.
// Tags: maxicode, background color, barcode generation, barcode recognition, aspose.barcode, c#

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

/// <summary>
/// Demonstrates generation and decoding of a MaxiCode barcode using Aspose.BarCode.
/// Generates a MaxiCode barcode with a custom background color,
/// saves it as an image, and verifies that the barcode can be decoded successfully.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application.
/// Generates a MaxiCode (Mode 2) with a custom background, saves it to a memory stream,
/// and then reads/decodes the barcode from that stream.
/// Entry point of the example. Creates a MaxiCode with a light‑yellow background,
/// writes it to a PNG file, and then reads the file back to confirm decoding.
/// </summary>
static void Main()
{
// ------------------------------------------------------------
// Prepare MaxiCode codetext (Mode 2 with a standard second message)
// ------------------------------------------------------------
var maxiCodeCodetext = new MaxiCodeCodetextMode2
// Define the output file name.
string outputPath = "maxicode.png";

// Prepare MaxiCode codetext (Mode 2 example) with postal code, country code, and service category.
var maxiCode = new MaxiCodeCodetextMode2
{
PostalCode = "524032140", // 9‑digit US postal code
CountryCode = 56, // USA
ServiceCategory = 999
PostalCode = "524032140", // 9‑digit US postal code
CountryCode = 56, // USA
ServiceCategory = 999 // Example service category
};

// Create the optional second message for the MaxiCode
// Add a second message to the MaxiCode.
var secondMessage = new MaxiCodeStandardSecondMessage
{
Message = "Sample MaxiCode"
};
maxiCodeCodetext.SecondMessage = secondMessage;
maxiCode.SecondMessage = secondMessage;

// Generate the MaxiCode barcode with a custom background color.
using (var complexGenerator = new ComplexBarcodeGenerator(maxiCode))
{
// Set background to light yellow (RGB 255,255,224).
complexGenerator.Parameters.BackColor = Aspose.Drawing.Color.FromArgb(255, 255, 224);

// Create the barcode image.
using (var bitmap = complexGenerator.GenerateBarCodeImage())
{
// Save the image as PNG.
bitmap.Save(outputPath, Aspose.Drawing.Imaging.ImageFormat.Png);
}
}

// ------------------------------------------------------------
// Generate the barcode with a custom background color
// ------------------------------------------------------------
using (var generator = new ComplexBarcodeGenerator(maxiCodeCodetext))
// Verify that the image file was created.
if (!File.Exists(outputPath))
{
// Set a custom background (e.g., light yellow)
generator.Parameters.BackColor = Color.FromArgb(255, 255, 224); // LightYellow
Console.WriteLine("Failed to create barcode image.");
return;
}

// --------------------------------------------------------
// Save the generated barcode to a memory stream in PNG format
// --------------------------------------------------------
using (var ms = new MemoryStream())
// Read and decode the generated MaxiCode barcode.
using (var reader = new BarCodeReader(outputPath, DecodeType.MaxiCode))
{
foreach (var result in reader.ReadBarCodes())
{
generator.Save(ms, BarCodeImageFormat.Png);
ms.Position = 0; // Reset stream position for reading
// Decode the raw codetext using the appropriate MaxiCode mode.
var decoded = ComplexCodetextReader.TryDecodeMaxiCode(
result.Extended.MaxiCode.MaxiCodeMode,
result.CodeText);

// --------------------------------------------------------
// Read and decode the barcode from the generated image stream
// --------------------------------------------------------
using (var reader = new BarCodeReader(ms, DecodeType.MaxiCode))
// Check if decoding produced the expected Mode 2 codetext.
if (decoded is MaxiCodeCodetextMode2 decodedMode2)
{
var results = reader.ReadBarCodes();
Console.WriteLine("Decoding successful:");
Console.WriteLine($"Postal Code: {decodedMode2.PostalCode}");
Console.WriteLine($"Country Code: {decodedMode2.CountryCode}");
Console.WriteLine($"Service Category: {decodedMode2.ServiceCategory}");

// Check if any barcodes were detected
if (results.Length == 0)
// Output the second message if present.
if (decodedMode2.SecondMessage is MaxiCodeStandardSecondMessage stdMsg)
{
Console.WriteLine("No barcode detected.");
}
else
{
// Iterate through all detected barcodes
foreach (var result in results)
{
// Verify that decoding succeeded (non‑empty CodeText)
if (!string.IsNullOrEmpty(result.CodeText))
{
Console.WriteLine("Decoding successful.");
Console.WriteLine("Decoded CodeText: " + result.CodeText);
}
else
{
Console.WriteLine("Decoding failed: empty CodeText.");
}
}
Console.WriteLine($"Message: {stdMsg.Message}");
}
}
else
{
Console.WriteLine("Decoding failed or unexpected codetext type.");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,57 @@
// Title: Apply custom foreground color to a MaxiCode Mode 2 barcode
// Description: This example creates a MaxiCode Mode 2 barcode, sets a custom bar (foreground) color, and saves it as a PNG image. It demonstrates how to customize the visual appearance of complex barcodes using Aspose.BarCode.
// Category-Description: The sample belongs to the Aspose.BarCode complex barcode generation category, where developers work with multi‑message symbologies such as MaxiCode. It showcases the use of ComplexBarcodeGenerator, MaxiCodeCodetextMode2, and related parameter classes to configure barcode data and appearance. Typical scenarios include shipping labels, parcel tracking, and logistics applications that require colored MaxiCode symbols.
// Prompt: Apply a custom foreground color to a MaxiCode Mode 2 barcode using the generator's ForeColor property.
// Tags: maxicode, color, generation, png, aspose.barcode, complexbarcodegenerator, barcode

using System;
using System.IO;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.BarCode.ComplexBarcode;
using Aspose.Drawing;
using Aspose.Drawing.Imaging;

/// <summary>
/// Demonstrates generation of a MaxiCode Mode 2 barcode using Aspose.BarCode.
/// Demonstrates applying a custom foreground color to a MaxiCode Mode 2 barcode and saving it as PNG.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application. Creates a MaxiCode codetext, configures the generator,
/// and saves the resulting barcode image to a file.
/// Entry point that builds the MaxiCode data, configures the barcode color, generates the image, and writes it to disk.
/// </summary>
static void Main()
{
// Initialize MaxiCode codetext for Mode 2 with required fields.
var maxiCode = new MaxiCodeCodetextMode2
// Prepare MaxiCode Mode 2 codetext with required fields
var maxiCodeCodetext = new MaxiCodeCodetextMode2
{
PostalCode = "524032140", // 9‑digit postal code
CountryCode = 56, // Numeric country identifier
ServiceCategory = 999 // Service category value
PostalCode = "524032140", // 9‑digit US postal code
CountryCode = 56, // USA numeric country code
ServiceCategory = 999 // Example service category
};

// Create the optional second message and assign it to the codetext.
// Create and assign the standard second message
var secondMessage = new MaxiCodeStandardSecondMessage
{
Message = "Test message"
Message = "Sample MaxiCode"
};
maxiCode.SecondMessage = secondMessage;
maxiCodeCodetext.SecondMessage = secondMessage;

// Use ComplexBarcodeGenerator to render the MaxiCode barcode.
using (var generator = new ComplexBarcodeGenerator(maxiCode))
// Initialize the generator with the prepared codetext
using (var generator = new ComplexBarcodeGenerator(maxiCodeCodetext))
{
// Set the color of the barcode bars (foreground). No ForeColor property exists.
generator.Parameters.Barcode.BarColor = Color.Red;
// Set a custom foreground (bar) color for the barcode
generator.Parameters.Barcode.BarColor = Color.Blue;

// Generate the barcode image in memory
generator.GenerateBarCodeImage();

// Define output file name and format, then save the image.
string outputPath = "maxicode_mode2.png";
// Define output file path and save the image as PNG
const string outputPath = "maxicode_mode2.png";
generator.Save(outputPath, BarCodeImageFormat.Png);

// Output the full path of the saved file for verification.
Console.WriteLine($"Barcode saved to: {Path.GetFullPath(outputPath)}");
// Inform the user where the file was saved
Console.WriteLine($"Barcode saved to {Path.GetFullPath(outputPath)}");
}
}
}
Loading
Loading