.NET - Generate and recognize QRCode

Generate QRCode

  • 使用 QRCoder

Github

class Program
{
    static void Main(string[] args)
    {
        QRCodeGenerator qrGenerator = new QRCodeGenerator();
        QRCodeData qrCodeData = qrGenerator.CreateQrCode("你好,世界!", QRCodeGenerator.ECCLevel.Q);
        QRCode qrCode = new QRCode(qrCodeData);
        var qrCodeImage = qrCode.GetGraphic(20);
        qrCodeImage.Save(@"C:\TEMP\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}

输出结果

QR Example

Recognize QRcode

Github

class Program
{
    static void Main(string[] args)
    {
        // create a barcode reader instance

        IBarcodeReader reader = new BarcodeReader();
        // load a bitmap

        var barcodeBitmap = (Bitmap)Image.FromFile(@"C:\\TEMP\test.jpg");
        // detect and decode the barcode inside the bitmap

        var result = reader.Decode(barcodeBitmap);
        // do something with the result

        if (result != null) {
            Console.WriteLine(result.BarcodeFormat.ToString());
            Console.WriteLine(result.Text);
        }
    }
}

输出结果

QR_CODE
你好,世界!