首页 > 代码库 > 生成二维码

生成二维码

二维码公共类

    /// <summary>
    /// QrCode
    /// </summary>    
    public class QrCode
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="data">生成二维码的文本</param>
        public QrCode(string data)
        {
            this.Data =http://www.mamicode.com/ data;
        }

        /// <summary>
        /// 生成二维码数据
        /// </summary>
        public string Data { get; set; }

        /// <summary>
        /// 生成二维码尺寸
        /// </summary>
        public QRCodeEncoder.ENCODE_MODE CodeEncoder { get; set; } = QRCodeEncoder.ENCODE_MODE.BYTE;

        /// <summary>
        /// 错误相识度
        /// </summary>
        public QRCodeEncoder.ERROR_CORRECTION CodeErrorCorrect { get; set; } = QRCodeEncoder.ERROR_CORRECTION.M;

        /// <summary>
        /// 版本
        /// </summary>
        public int CodeVersion { get; set; } = 8;

        /// <summary>
        /// 尺寸大小
        /// </summary>
        public int Scale { get; set; } = 4;

        /// <summary>
        /// 背景图片地址
        /// </summary>
        public string ImgBackPath { get; set; }

        /// <summary>
        /// 背景图片大小
        /// </summary>
        public int ImgBackSize { get; set; }

        /// <summary>
        /// 生成二维码图片
        /// </summary>
        /// <returns>Image</returns>
        public Image CreateCodeImage()
        {
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder
            {
                QRCodeEncodeMode = this.CodeEncoder,
                QRCodeScale = this.Scale,
                QRCodeVersion = this.CodeVersion,
                QRCodeErrorCorrect = this.CodeErrorCorrect
            };
            Image image = qrCodeEncoder.Encode(this.Data);
            if (!string.IsNullOrEmpty(this.ImgBackPath) && this.ImgBackSize > 0)
            {
                Image imgBack = CombinImage(image, this.ImgBackPath);
                return imgBack;
            }
            return image;
        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <returns>byte[]</returns>
        public byte[] CreateCodeByte()
        {
            var image = CreateCodeImage();
            MemoryStream ms = new MemoryStream();
            image.Save(ms, ImageFormat.Jpeg);
            return ms.ToArray();
        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <returns>string</returns>
        public string CreateCodeString()
        {
            var bytes = CreateCodeByte();
            var result = Convert.ToBase64String(bytes);
            return result;
        }

        /// <summary>
        /// Convert String to Image
        /// </summary>
        /// <param name="str">string</param>
        /// <returns>Image</returns>
        public Image StringToImage(string str)
        {
            byte[] array = Convert.FromBase64String(str);
            MemoryStream ms = new MemoryStream(array);
            Image image = Image.FromStream(ms);
            return image;
        }

        /// <summary>    
        /// 调用此函数后使此两种图片合并,类似相册,有个    
        /// 背景图,中间贴自己的目标图片    
        /// </summary>    
        /// <param name="imgBack">粘贴的源图片</param>    
        /// <param name="destImg">粘贴的目标图片</param>    
        private Image CombinImage(Image imgBack, string destImg)
        {
            Image img = Image.FromFile(destImg);
            if (img.Height != this.ImgBackSize || img.Width != this.ImgBackSize)
            {
                img = KiResizeImage(img, this.ImgBackSize, this.ImgBackSize);
            }
            Graphics g = Graphics.FromImage(imgBack);
            g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);      //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);     
            g.FillRectangle(Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1, 1, 1);//相片四周刷一层黑色边框    
            g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height);
            GC.Collect();
            return imgBack;
        }

        /// <summary>    
        /// Resize图片    
        /// </summary>    
        /// <param name="bmp">原始Bitmap</param>    
        /// <param name="newW">新的宽度</param>    
        /// <param name="newH">新的高度</param>    
        /// <returns>处理以后的图片</returns>    
        private static Image KiResizeImage(Image bmp, int newW, int newH)
        {
            try
            {
                Image b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                g.Dispose();
                return b;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 二维码解码
        /// </summary>
        /// <param name="filePath">图片路径</param>
        /// <returns>二维码文本</returns>
        public string CodeDecoder(string filePath)
        {
            if (!System.IO.File.Exists(filePath))
            {
                return null;
            }
            Bitmap myBitmap = new Bitmap(Image.FromFile(filePath));
            QRCodeDecoder decoder = new QRCodeDecoder();
            string decodedString = decoder.decode(new QRCodeBitmapImage(myBitmap));
            return decodedString;
        }
    }

调用方法

            QrCode.QrCode qrCode = new QrCode.QrCode("http://www.baidu.com");
            var str = qrCode.CreateCodeString();
            var imge = qrCode.StringToImage(str);
            var filePath = @"E:" + DateTime.Now.ToString("yyyymmddhhmmssfff") + ".jpg";
            FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
            imge.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
            fs.Close();
            imge.Dispose();

ThoughtWorks.QRCode.dll

生成二维码