Skip to content

DataMatrix FNC1 at first byte #635

Description

@NenashevArtem

I am using the ZXing.Net package (0.16.11) in a .NET9 project. I need to generate a DataMatrix GS1 image. I need the symbol, when read, to have the string start with the FNC1 code (0xE8), but I can't get this to work. Setting GS1Format = true doesn't help. Here is my method:

public static Bitmap GenerateFromBase64(int size, string base64Data)
{
    byte[] rawBytes = Convert.FromBase64String(base64Data);

    // Add prefix 0xE8 if not exist
    byte[] finalBytes;
    if (rawBytes.Length > 0 && rawBytes[0] == 0xE8)
    {
        // Prifix exist
        finalBytes = rawBytes;
    }
    else
    {
        // Add prefix
        finalBytes = new byte[rawBytes.Length + 1];
        finalBytes[0] = 0xE8;
        Buffer.BlockCopy(rawBytes, 0, finalBytes, 1, rawBytes.Length);
    }

    string data = System.Text.Encoding.GetEncoding("ISO-8859-1").GetString(rawBytes);

    var writer = new BarcodeWriterPixelData
    {
        Format = BarcodeFormat.DATA_MATRIX,
        Options = new DatamatrixEncodingOptions
        {
            Height = size,
            Width = size,
            Margin = 0,
            SymbolShape = SymbolShapeHint.FORCE_SQUARE,
            GS1Format = true,
            PureBarcode = false
        }
    };

    var pixelData = writer.Write(data);

    var bitmap = new Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height),
                                     System.Drawing.Imaging.ImageLockMode.WriteOnly,
                                     System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    try
    {
        System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
    }
    finally
    {
        bitmap.UnlockBits(bitmapData);
    }
    return bitmap;
}

Testing with value

string base64Input = "MDEwNDYwMTAzMjAxMDkwNDIxNVpTRmZHUUxRckhoNkRjKkdZeU0dOTFFRTEyHTkyZmw1akJyTGRoeUNLUklmMHdwWEZ5eExUZ3BqWEtBQzJWQzZKM01jY1N6Yz0=";
Bitmap dmCode = DataMatrixTest.DataMatrixGenerator.GenerateFromBase64(300, base64Input);

It mast by like this, but no e8 at begining
Image

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions