zoukankan      html  css  js  c++  java
  • 【彩色二维码】处理生成彩色二维码图片

    调用草料二维码的API接口

    https://cli.im/api/qrcode/code?text=***&mhid=***

    mhid:草料二维码的模版样式id,这里选择的是【vUbEWVm7mp0hPn0nLdc】,可以在草料的网站上生成模版:https://mh.cli.im/

    text:就是二维码字符串内容,可以是网址或其他自定义的string

    由于接口并没有直接返回二维码图片url,而是掺杂着html代码,所以需要处理接口返回的数据,正则出图片url

            /// <summary>
            /// 处理生成彩色二维码图片
            /// </summary>
            /// <param name="code"></param>
            /// <returns></returns>
            public string _GetColorfullQRCode(string code = "")
            {
    #if DEBUG
                code = "Hello Word!";
    #endif
                m_Trace.Trace("_GetColorfullQRCode code=" + code);
                if (string.IsNullOrWhiteSpace(code))
                    return "请输入二维码串";
    
                code = code.Replace("+", "-");
                code = HttpUtility.UrlEncode(code);
                string md5 = Utility.MD5(code);
                var cache = CacheHelper.Get(md5);
    
                if (null != cache && cache is string)
                {
                    string cacheResult = cache as string;
                    m_Trace.Trace("_GetColorfullQRCode cacheResult=" + cacheResult);
                    return cacheResult;
                }
    
                var mhids = new string[]{
                    "vUbEWVm7mp0hPn0nLdc"
                };
    
                string mhid = "";
                if (mhids.Length == 1)
                {
                    mhid = mhids[0];
                }
                else
                {
                    int seed = 2;
                    int.TryParse(DateTime.Now.ToString("ffff"), out seed);
                    var r = new Random(seed);
                    mhid = mhids[r.Next(0, mhids.Length - 1)];
                }
                string result = "";
                string url = "https://cli.im/api/qrcode/code?text=" + code + "&mhid=" + mhid;
                m_Trace.Trace("_GetColorfullQRCode url=" + url);
                string htmltext = KTHttpRequest._Get(url, false);
                //m_Trace.Trace("_GetColorfullQRCode htmltext=" + htmltext);
    
                string[] imgUrlList = GetHtmlImageUrlList(htmltext);
                if (null != imgUrlList && imgUrlList.Length > 0)
                {
                    result = "http:" + imgUrlList[0];
                    CacheHelper.Max(md5, result);
                }
                m_Trace.Trace("_GetColorfullQRCode result=" + result);
                return result;
            }    

    正则取出图片url的方法

            /// <summary> 
            /// 取得HTML中所有图片的 URL。 
            /// </summary> 
            /// <param name="sHtmlText">HTML代码</param> 
            /// <returns>图片的URL列表</returns> 
            private string[] GetHtmlImageUrlList(string sHtmlText)
            {
                // 定义正则表达式用来匹配 img 标签 
                Regex regImg = new Regex(@"<img[^<>]*?src[s	
    ]*=[s	
    ]*[""']?[s	
    ]*(?<imgUrl>[^s	
    ""'<>]*)[^<>]*?/?[s	
    ]*>", RegexOptions.IgnoreCase);
    
                // 搜索匹配的字符串 
                MatchCollection matches = regImg.Matches(sHtmlText);
                int i = 0;
                string[] sUrlList = new string[matches.Count];
    
                // 取得匹配项列表 
                foreach (Match match in matches)
                    sUrlList[i++] = match.Groups["imgUrl"].Value;
                return sUrlList;
            }
    
            #endregion

    最后得到了一长串的二维码url

    http://qr.api.cli.im/qr?data=F9GgzK99KhtObCUuiKsEZQ844D-LU%2F9Fy3fmjSUM%2FOBoDRBqwL4AabAW-L5f5QXMqIl3q3NhR83gINMkoexmmA%3D%3D&level=H&transparent=0&bgcolor=%23FFFFFF&forecolor=%2F%2Fstatic-develop.clewm.net%2Fcli%2Fimages%2Fbeautify%2Ftpl%2Ffg1.jpg&blockpixel=12&marginblock=2&logourl=&size=400&text=&logoshape=no&fontsize=46&fontfamily=msyh.ttf&fontcolor=%23000000&incolor=&outcolor=%23368af4&background=%2F%2Fstatic.clewm.net%2Fcli%2Fimages%2Fbeautify%2Ftpl%2Fbg1.png&qrcode_eyes=&wper=0.86&hper=0.86&lper=0.07&tper=0.07&eye_use_fore=1&qrpad=10&kid=cliim&key=ae4ec3d0e4fbcd224af775ba353bb868

    访问这个长连接就可以得到想要的彩色二维码

    搞定!

  • 相关阅读:
    OpenStack-Ocata版+CentOS7.6 云平台环境搭建 — 2.安装配置OpenStack基础服务
    OpenStack-Ocata版+CentOS7.6 云平台环境搭建 — 1.操作系统环境配置
    ssh免密登录
    PHP实现识别带emoji表情的字符串
    算法-PHP实现八大算法
    项目总结-第一次外包项目2-项目相关
    PHP.52-TP框架商城应用实例-前台4-商品详情页-面包屑导航、AJAX浏览历史
    PHP.51-TP框架商城应用实例-前台3-楼层推荐、TP框架雪崩问题
    PHP.50-TP框架商城应用实例-前台2-商品推荐
    PHP.49-TP框架商城应用实例-前台1-公共布局、制作首页
  • 原文地址:https://www.cnblogs.com/jhli/p/6113286.html
Copyright © 2011-2022 走看看