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

namespace JiaZhiQuan.Common.Utils
{
    /// <summary>
    /// 二维码生成工具类
    /// </summary>
    public class GenerateQRCodeUtils
    {
        /// <summary>
        /// 生成二维码
        /// 默认生成白底黑块的二维码
        /// </summary>
        /// <param name="text"></param>
        /// <param name="outputPath"></param>
        /// <param name="pixel">像素大小</param>
        public static void GenerateQRCode(string text, string outputPath, int pixel = 5)
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new QRCode(qrCodeData);
            Bitmap qrCodeImage = qrCode.GetGraphic(pixel, Color.Black, Color.White, true);
            qrCodeImage.Save(outputPath, System.Drawing.Imaging.ImageFormat.Png);
        }

        /// <summary>
        /// 生成二维码，返回内存流(记得关闭流！！！)
        /// 默认生成白底黑块的二维码
        /// </summary>
        /// <param name="text"></param>
        /// <param name="pixel">像素大小</param>
        /// <returns></returns>
        public static MemoryStream GenerateQRCode(string text, int pixel = 5)
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new QRCode(qrCodeData);

            Bitmap qrCodeImage = qrCode.GetGraphic(pixel, Color.Black, Color.White, true);

            MemoryStream memoryStream = new MemoryStream();
            qrCodeImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
            return memoryStream;
        }
    }
}
