Net Core教程

C# - 二维码QRCode

本文主要是介绍C# - 二维码QRCode,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1. 先下载nuget 包: ThoughtWorks.QRCode.DLL

2.   控制器(Controller)生成二维码数据流;HTML页面展示 二维码图片

 

2.1 HTML页面

 

<div class="text-center">
    <img id="erWeiMa" src="../../Driver/GetErWeiMa?ID=@ViewBag.vgamTaskInfo.ID" style="width: 200px; height: 200px; " />
</div>

2.2 二维码生成

        #region 生成二维码
        /// <summary>
        /// 获得二维码
        /// </summary>
        /// <param name="ID">身份证号</param>
        /// <returns></returns>
        public FileContentResult GetErWeiMa(string ID)
        {
            GenerateQRCode qr = new GenerateQRCode();
            string filePath = "/Upload/SamplingImg\\";
            string logoFilePath = "";//Logo路径50*50
            if (!System.IO.Directory.Exists(filePath))
            {
                System.IO.Directory.CreateDirectory(filePath);
            }
            filePath = filePath + "H" + ID + ".jpg";//二维码文件名
            qr.CreateQRCode(ID, "Byte", 5, 0, "Q", filePath, false, logoFilePath);  //生成
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(filePath);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return File(ms.ToArray(), "image/jpeg");
        }
        #endregion

        #region 二维码生成方法
        public class GenerateQRCode
        {
            /// <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
                {
                    System.Drawing.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();

                    //二维码内添加Logo
                    if (hasLogo)
                    {
                        //System.Drawing.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);
                        //image.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
                        //copyImage.Dispose();

                        image = CombinImage(image, logoFilePath);
                        image.Save(filePath);
                    }
                    image.Dispose();

                }
                catch
                {
                    result = false;
                }
                return result;
            }

            /// <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 != 50 || img.Width != 50)
                {
                    img = KiResizeImage(img, 50, 50, 0);
                }
                Graphics g = Graphics.FromImage(imgBack);

                g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);

                g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height);
                g.Dispose();
                img.Dispose();
                return imgBack;
            }


            /// <summary>    
            /// Resize图片    
            /// </summary>    
            /// <param name="bmp">原始Bitmap</param>    
            /// <param name="newW">新的宽度</param>    
            /// <param name="newH">新的高度</param>    
            /// <param name="Mode">保留着,暂时未用</param>    
            /// <returns>处理以后的图片</returns>    
            public static Image KiResizeImage(Image bmp, int newW, int newH, int Mode)
            {
                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;
                }
            }
        }

        #endregion

 

相关链接

【js 生成二维码】 https://www.cnblogs.com/best/p/9182702.html

【wonform  生成二维码】  https://mp.weixin.qq.com/s/CDBkP3ub3ht0P4rf8dSj4g

https://www.cnblogs.com/lyl6796910/p/14216013.html

 

这篇关于C# - 二维码QRCode的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!