C# generates QR code and the pits that appear

C# generates QR code and the pits that appear

QR code is a common coding method, which is often used in scanning payment, commodity bar code and other fields. In the process of generating and using the QR code, some problems may be encountered.

QR code generation principle

The two-dimensional code is generated by encoding the data, and then using a specific algorithm to convert the encoded data into a two-dimensional code pattern. Common two-dimensional code encoding methods include QR Code, Data Matrix, PDF417, etc.

When generating a QR code, many factors need to be considered, such as the type of QR code, error tolerance rate, version, etc. Choosing proper parameters can make the QR code more stable and reliable.

C# implements two-dimensional code generation

In C#, you can use a third-party library such as ZXing.Net to realize the generation and recognition of the QR code. The following is a simple C# code example for generating a QR code containing a string:

using ZXing;
using ZXing.Common;
using ZXing.QrCode;

public Bitmap GenerateQRCode(string content)
{
    var options = new QrCodeEncodingOptions
    {
        DisableECI = true,
        CharacterSet = "UTF-8",
        ErrorCorrection = ErrorCorrectionLevel.H,
        Width = 250,
        Height = 250,
    };
    var writer = new BarcodeWriterPixelData
    {
        Format = BarcodeFormat.QR_CODE,
        Options = options,
    };
    var pixelData = writer.Write(content);
    var bmp = pixelData.ToBitmap();
    return bmp;
}

The following is a simple C# code example for generating a QR code in Data Matrix format:

using ZXing;
using ZXing.Common;
using ZXing.Datamatrix;
using ZXing.Datamatrix.Encoder;

public Bitmap GenerateDataMatrixCode(string content)
{
    var encoding = new ASCIIEncoding();
    var byteContent = encoding.GetBytes(content);
    var matrix = new DataMatrixWriter().encode(byteContent, BarcodeFormat.DATA_MATRIX, 0, 0);
    var pixelData = new PixelData(matrix.Width, matrix.Height, matrix.Array, PixelFormat.Gray8);
    var bmp = pixelData.ToBitmap();
    return bmp;
}

The following is a simple C# code example for generating a QR code in PDF417 format:

using ZXing;
using ZXing.Common;
using ZXing.PDF417;

public Bitmap GeneratePDF417Code(string content)
{
    var options = new PDF417EncodingOptions
    {
        ErrorCorrection = 0,
        Rows = 10,
        Columns = 10,
        Truncated = false,
    };
    var writer = new BarcodeWriterPixelData
    {
        Format = BarcodeFormat.PDF_417,
        Options = options,
    };
    var pixelData = writer.Write(content);
    var bmp = pixelData.ToBitmap();
    return bmp;
}

The pit that appears in the QR code

Recently, when using Data Matrix to scan a QR code, I found that a special character appeared at the 33rd position, which caused some troubles. After searching and analyzing, the following reasons and solutions were obtained.

reason

The reason for the appearance of special characters is that the QR code contains illegal characters or escape characters, which will be mistaken by the parser for instructions rather than content when scanned.

Solution

The solution to this problem is to encode the possible illegal characters or escape characters when generating the QR code, so that the content can be correctly parsed when scanning.

If the QR code has been generated, you can use some professional QR code repair tools to repair it, or you can solve it by regenerating the QR code.

The above is about the reasons and solutions for special characters appearing in the QR code scanned by Data Matrix, I hope it will be helpful to everyone.

Guess you like

Origin blog.csdn.net/Documentlv/article/details/130401004