zoukankan      html  css  js  c++  java
  • .netcore 生成二维码:中间有logo图标的二维码

    本文介绍在.netcore 平台下,使用 ThoughtWorks.QRCode.Core 生成中间有logo图标的二维码

    第一步:NuGet 添加 ThoughtWorks.QRCode.Core:

    第二步:编写代码

    public class QRCodeHelper
    {
    public static string GetQRCode()
    {

            using (System.Drawing.Image qrCodeImg = GenerateQRCode("1234567890abcdefghABCDEFGH唐宋元明清")) //生成的二维码
            {
                //生成二维码中间logo的图片。
                string logoImgPath = $"{AppDomain.CurrentDomain.BaseDirectory}/Assets/ContactQRCode/QRCodeLogo.png";
                using (System.Drawing.Image logoImg = System.Drawing.Image.FromFile(logoImgPath))
                {
                    //组合 二维码和logo,形成带logo的二维码,并保存
                    string qrCodeImgPath = $"{AppDomain.CurrentDomain.BaseDirectory}/Assets/ContactQRCode/22.png";
                    CombinImage(qrCodeImg, logoImg).Save(qrCodeImgPath);
                    return qrCodeImgPath;
                }   
            }
        }
    
        /// <summary>
        /// 生成二维码:根据传进去的数据 生成二维码
        /// </summary>
        /// <param name="data">用于生成二维码的数据</param>
        /// <returns></returns>
        public static System.Drawing.Image GenerateQRCode(String data)
        {
            //创建编码器,设置编码格式。Byte格式的编码,只要能转成Byte的数据,都可以进行编码,比如中文。NUMERIC 只能编码数字。
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
    
            //大小,值越大生成的二维码图片像素越高
            qrCodeEncoder.QRCodeScale = 5;
    
            //版本,设置为0主要是防止编码的字符串太长时发生错误
            qrCodeEncoder.QRCodeVersion = 0;
    
            //生成二维码 Bitmap
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;//错误效验、错误更正(有4个等级)
            qrCodeEncoder.QRCodeBackgroundColor = System.Drawing.Color.White;//背景色
            qrCodeEncoder.QRCodeForegroundColor = System.Drawing.Color.Black;//前景色
            var pbImg = qrCodeEncoder.Encode(data, System.Text.Encoding.UTF8);
            return pbImg;
        }
    
        /// <summary>
        /// 给二维码中间添加图片(logo):将二维码作为背景图片,把小的logo图片放入背景图片的正中央。  
        /// </summary>
        /// <param name="backgroundImg">背景图片(此处为二维码)</param>
        /// <param name="logoImg">logo 图片</param>
        public static System.Drawing.Image CombinImage(System.Drawing.Image backgroundImg, System.Drawing.Image logoImg)
        {
            using (Graphics g = Graphics.FromImage(backgroundImg))
            {
    
                //画背景(二维码)图片,指定开始坐标为 x:0,y:0,指定背景图片宽高。
                g.DrawImage(backgroundImg, 0, 0, backgroundImg.Width, backgroundImg.Height);
    
                //logo 图片,重新设置图片宽高
                logoImg = ResizeImage(logoImg, 30, 30, 0);
    
                //logo四周刷一层红色边框
                //g.FillRectangle(System.Drawing.Brushes.Red, backgroundImg.Width / 2 - img.Width / 2 - 1, backgroundImg.Width / 2 - img.Width / 2 - 1, 32, 32);
    
                //画logo图片,设置坐标位置,让其居于正中央。
                g.DrawImage(logoImg, backgroundImg.Width / 2 - logoImg.Width / 2, backgroundImg.Width / 2 - logoImg.Width / 2, logoImg.Width, logoImg.Height);
    
                return backgroundImg;
            }
        }
    
        /// <summary>
        /// 重新设置图片的宽高
        /// </summary>
        /// <param name="bmp">原始Bitmap</param>
        /// <param name="newW">新的宽度</param>
        /// <param name="newH">新的高度</param>
        /// <param name="Mode">保留着,暂时未用</param>
        /// <returns>重新设置宽高后的图片</returns>
        public static System.Drawing.Image ResizeImage(System.Drawing.Image bmp, int newW, int newH, int Mode)
        {
            System.Drawing.Image b = new Bitmap(newW, newH);
            using (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);
                return b;
            }
        }
    }
  • 相关阅读:
    cv2.imwrite()指定图片存储路径问题
    fgets读取文件最后一行重复问题
    KEAZ128 时钟配置
    MinGW x64 for Windows安装
    [python] pygame安装与配置
    S32K144之时钟配置
    C/C++ scanf和gets 区别 , printf和puts区别
    堆排序
    约瑟夫问题
    Coursera 国内无法登陆问题
  • 原文地址:https://www.cnblogs.com/Fengyinyong/p/13522651.html
Copyright © 2011-2022 走看看