C# 二维码的生成 Open Source QRCode Library

时间:2022-12-14 20:33:34

一、简单点的

public  class QRCode
    {
        /// <summary>
        /// 根据ID生成一个二维码
        /// /// </summary>
        /// <param name="cardID"></param>
        /// <returns>二维码</returns>
        public  Image QREncoder(string cardID)
        {
            Image image = null;
            QRCodeEncoder encoder = new QRCodeEncoder();
            try
            {
                //设置编码类型
                encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
                //设置尺寸
                encoder.QRCodeScale = 3;
                //设置版本
                encoder.QRCodeVersion = 2;
                //设置纠错
                encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
                image = encoder.Encode(cardID);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                image = null;
            }
            return image;
        }
        /// <summary>
        /// 根据二维码获取数据
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public string QRDecoder(Image image)
        {
            string str = new QRCodeDecoder().decode(QRCodeBitmapImage(new Bitmap(image)));
            return str;
         }
    }

二、详细点的:

Introduction

In this article, I will briefly describe the functionalities of the QRCode library.

C# 二维码的生成 Open Source QRCode Library

Background

QRCode library is a .NET component that can be used to encode and decode QRCode. QRCode is a 2 dimensional bar code that originated in Japan. Nowadays, it is widely used in a wide range of industries, e.g. for vehicle parts tracking and inventory management.

QR stands for "Quick Response". It was created by the Japanese corporation Denso-Wave in 1994 and is aimed at decoding contents at high speed. Nowadays, QR Code is used in mobile phones to ease data entry.

QRCode can also be printed on a business card or shown on any display, which can then be captured by the mobile phone provided the mobile phone has the software to read QRCode.

QRCode library provides functions to:

  1. Encode content into a QR Code image which can be saved in JPEG, GIF, PNG, or Bitmap formats
  2. Decode a QR Code image

Using the Code

The library can be used in any .NET 2.0 Windows Application, ASP.NET Web application or Windows Mobile device application.

Some sample screenshots are displayed below:

 C# 二维码的生成 Open Source QRCode Library

C# 二维码的生成 Open Source QRCode Library

C# 二维码的生成 Open Source QRCode Library Collapse |Copy Code
  QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
String encoding = cboEncoding.Text ;
if (encoding == "Byte") {
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
} else if (encoding == "AlphaNumeric") {
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
} else if (encoding == "Numeric") {
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
}
try {
int scale = Convert.ToInt16(txtSize.Text);
qrCodeEncoder.QRCodeScale = scale;
} catch (Exception ex) {
MessageBox.Show("Invalid size!");
return;
}
try {
int version = Convert.ToInt16(cboVersion.Text) ;
qrCodeEncoder.QRCodeVersion = version;
} catch (Exception ex) {
MessageBox.Show("Invalid version !");
}

string errorCorrect = cboCorrectionLevel.Text;
if (errorCorrect == "L")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
else if (errorCorrect == "M")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
else if (errorCorrect == "Q")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
else if (errorCorrect == "H")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;

Image image;
String data = txtEncodeData.Text;
image = qrCodeEncoder.Encode(data);
picEncode.Image = image;