From cef52a216ec08adea6aa7ff97689bb54250181df Mon Sep 17 00:00:00 2001 From: Tony Peiffer Date: Wed, 23 Jul 2025 16:37:35 +0200 Subject: [PATCH 1/7] Adding support for Belgium id cards, Update to .net9.0 --- .gitignore | 1 + MRZCode.Samples/MRZCode.Samples.csproj | 2 +- .../MRZCodeParser.Tests.csproj | 2 +- MRZCodeParser.Tests/MrzSamples.cs | 4 ++ .../TD1DocumentLongMrzCodeTest.cs | 60 +++++++++++++++++++ MRZCodeParser.Tests/TD1MrzCodeLinesTest.cs | 2 +- MRZCodeParser/CodeType.cs | 3 +- MRZCodeParser/CodeTypeDetector.cs | 8 ++- MRZCodeParser/CodeTypes/TD1FirstLine.cs | 4 +- .../CodeTypes/TD1FirstLineLongDocument.cs | 22 +++++++ .../CodeTypes/TD1LongDocumentMrzCode.cs | 21 +++++++ MRZCodeParser/DocumentType.cs | 3 +- MRZCodeParser/FieldType.cs | 1 + MRZCodeParser/MRZCodeParser.csproj | 2 +- MRZCodeParser/MrzCode.cs | 1 + MRZCodeParser/MrzLine.cs | 4 +- MRZCodeParser/ReplaceHelper.cs | 28 +++++++++ MRZCodeParser/ValueCleaner.cs | 20 ++++++- 18 files changed, 175 insertions(+), 13 deletions(-) create mode 100644 MRZCodeParser.Tests/TD1DocumentLongMrzCodeTest.cs create mode 100644 MRZCodeParser/CodeTypes/TD1FirstLineLongDocument.cs create mode 100644 MRZCodeParser/CodeTypes/TD1LongDocumentMrzCode.cs create mode 100644 MRZCodeParser/ReplaceHelper.cs diff --git a/.gitignore b/.gitignore index 56e52b9..dfecfe3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ obj/ bin/ *.DotSettings.user dist/ +/.vs diff --git a/MRZCode.Samples/MRZCode.Samples.csproj b/MRZCode.Samples/MRZCode.Samples.csproj index 5753a57..14e51d4 100644 --- a/MRZCode.Samples/MRZCode.Samples.csproj +++ b/MRZCode.Samples/MRZCode.Samples.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1 + net9.0 diff --git a/MRZCodeParser.Tests/MRZCodeParser.Tests.csproj b/MRZCodeParser.Tests/MRZCodeParser.Tests.csproj index 3366375..787fa86 100644 --- a/MRZCodeParser.Tests/MRZCodeParser.Tests.csproj +++ b/MRZCodeParser.Tests/MRZCodeParser.Tests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net9.0 false diff --git a/MRZCodeParser.Tests/MrzSamples.cs b/MRZCodeParser.Tests/MrzSamples.cs index 6811096..2004596 100644 --- a/MRZCodeParser.Tests/MrzSamples.cs +++ b/MRZCodeParser.Tests/MrzSamples.cs @@ -12,6 +12,10 @@ public static class MrzSamples public const string TD1_SINGLE_LETTER_COUNTRY_CODE = @"I lines; + /// + /// Countries that use TD1 with long document number (30 characters). + /// + private readonly IEnumerable Countries_LongDocNumber = new List { "BEL" }; + internal CodeTypeDetector(IEnumerable lines) { this.lines = lines; @@ -15,7 +20,8 @@ internal CodeTypeDetector(IEnumerable lines) internal CodeType DetectType() { CodeType type = lines.Count() == 3 && lines.First().Length == 30 - ? CodeType.TD1 + ? Countries_LongDocNumber.Contains(lines.First().Substring(2,3)) + ? CodeType.TD1_LONG_DOC_NUMBER : CodeType.TD1 : lines.First().Length == 44 && lines.Count() == 2 ? lines.First()[0] == 'P' ? CodeType.TD3 diff --git a/MRZCodeParser/CodeTypes/TD1FirstLine.cs b/MRZCodeParser/CodeTypes/TD1FirstLine.cs index bc349d2..9c8a412 100644 --- a/MRZCodeParser/CodeTypes/TD1FirstLine.cs +++ b/MRZCodeParser/CodeTypes/TD1FirstLine.cs @@ -5,11 +5,11 @@ namespace MRZCodeParser.CodeTypes internal class TD1FirstLine : MrzLine { internal TD1FirstLine(string value) : base(value) - { - } + { } protected override string Pattern => "([A|C|I][A-Z0-9<]{1})([A-Z<]{3})([A-Z0-9<]{9})([0-9]{1})([A-Z0-9<]{15})"; + internal override IEnumerable FieldTypes => new[] { FieldType.DocumentType, diff --git a/MRZCodeParser/CodeTypes/TD1FirstLineLongDocument.cs b/MRZCodeParser/CodeTypes/TD1FirstLineLongDocument.cs new file mode 100644 index 0000000..6535e8b --- /dev/null +++ b/MRZCodeParser/CodeTypes/TD1FirstLineLongDocument.cs @@ -0,0 +1,22 @@ +using System.Collections.Generic; + +namespace MRZCodeParser.CodeTypes +{ + internal class TD1FirstLineLongDocument : MrzLine + { + internal TD1FirstLineLongDocument(string value) : base(value) + { } + + protected override string Pattern => "([A|C|I][A-Z0-9<]{1})([A-Z<]{3})([A-Z0-9<]{13})([0-9]{1})([A-Z0-9<]{11})"; + + + internal override IEnumerable FieldTypes => new[] + { + FieldType.DocumentType, + FieldType.CountryCode, + FieldType.DocumentNumber, + FieldType.DocumentNumberCheckDigit, + FieldType.OptionalData1 + }; + } +} diff --git a/MRZCodeParser/CodeTypes/TD1LongDocumentMrzCode.cs b/MRZCodeParser/CodeTypes/TD1LongDocumentMrzCode.cs new file mode 100644 index 0000000..b431fda --- /dev/null +++ b/MRZCodeParser/CodeTypes/TD1LongDocumentMrzCode.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using System.Linq; + +namespace MRZCodeParser.CodeTypes +{ + internal class TD1LongDocumentMrzCode : MrzCode + { + internal TD1LongDocumentMrzCode(IEnumerable lines) : base(lines) + { + } + + public override CodeType Type => CodeType.TD1; + + public override IEnumerable Lines => new MrzLine[] + { + new TD1FirstLineLongDocument(RawLines.First()), + new TD1SecondLine(RawLines.ElementAt(1)), + new TD1ThirdLine(RawLines.Last()) + }; + } +} \ No newline at end of file diff --git a/MRZCodeParser/DocumentType.cs b/MRZCodeParser/DocumentType.cs index 563aa26..5add892 100644 --- a/MRZCodeParser/DocumentType.cs +++ b/MRZCodeParser/DocumentType.cs @@ -6,6 +6,7 @@ public enum DocumentType C, I, P, - V + V, + ID } } \ No newline at end of file diff --git a/MRZCodeParser/FieldType.cs b/MRZCodeParser/FieldType.cs index c9bd5be..3294548 100644 --- a/MRZCodeParser/FieldType.cs +++ b/MRZCodeParser/FieldType.cs @@ -6,6 +6,7 @@ public enum FieldType { DocumentType, CountryCode, + [ReplaceHelper("")] DocumentNumber, DocumentNumberCheckDigit, OptionalData1, diff --git a/MRZCodeParser/MRZCodeParser.csproj b/MRZCodeParser/MRZCodeParser.csproj index bd2b578..1844040 100644 --- a/MRZCodeParser/MRZCodeParser.csproj +++ b/MRZCodeParser/MRZCodeParser.csproj @@ -1,7 +1,7 @@ - netstandard2.1;netcoreapp3.1 + net9.0 true 0.4.1 snifter diff --git a/MRZCodeParser/MrzCode.cs b/MRZCodeParser/MrzCode.cs index 2a6e73f..6a45523 100644 --- a/MRZCodeParser/MrzCode.cs +++ b/MRZCodeParser/MrzCode.cs @@ -72,6 +72,7 @@ public static MrzCode Parse(string code) CodeType.TD3 => new TD3MrzCode(lines), CodeType.MRVA => new MRVAMrzCode(lines), CodeType.MRVB => new MRVBMrzCode(lines), + CodeType.TD1_LONG_DOC_NUMBER => new TD1LongDocumentMrzCode(lines), _ => new UnknownMrzCode(lines) }; } diff --git a/MRZCodeParser/MrzLine.cs b/MRZCodeParser/MrzLine.cs index 368ce11..950f7a2 100644 --- a/MRZCodeParser/MrzLine.cs +++ b/MRZCodeParser/MrzLine.cs @@ -27,14 +27,14 @@ public FieldsCollection Fields { fields.Add(new Field( FieldTypes.ElementAt(i), - new ValueCleaner(match.Groups[i + 1].Value).Clean())); + new ValueCleaner(match.Groups[i + 1].Value).Clean(FieldTypes.ElementAt(i)))); } return new FieldsCollection(fields); } } - protected abstract string Pattern { get; } + protected abstract string Pattern { get;} internal abstract IEnumerable FieldTypes { get; } diff --git a/MRZCodeParser/ReplaceHelper.cs b/MRZCodeParser/ReplaceHelper.cs new file mode 100644 index 0000000..cb2f4ff --- /dev/null +++ b/MRZCodeParser/ReplaceHelper.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace MRZCodeParser +{ + [AttributeUsage(AttributeTargets.Field)] + internal class ReplaceHelper :Attribute + { + public string Simple { get; set; } = " "; + public string Double { get; set; } = ", "; + + public ReplaceHelper() + { } + + public ReplaceHelper(string simpleReplaceText) + { + Simple = simpleReplaceText; + Double = ", "; + } + + public ReplaceHelper(string simpleReplaceText, string doubleReplaceText) + { + Simple = simpleReplaceText; + Double = doubleReplaceText; + } + } +} diff --git a/MRZCodeParser/ValueCleaner.cs b/MRZCodeParser/ValueCleaner.cs index ba82040..d52b6a4 100644 --- a/MRZCodeParser/ValueCleaner.cs +++ b/MRZCodeParser/ValueCleaner.cs @@ -1,3 +1,7 @@ +using System; +using System.Linq; +using System.Reflection; + namespace MRZCodeParser { internal class ValueCleaner @@ -12,8 +16,20 @@ internal ValueCleaner(string value) internal string Clean() { return value.TrimEnd('<') - .Replace("<<", ", ") - .Replace("<", " "); + .Replace("<<", ", ") + .Replace("<", " "); + } + + internal string Clean(FieldType f) + { + var member = typeof(FieldType).GetMember(f.ToString()).FirstOrDefault(); + var s_replacer = member?.GetCustomAttribute() ?? new ReplaceHelper(" ", ", "); + + var t_val = value.TrimEnd('<') + .Replace("<<", s_replacer.Double) + .Replace("<", s_replacer.Simple); + + return t_val; } } } \ No newline at end of file From a5780480d05f976df93843856594c750f1ba87d8 Mon Sep 17 00:00:00 2001 From: Tony Peiffer Date: Thu, 24 Jul 2025 09:56:06 +0200 Subject: [PATCH 2/7] Restore target framework: netstandard2.1 --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index dfecfe3..7e35e65 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ bin/ *.DotSettings.user dist/ /.vs +/MRZCodeParser.Tests/TD1DocumentLongMrzCodeTest - Copy.cs From 76a46c2ce7b2458fa666dcd2d7fccb0d54b80126 Mon Sep 17 00:00:00 2001 From: Tony Peiffer Date: Thu, 24 Jul 2025 09:56:53 +0200 Subject: [PATCH 3/7] Adding a sample for the belgium id with the long document format --- MRZCode.Samples/TD1_doc_longSample.cs | 41 +++++++++++++++++++ .../MRZCodeParser.Tests.csproj | 4 ++ MRZCodeParser/MRZCodeParser.csproj | 6 +-- 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 MRZCode.Samples/TD1_doc_longSample.cs diff --git a/MRZCode.Samples/TD1_doc_longSample.cs b/MRZCode.Samples/TD1_doc_longSample.cs new file mode 100644 index 0000000..8a61b33 --- /dev/null +++ b/MRZCode.Samples/TD1_doc_longSample.cs @@ -0,0 +1,41 @@ +using System; +using MRZCodeParser; + +namespace MRZCode.Samples +{ + public static class TD1_doc_long_Sample + { + public static void Run() + { + var text = @"IDBEL123456789<2383<<<<<<<<<<< +7408122F1204159BEL740812123457 +ERIKSSON<false + + + + diff --git a/MRZCodeParser/MRZCodeParser.csproj b/MRZCodeParser/MRZCodeParser.csproj index 1844040..7caaa79 100644 --- a/MRZCodeParser/MRZCodeParser.csproj +++ b/MRZCodeParser/MRZCodeParser.csproj @@ -1,11 +1,11 @@ - net9.0 + netstandard2.1;net9.0 true - 0.4.1 + 0.4.2 snifter - 0.4.1 + 0.4.2 MRZCodeParser https://github.com/snifter/MRZCode.NET https://github.com/snifter/MRZCode.NET/blob/master/LICENSE From 48ec08461ae218280e32c7f1892e9bec46e22981 Mon Sep 17 00:00:00 2001 From: tpe_007 <28335571+tPeif@users.noreply.github.com> Date: Thu, 24 Jul 2025 10:01:00 +0200 Subject: [PATCH 4/7] Update .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7e35e65..dfecfe3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,3 @@ bin/ *.DotSettings.user dist/ /.vs -/MRZCodeParser.Tests/TD1DocumentLongMrzCodeTest - Copy.cs From ebcefa2655234875a77e2ae34343dd1c8d2f8b62 Mon Sep 17 00:00:00 2001 From: tpe_007 <28335571+tPeif@users.noreply.github.com> Date: Thu, 24 Jul 2025 10:21:14 +0200 Subject: [PATCH 5/7] Update MRZCode.Samples.csproj .netstandard2.1 --- MRZCode.Samples/MRZCode.Samples.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MRZCode.Samples/MRZCode.Samples.csproj b/MRZCode.Samples/MRZCode.Samples.csproj index 14e51d4..dd35513 100644 --- a/MRZCode.Samples/MRZCode.Samples.csproj +++ b/MRZCode.Samples/MRZCode.Samples.csproj @@ -2,7 +2,7 @@ Exe - net9.0 + netstandard2.1;net9.0 From 1634450d3d935a0656199d7c33c5a6122c6de837 Mon Sep 17 00:00:00 2001 From: tpe_007 <28335571+tPeif@users.noreply.github.com> Date: Thu, 24 Jul 2025 10:21:45 +0200 Subject: [PATCH 6/7] Update MRZCodeParser.Tests.csproj netstandard2.1 --- MRZCodeParser.Tests/MRZCodeParser.Tests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MRZCodeParser.Tests/MRZCodeParser.Tests.csproj b/MRZCodeParser.Tests/MRZCodeParser.Tests.csproj index 971c8d8..b07102e 100644 --- a/MRZCodeParser.Tests/MRZCodeParser.Tests.csproj +++ b/MRZCodeParser.Tests/MRZCodeParser.Tests.csproj @@ -1,7 +1,7 @@ - net9.0 + netstandard2.1;net9.0 false From 1c263e55a771792dd313aefa5ae67472849dd60e Mon Sep 17 00:00:00 2001 From: Tony Peiffer Date: Thu, 24 Jul 2025 10:24:45 +0200 Subject: [PATCH 7/7] support for netstandard2.1 --- MRZCode.Samples/MRZCode.Samples.csproj | 2 +- MRZCodeParser.Tests/MRZCodeParser.Tests.csproj | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/MRZCode.Samples/MRZCode.Samples.csproj b/MRZCode.Samples/MRZCode.Samples.csproj index 14e51d4..dd35513 100644 --- a/MRZCode.Samples/MRZCode.Samples.csproj +++ b/MRZCode.Samples/MRZCode.Samples.csproj @@ -2,7 +2,7 @@ Exe - net9.0 + netstandard2.1;net9.0 diff --git a/MRZCodeParser.Tests/MRZCodeParser.Tests.csproj b/MRZCodeParser.Tests/MRZCodeParser.Tests.csproj index 971c8d8..cc1a3c7 100644 --- a/MRZCodeParser.Tests/MRZCodeParser.Tests.csproj +++ b/MRZCodeParser.Tests/MRZCodeParser.Tests.csproj @@ -1,15 +1,11 @@ - net9.0 + netstandard2.1;net9.0 false - - - -