微信小程序获取普通二维码

Posted 忧郁的小学生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信小程序获取普通二维码相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using LitJson;
using System.Drawing;
using System.Drawing.Imaging;

namespace WetChat
{
  public  class Chat
    {
        public string Path;

        public string Width;

        public string Savepath;


        public string Codes;
        public Chat(string savepath,string codes)
        {
            this.Path = "pages/index/index?codes="+ codes;
            this.Savepath = savepath+"qr_"+codes+".png";
            this.Codes = codes;
        }
        /// <summary>
        /// 获取acesstoken
        /// </summary>
        /// <returns></returns>
        public string GetAccess_Token()
        {
            string urls = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&secret=";
            HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(urls);  //请求api
            HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            string respText = "";
            using (Stream resStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(resStream, Encoding.Default);
                respText = reader.ReadToEnd();
                resStream.Close();
            }
            javascriptSerializer Jss = new JavaScriptSerializer();
            Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText);
            //通过键access_token获取值
            return respDic["access_token"].ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <param name="width"></param>
        /// <param name="savepath"></param>
        public void GetWxCode()
        {
            string url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + GetAccess_Token();
            HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(url);  //创建url
            request.Method = "POST";
            request.ContentType = "application/json;charset=UTF-8";
            JsonData data = new JsonData();
            data["path"] = this.Path;
            data["width"] = this.Width;

            string json = data.ToJson();
            byte[] load = Encoding.UTF8.GetBytes(json);
            request.ContentLength = load.Length;
            Stream writer = request.GetRequestStream();
            writer.Write(load, 0, load.Length);


            HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            Stream s = response.GetResponseStream();
            byte[] mg = StreamToBytes(s);
            Image imgs = CaptureImage(mg);
            byte[] mgs = PhotoImageInsert(imgs);
            if (File.Exists(this.Savepath))
            {
                File.Delete(this.Savepath);
                File.WriteAllBytes(this.Savepath, mgs);


            }
            else
            {
             
                File.WriteAllBytes(this.Savepath, mgs);
              


            }
           

        }

        public static byte[] StreamToBytes(Stream stream)
        {

            List<byte> bytes = new List<byte>();
            int temp = stream.ReadByte();
            while (temp != -1)
            {
                bytes.Add((byte)temp);
                temp = stream.ReadByte();
            }
            return bytes.ToArray();

        }

        /// <summary>
        /// 第二部截取:图片转换为文件流
        /// </summary>
        /// <param name="bytes"></param>
        public System.Drawing.Image CaptureImage(byte[] bytes)
        {
            Image formimage = ReturnPhoto(bytes);
            Bitmap bitmap = new Bitmap(440, 440);
            Point p = new Point(0, 0);

            Size s = new Size(455, 455);
            Graphics gra = Graphics.FromImage(bitmap);
            gra.DrawImage(formimage, -15, -15, new Rectangle(p,s), GraphicsUnit.Pixel);
            Image saveimage = Image.FromHbitmap(bitmap.GetHbitmap());

            return saveimage;


        }

        /// <summary>
        /// 第三部:图片转换文件流
        /// </summary>
        /// <param name="imgPhoto"></param>
        /// <returns></returns>
        public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
        {
            MemoryStream mstream = new MemoryStream();
            imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] byData = new Byte[mstream.Length];
            mstream.Position = 0;
            mstream.Read(byData, 0, byData.Length); mstream.Close();
            return byData;
        }
        /// <summary>
        /// 第一步:将文件流转换为image
        /// </summary>
        /// <param name="streamByte"></param>
        /// <returns></returns>
        public System.Drawing.Image ReturnPhoto(byte[] streamByte)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
            return img;
        }












    }
}

  普通二维码有下面的微信的logo 所以截取

  分了三部:

  第一:将文件流转换为image

  第二:将image截取

  第三:将image转换为文件流

  

  生成100个编码总共用时40s(着实有点多,转换哪里太浪费了)

  

  

以上是关于微信小程序获取普通二维码的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序二维码怎么获取二维码

关于.NET HttpClient方式获取微信小程序码(二维码)

微信小程序-扫码进入小程序获取二维码携带参数

微信小程序码如何弄怎么生成 获取小程序二维码方法

微信小程序代码片段

微信小程序 获取小程序码和二维码java接口开发