|
| 1 | +// Title: Custom Background Color for MaxiCode Barcode |
| 2 | +// Description: Demonstrates applying a custom background color to a MaxiCode barcode and confirming that it can still be decoded correctly. |
| 3 | +// 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. |
| 4 | +// Prompt: Apply a custom background color to a MaxiCode barcode and verify that decoding remains successful. |
| 5 | +// Tags: maxicode, background color, barcode generation, barcode recognition, aspose.barcode, c# |
| 6 | + |
1 | 7 | using System; |
2 | 8 | using System.IO; |
3 | | -using Aspose.BarCode; |
4 | 9 | using Aspose.BarCode.Generation; |
5 | 10 | using Aspose.BarCode.BarCodeRecognition; |
6 | 11 | using Aspose.BarCode.ComplexBarcode; |
7 | 12 | using Aspose.Drawing; |
8 | 13 |
|
9 | 14 | /// <summary> |
10 | | -/// Demonstrates generation and decoding of a MaxiCode barcode using Aspose.BarCode. |
| 15 | +/// Generates a MaxiCode barcode with a custom background color, |
| 16 | +/// saves it as an image, and verifies that the barcode can be decoded successfully. |
11 | 17 | /// </summary> |
12 | 18 | class Program |
13 | 19 | { |
14 | 20 | /// <summary> |
15 | | - /// Entry point of the application. |
16 | | - /// Generates a MaxiCode (Mode 2) with a custom background, saves it to a memory stream, |
17 | | - /// and then reads/decodes the barcode from that stream. |
| 21 | + /// Entry point of the example. Creates a MaxiCode with a light‑yellow background, |
| 22 | + /// writes it to a PNG file, and then reads the file back to confirm decoding. |
18 | 23 | /// </summary> |
19 | 24 | static void Main() |
20 | 25 | { |
21 | | - // ------------------------------------------------------------ |
22 | | - // Prepare MaxiCode codetext (Mode 2 with a standard second message) |
23 | | - // ------------------------------------------------------------ |
24 | | - var maxiCodeCodetext = new MaxiCodeCodetextMode2 |
| 26 | + // Define the output file name. |
| 27 | + string outputPath = "maxicode.png"; |
| 28 | + |
| 29 | + // Prepare MaxiCode codetext (Mode 2 example) with postal code, country code, and service category. |
| 30 | + var maxiCode = new MaxiCodeCodetextMode2 |
25 | 31 | { |
26 | | - PostalCode = "524032140", // 9‑digit US postal code |
27 | | - CountryCode = 56, // USA |
28 | | - ServiceCategory = 999 |
| 32 | + PostalCode = "524032140", // 9‑digit US postal code |
| 33 | + CountryCode = 56, // USA |
| 34 | + ServiceCategory = 999 // Example service category |
29 | 35 | }; |
30 | 36 |
|
31 | | - // Create the optional second message for the MaxiCode |
| 37 | + // Add a second message to the MaxiCode. |
32 | 38 | var secondMessage = new MaxiCodeStandardSecondMessage |
33 | 39 | { |
34 | 40 | Message = "Sample MaxiCode" |
35 | 41 | }; |
36 | | - maxiCodeCodetext.SecondMessage = secondMessage; |
| 42 | + maxiCode.SecondMessage = secondMessage; |
| 43 | + |
| 44 | + // Generate the MaxiCode barcode with a custom background color. |
| 45 | + using (var complexGenerator = new ComplexBarcodeGenerator(maxiCode)) |
| 46 | + { |
| 47 | + // Set background to light yellow (RGB 255,255,224). |
| 48 | + complexGenerator.Parameters.BackColor = Aspose.Drawing.Color.FromArgb(255, 255, 224); |
| 49 | + |
| 50 | + // Create the barcode image. |
| 51 | + using (var bitmap = complexGenerator.GenerateBarCodeImage()) |
| 52 | + { |
| 53 | + // Save the image as PNG. |
| 54 | + bitmap.Save(outputPath, Aspose.Drawing.Imaging.ImageFormat.Png); |
| 55 | + } |
| 56 | + } |
37 | 57 |
|
38 | | - // ------------------------------------------------------------ |
39 | | - // Generate the barcode with a custom background color |
40 | | - // ------------------------------------------------------------ |
41 | | - using (var generator = new ComplexBarcodeGenerator(maxiCodeCodetext)) |
| 58 | + // Verify that the image file was created. |
| 59 | + if (!File.Exists(outputPath)) |
42 | 60 | { |
43 | | - // Set a custom background (e.g., light yellow) |
44 | | - generator.Parameters.BackColor = Color.FromArgb(255, 255, 224); // LightYellow |
| 61 | + Console.WriteLine("Failed to create barcode image."); |
| 62 | + return; |
| 63 | + } |
45 | 64 |
|
46 | | - // -------------------------------------------------------- |
47 | | - // Save the generated barcode to a memory stream in PNG format |
48 | | - // -------------------------------------------------------- |
49 | | - using (var ms = new MemoryStream()) |
| 65 | + // Read and decode the generated MaxiCode barcode. |
| 66 | + using (var reader = new BarCodeReader(outputPath, DecodeType.MaxiCode)) |
| 67 | + { |
| 68 | + foreach (var result in reader.ReadBarCodes()) |
50 | 69 | { |
51 | | - generator.Save(ms, BarCodeImageFormat.Png); |
52 | | - ms.Position = 0; // Reset stream position for reading |
| 70 | + // Decode the raw codetext using the appropriate MaxiCode mode. |
| 71 | + var decoded = ComplexCodetextReader.TryDecodeMaxiCode( |
| 72 | + result.Extended.MaxiCode.MaxiCodeMode, |
| 73 | + result.CodeText); |
53 | 74 |
|
54 | | - // -------------------------------------------------------- |
55 | | - // Read and decode the barcode from the generated image stream |
56 | | - // -------------------------------------------------------- |
57 | | - using (var reader = new BarCodeReader(ms, DecodeType.MaxiCode)) |
| 75 | + // Check if decoding produced the expected Mode 2 codetext. |
| 76 | + if (decoded is MaxiCodeCodetextMode2 decodedMode2) |
58 | 77 | { |
59 | | - var results = reader.ReadBarCodes(); |
| 78 | + Console.WriteLine("Decoding successful:"); |
| 79 | + Console.WriteLine($"Postal Code: {decodedMode2.PostalCode}"); |
| 80 | + Console.WriteLine($"Country Code: {decodedMode2.CountryCode}"); |
| 81 | + Console.WriteLine($"Service Category: {decodedMode2.ServiceCategory}"); |
60 | 82 |
|
61 | | - // Check if any barcodes were detected |
62 | | - if (results.Length == 0) |
| 83 | + // Output the second message if present. |
| 84 | + if (decodedMode2.SecondMessage is MaxiCodeStandardSecondMessage stdMsg) |
63 | 85 | { |
64 | | - Console.WriteLine("No barcode detected."); |
65 | | - } |
66 | | - else |
67 | | - { |
68 | | - // Iterate through all detected barcodes |
69 | | - foreach (var result in results) |
70 | | - { |
71 | | - // Verify that decoding succeeded (non‑empty CodeText) |
72 | | - if (!string.IsNullOrEmpty(result.CodeText)) |
73 | | - { |
74 | | - Console.WriteLine("Decoding successful."); |
75 | | - Console.WriteLine("Decoded CodeText: " + result.CodeText); |
76 | | - } |
77 | | - else |
78 | | - { |
79 | | - Console.WriteLine("Decoding failed: empty CodeText."); |
80 | | - } |
81 | | - } |
| 86 | + Console.WriteLine($"Message: {stdMsg.Message}"); |
82 | 87 | } |
83 | 88 | } |
| 89 | + else |
| 90 | + { |
| 91 | + Console.WriteLine("Decoding failed or unexpected codetext type."); |
| 92 | + } |
84 | 93 | } |
85 | 94 | } |
86 | 95 | } |
|
0 commit comments