【彩色二维码】处理生成彩色二维码图片

调用草料二维码的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\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", 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

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

搞定!

posted @ 2016-11-29 13:41  0539  阅读(4888)  评论(0编辑  收藏  举报