ZXing.Net icon indicating copy to clipboard operation
ZXing.Net copied to clipboard

force C40 encoding mode in data matrix

Open cyril0511 opened this issue 3 years ago • 3 comments

Hello, I'd like to create a datamatrix qr code with C40 encoding. There's a french institutional website (https://interop.esante.gouv.fr/datamatrixins-1.0-SNAPSHOT/) which allow me to verify my qr Code. Here is my code:

var writer = new BarcodeWriter
            {
                Format = ZXing.BarcodeFormat.DATA_MATRIX,
                Options = new DatamatrixEncodingOptions
                {
                    DefaultEncodation = (int?)Encodation.C40,
                    Height = 175,
                    Width = 175
                },
                Encoder = new DataMatrixWriter()
            };

            writer.Write("IS010000000000000000000000S1123456789876543S21.2.250.1.213.1.4.8"+(char)29+"S3MATHIEU MICHEL"+(char)29+"S4MELON"+(char)29+"S5MS610-02-1966")
                .Save(@"C:\Barcode.png");

When I try to validate generated qr code, it fails and says that C40 encoding is incorrect. When I look at C40Encoder.cs, it seems that the problem is located here (encode method):

if ((count % 3) == 0)
  {
      int newMode = HighLevelEncoder.lookAheadTest(context.Message, context.Pos, EncodingMode);
      if (newMode != EncodingMode)
      {
          // Return to ASCII encodation, which will actually handle latch to new mode
          context.signalEncoderChange(Encodation.C40);
          break;
      }
  }

If I remove that portion of code, or if I remove break statement, generated qr code is validated. Is that portion of code really necessary (or at least break statement)?

cyril0511 avatar Feb 08 '22 13:02 cyril0511

Yes, these lines are necessary to switch between different encoding modes. A round-trip test (encoding and decoding) shows that the encoded barcode with the current implementation can be successfully decoded. The implementation is correct at this point. I think you need something different. There is a requirement of the French government that the DataMatrix code should be using the C40 encoding only. You need an encoding switch to force the use of C40 and prevent switching between different modes. There is already an implementation of that switch in the java version of zxing: https://github.com/zxing/zxing/pull/1495 I have to port that enhancement to ZXing.Net. Meanwhile, you can build your own version of ZXing.Net and remove the two lines of code.

micjahn avatar Feb 09 '22 06:02 micjahn

Hello guys, i have the exact same requirements (from the french government too).

Is this issue still relevant ? (i think so otherwise this issue will no longer be open).

If not, there is a easier workaround than build an update version of this library ?

thanks !

julienGrd avatar Sep 14 '22 10:09 julienGrd

It's relevant. I didn't find the time to port the changes in the java version to .net.

micjahn avatar Sep 14 '22 18:09 micjahn

Hello, This problem is resolved in master and by enabling FORCE_C40, see this commit.

A release is planned with any changes?

meurillonb avatar Feb 01 '23 08:02 meurillonb

I will build a new release as soon as possible. But there is a lot of work which has to be done before.

micjahn avatar Feb 08 '23 20:02 micjahn

Looking forward to that release, Thanks!

ualidigithou avatar Feb 22 '23 18:02 ualidigithou

Please try version 0.16.9, available via nuget

micjahn avatar Feb 23 '23 22:02 micjahn