关于QRCode生成二维码(背景图、Logo)

关于QRCode生成二维码的代码

///   
        /// 创建二维码  
        ///   
        /// "QRString">二维码字符串  
        /// "QRCodeEncodeMode">二维码编码(Byte、AlphaNumeric、Numeric)  
        /// "QRCodeScale">二维码尺寸(Version为0时,126x26,每加1宽和高各加25  
        /// "QRCodeVersion">二维码密集度0-40  
        /// "QRCodeErrorCorrect">二维码纠错能力(L:7% M:15% Q:25% H:30%)  
        /// "filePath">保存路径  
        /// "hasLogo">是否有logo(logo尺寸50x50,QRCodeScale>=5,QRCodeErrorCorrect为H级)  
        /// "logoFilePath">logo路径  
        ///   
public static bool CreateQRCode(string QRString, string QRCodeEncodeMode, short QRCodeScale, int QRCodeVersion, string QRCodeErrorCorrect, string filePath, bool hasLogo, string logoFilePath,bool hasBack, string backFilePath,string backName, string fileName)
        {
            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
            {
                if (hasBack)
                {
                    //抓取背景图片复制到新文件夹并命名
                    File.Copy(backFilePath, filePath + fileName);
                    //生成二维码图片
                    Image image_b = qrCodeEncoder.Encode(QRString, System.Text.Encoding.UTF8);
                    System.IO.FileStream fs_b = new System.IO.FileStream(filePath + backName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
                    //存储二维码图片
                    image_b.Save(fs_b, System.Drawing.Imaging.ImageFormat.Jpeg);
                    //关闭IO
                    fs_b.Close();
                    //将二维码图片转化为Image对象
                    Image copyImage = Image.FromFile(filePath + backName);
                    //创建FileStream
                    FileStream fs = new FileStream(filePath + fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                    //将背景图片转化为二进制数据
                    byte[] buffer = new byte[fs.Length];
                    int length = 0;
                    int ibyteRead = 0;
                    do
                    {
                        length = fs.Read(buffer, ibyteRead, buffer.Length - ibyteRead);
                        ibyteRead += length;
                    }
                    while (length > 0);
                    MemoryStream mfs = new MemoryStream(buffer);
                    //关闭创建FileStream
                    fs.Close();
                    fs.Dispose();
                    //创建画布
                    Image bmp = Image.FromStream(mfs);
                    //创建Graphics类
                    Graphics g = Graphics.FromImage(bmp);
                    //设定坐标
                    int x = bmp.Width / 2 - copyImage.Width / 2;
                    int y = ((bmp.Height / 5)*4)/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();

                    //保存画好的图片
                    bmp.Save(filePath + fileName);
                    copyImage.Dispose();
                    //删除生成的二维码图片
                    if (File.Exists(filePath + backName))
                    {
                        File.Delete(filePath + backName);
                    }

                }
                else
                {
                    Image image = qrCodeEncoder.Encode(QRString, System.Text.Encoding.UTF8);
                    System.IO.FileStream fs = new System.IO.FileStream(filePath + fileName, 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 + fileName);
                        copyImage.Dispose();
                    }
                    image.Dispose();
                }
            }
            catch (Exception ex)
            {
                result = false;
            }
            return result;
        } 

使用本方法时请先调用ThougtWorks.QRCode.dll

你可能感兴趣的:(Codeing)