1. 程式人生 > >C#使用QRCoder.dll生成二維碼

C#使用QRCoder.dll生成二維碼

1、專案新增QRCoder.dll的引用

使用NuGet新增該引用步驟:右鍵點選專案的,引用 —> 管理NuGet程式包 —> 在搜尋框中輸入:QRCoder —> 安裝。

2、建立二維碼公共處理類(QRCoderHelper.cs)

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using QRCoder;

/// <summary>
/// 二維碼公共處理類
/// </summary>
public static class QRCoderHelper
{
    /// <summary>
    /// 建立二維碼返回檔案路徑名稱
    /// </summary>
    /// <param name="plainText">二維碼內容</param>
    public static string CreateQRCodeToFile(string plainText)
    {
        try
        {
            string fileName = "";
            if (String.IsNullOrEmpty(plainText))
            {
                return "";
            }

            //二維碼檔案目錄
            string filePath = @"F:\Images\QR\";
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            //建立二維碼檔案路徑名稱
            fileName = filePath + DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(100, 1000) + ".jpeg";

            QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
            //QRCodeGenerator.ECCLevel:糾錯能力,Q級:約可糾錯25%的資料碼字
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(plainText, QRCodeGenerator.ECCLevel.Q);
            QRCode qrcode = new QRCode(qrCodeData);
            Bitmap qrCodeImage = qrcode.GetGraphic(15);
            qrCodeImage.Save(fileName, ImageFormat.Jpeg);
            return fileName;
        }
        catch(Exception ex)
        {
            throw new Exception("建立二維碼返回檔案路徑名稱方法異常", ex);
        }
    }

    /// <summary>
    /// 建立二維碼返回byte陣列
    /// </summary>
    /// <param name="plainText">二維碼內容</param>
    public static byte[] CreateQRCodeToBytes(string plainText)
    {
        try
        {
            if (String.IsNullOrEmpty(plainText))
            {
                return null;
            }

            QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
            //QRCodeGenerator.ECCLevel:糾錯能力,Q級:約可糾錯25%的資料碼字
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(plainText, QRCodeGenerator.ECCLevel.Q);
            QRCode qrcode = new QRCode(qrCodeData);
            Bitmap qrCodeImage = qrcode.GetGraphic(15);
            MemoryStream ms = new MemoryStream();
            qrCodeImage.Save(ms, ImageFormat.Jpeg);
            byte[] arr = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(arr, 0, (int)ms.Length);
            ms.Close();

            return arr;
        }
        catch(Exception ex)
        {
            throw new Exception("建立二維碼返回byte陣列方法異常", ex);
        }
    }

    /// <summary>
    /// 建立二維碼返回Base64字串
    /// </summary>
    /// <param name="plainText">二維碼內容</param>
    public static string CreateQRCodeToBase64(string plainText,bool hasEdify = true)
    {
        try
        {
            string result = "";
            if (String.IsNullOrEmpty(plainText))
            {
                return "";
            }

            QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
            //QRCodeGenerator.ECCLevel:糾錯能力,Q級:約可糾錯25%的資料碼字
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(plainText, QRCodeGenerator.ECCLevel.Q);
            QRCode qrcode = new QRCode(qrCodeData);
            Bitmap qrCodeImage = qrcode.GetGraphic(15);
            MemoryStream ms = new MemoryStream();
            qrCodeImage.Save(ms, ImageFormat.Jpeg);
            byte[] arr = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(arr, 0, (int)ms.Length);
            ms.Close();
            if (hasEdify)
            {
                result = "data:image/jpeg;base64," + Convert.ToBase64String(arr);
            }
            else
            {
                result = Convert.ToBase64String(arr);
            }
            return result;
        }
        catch (Exception ex)
        {
            throw new Exception("建立二維碼返回Base64字串方法異常", ex);
        }
    }
}

3、測試

public static void QRCoderTest()
{
    //二維碼內容
    string plainText = "https://blog.csdn.net/pan_junbiao/article/details/82956799";

    //建立二維碼返回檔案路徑名稱
    string fileName = QRCoderHelper.CreateQRCodeToFile(plainText);
}