-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-that-generated-2-state-barcodes-contain-only-numeric-characters-by-inspecting-encoded-data.cs
More file actions
100 lines (90 loc) · 3.51 KB
/
Copy pathvalidate-that-generated-2-state-barcodes-contain-only-numeric-characters-by-inspecting-encoded-data.cs
File metadata and controls
100 lines (90 loc) · 3.51 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
96
97
98
99
100
using System;
using System.IO;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.BarCode.BarCodeRecognition;
/// <summary>
/// Demonstrates generation and validation of RM4SCC (2‑state) barcodes using Aspose.BarCode.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application. Generates sample barcodes, reads them back,
/// validates that the decoded text contains only numeric characters, and cleans up temporary files.
/// </summary>
static void Main()
{
// Sample numeric texts for 2‑state (RM4SCC) barcodes
string[] samples = { "1234567890", "987654321", "0011223344" };
// Use the system temporary directory for generated images
string tempDir = Path.GetTempPath();
// Process each sample text
foreach (var text in samples)
{
// Build a unique file name for the barcode image
string filePath = Path.Combine(tempDir, $"rm4scc_{text}.png");
// -------------------------------------------------
// Generate the barcode image and save it to disk
// -------------------------------------------------
using (var generator = new BarcodeGenerator(EncodeTypes.RM4SCC, text))
{
generator.Save(filePath);
}
// Verify that the image file was successfully created
if (!File.Exists(filePath))
{
Console.WriteLine($"Failed to create barcode for '{text}'.");
continue;
}
// -------------------------------------------------
// Read the barcode from the image and validate its content
// -------------------------------------------------
using (var reader = new BarCodeReader(filePath, DecodeType.RM4SCC))
{
var results = reader.ReadBarCodes();
// No barcode detected
if (results.Length == 0)
{
Console.WriteLine($"No barcode detected in image for '{text}'.");
}
else
{
// Iterate over all detected barcodes (should be one per image)
foreach (var result in results)
{
// Check if the decoded text consists only of digits
bool isNumeric = IsStringNumeric(result.CodeText);
Console.WriteLine($"Input '{text}' => Decoded '{result.CodeText}' is {(isNumeric ? "valid" : "invalid")}.");
}
}
}
// -------------------------------------------------
// Clean up the temporary barcode image file
// -------------------------------------------------
try
{
File.Delete(filePath);
}
catch
{
// Suppress any exceptions during cleanup
}
}
}
/// <summary>
/// Determines whether the specified string consists solely of numeric digits.
/// </summary>
/// <param name="s">The string to evaluate.</param>
/// <returns>True if the string is non‑empty and contains only digits; otherwise, false.</returns>
static bool IsStringNumeric(string s)
{
if (string.IsNullOrEmpty(s))
return false;
foreach (char c in s)
{
if (!char.IsDigit(c))
return false;
}
return true;
}
}