-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode-dutch-kix-barcode-from-byte-array-and-handle-potential-format-exceptions.cs
More file actions
95 lines (87 loc) · 3.53 KB
/
Copy pathdecode-dutch-kix-barcode-from-byte-array-and-handle-potential-format-exceptions.cs
File metadata and controls
95 lines (87 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.IO;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.BarCode.BarCodeRecognition;
/// <summary>
/// Demonstrates decoding a Dutch KIX barcode from a byte array using Aspose.BarCode.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application. Reads a barcode image from a byte array,
/// attempts to decode Dutch KIX barcodes, and outputs the results to the console.
/// </summary>
static void Main()
{
// Obtain a sample byte array containing a Dutch KIX barcode image.
// In production, replace this with actual image data.
byte[] barcodeImageBytes = GetSampleBarcodeImageBytes();
// Validate that image data was provided.
if (barcodeImageBytes == null || barcodeImageBytes.Length == 0)
{
Console.WriteLine("No barcode image data provided.");
return;
}
try
{
// Wrap the byte array in a memory stream for the reader.
using (var ms = new MemoryStream(barcodeImageBytes))
{
// Initialize the barcode reader for Dutch KIX type.
using (var reader = new BarCodeReader(ms, DecodeType.DutchKIX))
{
// Enable checksum validation (optional; set to Off if not required).
reader.BarcodeSettings.ChecksumValidation = ChecksumValidation.On;
// Perform the barcode recognition.
var results = reader.ReadBarCodes();
// Check if any barcodes were detected.
if (results.Length == 0)
{
Console.WriteLine("No Dutch KIX barcode detected.");
}
else
{
// Output each detected barcode's type and decoded text.
foreach (var result in results)
{
Console.WriteLine($"Barcode Type: {result.CodeTypeName}");
Console.WriteLine($"Decoded Text: {result.CodeText}");
}
}
}
}
}
catch (ArgumentException ex)
{
// Thrown when the byte array does not represent a valid image.
Console.WriteLine($"Invalid image data: {ex.Message}");
}
catch (Exception ex)
{
// General exception handling for unexpected errors.
Console.WriteLine($"Error during barcode decoding: {ex.Message}");
}
}
/// <summary>
/// Generates a sample Dutch KIX barcode image and returns its byte representation.
/// This helper method is for demonstration purposes only.
/// </summary>
/// <returns>Byte array containing a PNG image of a generated Dutch KIX barcode.</returns>
static byte[] GetSampleBarcodeImageBytes()
{
const string sampleCodeText = "1234567890123"; // Example KIX code text.
// Create a memory stream to hold the generated barcode image.
using (var ms = new MemoryStream())
{
// Generate the barcode using Aspose.BarCode.
using (var generator = new BarcodeGenerator(EncodeTypes.DutchKIX, sampleCodeText))
{
// Save the generated barcode to the memory stream in PNG format.
generator.Save(ms, BarCodeImageFormat.Png);
}
// Return the image bytes.
return ms.ToArray();
}
}
}