zoukankan      html  css  js  c++  java
  • C# 二维码生成——QRCode

    C#二维码生成,这里使用开源的ThoughtWorks.QRCode.dll库。

    步骤:

    1.下载ThoughtWorks.QRCode.dll库文件,并引用到项目中。

    2.创建QRCodeHandler.cs二维码处理类

    /// 二维码处理类
        /// 创建人:杨武
        /// 创建日期:2015-01-22
        /// </summary>
        public class QRCodeHandler
        {
            /// <summary>
            /// 生成二维码
            /// </summary>
            /// <param name="data">数据</param>
            /// <param name="filePath">文件保存路径(绝对路径)</param>
            /// <param name="hasLogo">二维码图标是否添加图片</param>
            /// <param name="logoFilePath">图片的路径(绝对路径)</param>
            /// <returns>true--成功;false--失败</returns>
            public static bool CreateQRcode(string data, string filePath, bool hasLogo = false, string logoFilePath = "")
            {
                bool result = false;
                QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
                qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//二维码编码(Byte、AlphaNumeric、Numeric)
                qrCodeEncoder.QRCodeScale = 4;//二维码尺寸(Version为0时,1:26x26,每加1宽和高各加25
                qrCodeEncoder.QRCodeVersion = 8;//二维码密集度0-40
                qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//二维码纠错能力(L:7% M:15% Q:25% H:30%)
                try
                {
                    Image card = qrCodeEncoder.Encode(data, Encoding.UTF8);
    
                    using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        card.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
                        fs.Close();
                    }
    
                    if (hasLogo)
                    {
                        AddLogo(logoFilePath, card, filePath);
                    }
    
                    card.Dispose();
                    result = true;
                }
                catch (Exception ex)
                {
                    result = false;
                }
                return result;
            }
    
            /// <summary>
            /// 二维码图标中添加图片
            /// </summary>
            /// <param name="logoFilePath">图片路径</param>
            /// <param name="cord">二维码</param>
            /// <param name="cardSavePath">二维码保存路径</param>
            private static void AddLogo(string logoFilePath, Image cord, string cardSavePath)
            {
                if (string.IsNullOrWhiteSpace(logoFilePath) || cord == null || File.Exists(logoFilePath) == false)
                {
                    return;
                }
    
                using (Image copyImage = Image.FromFile(logoFilePath))
                {
                    using (Graphics g = Graphics.FromImage(cord))
                    {
                        int x = cord.Width / 2 - copyImage.Width / 2;
                        int y = cord.Height / 2 - copyImage.Height / 2;
                        g.DrawImage(copyImage, new Rectangle(x, y, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
                    }
                    cord.Save(cardSavePath);
                }
            }
        }
    View Code

    3.调用

    string str = "http://hao.360.cn/?src=lm&ls=n3335c8fd8f";
                string filtPath = Server.MapPath("~/Images/1.jpg");
                string logoFilePath = Server.MapPath("~/Images/faceIcon.png");
                QRCodeHandler.CreateQRcode(str, filtPath, true, logoFilePath);
    View Code
  • 相关阅读:
    Express ejs 3.* layout.ejs
    old header
    mac 命令行 安装 需要管理员 权限
    Understanding the Debug Log
    insufficient_access_on_cross_reference_entity APEX / Salesforce
    custom list view
    exam help
    Backbone.js Wine Cellar 教程
    理解RESTful架构
    SpringCloud入门之应用程序上下文服务(Spring Cloud Context)详解
  • 原文地址:https://www.cnblogs.com/ywblog/p/4243276.html
Copyright © 2011-2022 走看看