• 利用ThoughtWorks.QRCode生成二维码


    一、项目添加ThoughtWorks.QRCode.dll和System.Drawing.dll的引用

    二、创建二维码公共处理类(QRCodeHandler.cs)

    /// <summary>
        /// 二维码处理类
        /// </summary>
        public class QRCodeHandler
        {
            /// <summary>
            /// 创建二维码
            /// </summary>
            /// <param name="QRString">二维码字符串</param>
            /// <param name="QRCodeEncodeMode">二维码编码(Byte、AlphaNumeric、Numeric)</param>
            /// <param name="QRCodeScale">二维码尺寸(Version为0时,1:26x26,每加1宽和高各加25</param>
            /// <param name="QRCodeVersion">二维码密集度0-40</param>
            /// <param name="QRCodeErrorCorrect">二维码纠错能力(L:7% M:15% Q:25% H:30%)</param>
            /// <param name="filePath">保存路径</param>
            /// <param name="hasLogo">是否有logo(logo尺寸50x50,QRCodeScale>=5,QRCodeErrorCorrect为H级)</param>
            /// <param name="logoFilePath">logo路径</param>
            /// <returns></returns>
            public bool CreateQRCode(string QRString, string QRCodeEncodeMode, short QRCodeScale, int QRCodeVersion, string QRCodeErrorCorrect, string filePath, bool hasLogo, string logoFilePath)
            {
                bool result = true;
    
                QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
    
                switch (QRCodeEncodeMode)
                {
                    case "Byte":
                        qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
                        break;
                    case "AlphaNumeric":
                        qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
                        break;
                    case "Numeric":
                        qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
                        break;
                    default:
                        qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
                        break;
                }
    
                qrCodeEncoder.QRCodeScale = QRCodeScale;
                qrCodeEncoder.QRCodeVersion = QRCodeVersion;
    
                switch (QRCodeErrorCorrect)
                {
                    case "L":
                        qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
                        break;
                    case "M":
                        qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
                        break;
                    case "Q":
                        qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
                        break;
                    case "H":
                        qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
                        break;
                    default:
                        qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
                        break;
                }
    
                try
                {
                    Image image = qrCodeEncoder.Encode(QRString, System.Text.Encoding.UTF8);
                    System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
                    image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
                    fs.Close();
    
                    if (hasLogo)
                    {
                        Image copyImage = System.Drawing.Image.FromFile(logoFilePath);
                        Graphics g = Graphics.FromImage(image);
                        int x = image.Width / 2 - copyImage.Width / 2;
                        int y = image.Height / 2 - copyImage.Height / 2;
                        g.DrawImage(copyImage, new Rectangle(x, y, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
                        g.Dispose();
    
                        image.Save(filePath);
                        copyImage.Dispose();
                    }
                    image.Dispose();
    
                }
                catch (Exception ex)
                {
                    result = false;
                }
                return result;
            }
    
        }

    调用

      QRCodeHandler qr = new QRCodeHandler();
                string path = Path.Combine("C:\", "Down\");
                string qrString = "http://blog.csdn.net/pan_junbiao";                         //二维码字符串
                string logoFilePath = path + "myLogo.jpg";                                    //Logo路径50*50
                string filePath = path + "myCode.jpg";                                        //二维码文件名
                qr.CreateQRCode(qrString, "Byte", 5, 0, "H", filePath, true, logoFilePath);   //生成

     

  • 相关阅读:
    JavaScript按纯数字排序
    用jQuery监听浏览器窗口的变化
    jquery-jtemplates.js模板应用
    art-template模板应用
    JavaScript判断当前手机是Android还是iOS系统
    JavaScript数组转字符串,字符串转数组
    JavaScript数字转字符串,字符串转数字
    Play framework 安装
    JQuery判断数组中是否包含某个字符串
    js获取页面宽度高度及屏幕分辨率
  • 原文地址:https://www.cnblogs.com/macT/p/11395234.html
Copyright © 2020-2023  润新知