码迷,mamicode.com
首页 > Windows程序 > 详细

C# 生成二维码

时间:2020-10-27 11:12:22      阅读:34      评论:0      收藏:0      [点我收藏+]

标签:防止   for   重复   dev   com   exce   draw   二维码   get   

添加引用:

  ThoughtWorks.QRCode.dll

  System.Drawing

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System.Drawing;
using System.Text;
using ThoughtWorks.QRCode.Codec;
using System.IO;

namespace App.Web.APIControllers
{
    /// <summary>
    /// 生成二维码
    /// </summary>    public class QRCodeController : Controller
    {
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="enCodeString">要生成二维码的文字</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public string QRCode(string enCodeString)
        {
            Bitmap bt;
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字)
            qrCodeEncoder.QRCodeScale = 10;//大小(值越大生成的二维码图片像素越高)
            qrCodeEncoder.QRCodeVersion = 0;//版本(注意:设置为0主要是防止编码的字符串太长时发生错误)
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//错误效验、错误更正(有4个等级)
            qrCodeEncoder.QRCodeBackgroundColor = Color.White;//背景色
            qrCodeEncoder.QRCodeForegroundColor = Color.Black;//前景色
            bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);
            string filename = enCodeString;// "code";
            string file_path = AppDomain.CurrentDomain.BaseDirectory + "QRCode\\";
            string codeUrl = file_path + filename + ".jpg";

            //根据文件名称,自动建立对应目录
            if (!System.IO.Directory.Exists(file_path))
            {
                System.IO.Directory.CreateDirectory(file_path);
            }
            ////防止内容重复,导致名称重复问题,
            ////若要每次更新,可去掉本段代码 ↓↓↓↓↓
            //int i = 1;
            //while (System.IO.File.Exists(codeUrl))
            //{               
            //    string _filename = filename + "("+i+")";
            //    codeUrl = file_path + _filename + ".jpg";
            //    i++;
            //}
            ////   ↑↑↑↑↑↑↑

            bt.Save(codeUrl);//保存图片
            return codeUrl;
        }


        /// <summary>
        /// 生成二维码【方法二】
        /// </summary>
        /// <param name="qrCodeScale">尺寸4-15</param>
        /// <param name="qrCodeVersion">复杂级别3-12</param>
        /// <param name="qrCodeErrorCorrect">容错量"H","L","M","Q"</param>
        /// <param name="enCodeString">二维码信息</param>        
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public  string CreateEQcoder(int qrCodeScale, int qrCodeVersion, string qrCodeErrorCorrect, string enCodeString)
        {
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeScale = qrCodeScale;
            qrCodeEncoder.QRCodeVersion = qrCodeVersion;
            switch (qrCodeErrorCorrect)
            {
                case "H":
                    qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
                    break;
                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;
            }
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
            Bitmap bmPhoto = qrCodeEncoder.Encode(enCodeString, System.Text.Encoding.GetEncoding("UTF-8"));
            string fileName = enCodeString+"_"+ qrCodeScale+"_"+qrCodeVersion+"_"+ qrCodeErrorCorrect;
            string file_path = AppDomain.CurrentDomain.BaseDirectory + "QRCode\\";
            string savePath = file_path + fileName + ".jpg";
            try
            {
                if (!Directory.Exists(file_path))
                {
                    Directory.CreateDirectory(file_path);
                }

                //防止内容重复,导致名称重复问题,若要每次更新,可去掉本段代码 ↓↓↓↓↓
                int i = 1;
                while (System.IO.File.Exists(savePath))
                {
                    string _filename = fileName + "(" + i + ")";
                    savePath = file_path + _filename + ".jpg";
                    i++;
                }
                //↑↑↑↑↑↑↑
                bmPhoto.Save(savePath);
                bmPhoto.Dispose();
                return savePath ;
            }
            catch (Exception)
            {
                return "";
            }
            finally
            {
                bmPhoto.Dispose();
            }
        }
    }
}

原文链接:https://www.cnblogs.com/dyhao/p/11595714.html

C# 生成二维码

标签:防止   for   重复   dev   com   exce   draw   二维码   get   

原文地址:https://www.cnblogs.com/duhaoran/p/13879168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!