-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-barcodereader-to-read-barcode-from-byte-array-and-ensure-detectencoding-correctly-decodes-utf16-content.cs
More file actions
77 lines (67 loc) · 3.03 KB
/
Copy pathuse-barcodereader-to-read-barcode-from-byte-array-and-ensure-detectencoding-correctly-decodes-utf16-content.cs
File metadata and controls
77 lines (67 loc) · 3.03 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
using System;
using System.IO;
using System.Text;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.BarCode.BarCodeRecognition;
/// <summary>
/// Demonstrates generating a QR barcode with Unicode text and reading it back,
/// verifying that encoding detection works correctly.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application.
/// Generates a QR code from a Unicode string, reads it back, and validates the decoded text.
/// </summary>
static void Main()
{
// Sample text containing Unicode characters (Russian phrase "Hello, world!").
const string originalText = "Привет, мир!";
// Byte array that will hold the generated barcode image.
byte[] barcodeBytes;
// Generate the QR barcode and store it in a memory stream.
using (var ms = new MemoryStream())
{
// Create a barcode generator for QR codes.
using (var generator = new BarcodeGenerator(EncodeTypes.QR))
{
// Encode the text using UTF-16 Big Endian (includes BOM) to preserve Unicode characters.
generator.SetCodeText(originalText, Encoding.BigEndianUnicode);
// Save the generated barcode as a PNG image into the memory stream.
generator.Save(ms, BarCodeImageFormat.Png);
}
// Convert the memory stream contents to a byte array for later processing.
barcodeBytes = ms.ToArray();
}
// Read and decode the barcode from the previously generated byte array.
using (var imageStream = new MemoryStream(barcodeBytes))
{
// Ensure the stream is positioned at the beginning before reading.
imageStream.Position = 0;
// Initialize a barcode reader that supports all barcode types.
using (var reader = new BarCodeReader(imageStream, DecodeType.AllSupportedTypes))
{
// Explicitly enable encoding detection (true by default) for clarity.
reader.BarcodeSettings.DetectEncoding = true;
// Iterate over all detected barcodes in the image.
foreach (var result in reader.ReadBarCodes())
{
// Output the type of barcode that was detected.
Console.WriteLine($"Detected Type: {result.CodeTypeName}");
// Output the decoded text from the barcode.
Console.WriteLine($"Decoded Text : {result.CodeText}");
// Verify that the decoded text matches the original Unicode string.
if (result.CodeText == originalText)
{
Console.WriteLine("Encoding detection succeeded: decoded text matches original.");
}
else
{
Console.WriteLine("Encoding detection failed: decoded text does not match original.");
}
}
}
}
}
}