Micro-trust public platform for the development of processing pictures. NET code resolution _ Practical Skills

Source: Internet
Author: User
Tags cdata datetime

For example, someone sends a picture of our public micro-signal, and then we process the photo, such as OCR recognition (and then down to this example), or face recognition, or photo forensics, which is quite useful. Then we need to analyze this process now. Micro-letter platform must not help us OCR or face recognition functions, to do these functions first to get pictures! The photos taken by the user were first uploaded to the wenxin server, and then there was a mediaid that we could use to download to our own server and then process the results to the micro-trust platform, which is ultimately fed back to the user (the followers). The micro-letter development document has given the way to download resources, I have changed to. NET, as follows: &NBSP

<summary>///Download Save multimedia file, return to multimedia save path///</summary>///<param name= "Access_token" ></param&gt
  ; <param name= "media_id" ></param>///<returns></returns> public string Getmultimedia (Strin G Access_token, String media_id) {string file = string.
    Empty; String content = String.
    Empty; String strpath = String.
    Empty; String Savepath = String.
    Empty; String sturl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + Access_token + "&media_id=" +

    media_id;

    HttpWebRequest req = (HttpWebRequest) httpwebrequest.create (Sturl); Req.
    method = ' Get '; using (WebResponse WR = req. GetResponse ()) {HttpWebResponse Myresponse = (httpwebresponse) req.

      GetResponse ();
      strpath = MyResponse.ResponseUri.ToString ();
      Writelog ("Receive Category://" + Myresponse.contenttype);
      WebClient mywebclient = new WebClient (); Savepath = Server.MapPath ("image") + "\" + DateTime.Now.ToString ("yyyyMmddhhmmssfff ") + (new Random ()). Next (). ToString ().
      Substring (0, 4) + ". jpg";
      Writelog ("Path://" + Savepath); try {mywebclient.
        DownloadFile (strpath, Savepath);
      File = Savepath; The catch (Exception ex) {Savepath = ex.
      ToString ();
  } return file;

 }

The above two parameters are well understood, the first is Access_token, said a lot before, the second is in the Micro-trust server resource ID, that is, MediaID. If we want to download the resources on the micro-trust server, we always need to know the ID. But how did media_id come into being? I first change the message entity class before, add the MediaID property

 Class Wxmessage 
  {public 
    string Fromusername {get; set;} 
    public string Tousername {get; set;} 
    public string Msgtype {get; set;} 
    public string EventName {get; set;} 
    public string Content {get; set;}
    public string Recognition {get; set;}
    public string MediaId {get; set;}
    public string Eventkey {get; set;} 
  }

Then change the Getwxmessage () and assign a value to MediaID.

Private Wxmessage Getwxmessage () {wxmessage wx = new Wxmessage ();
     StreamReader str = new StreamReader (Request.inputstream, System.Text.Encoding.UTF8);
     XmlDocument xml = new XmlDocument (); Xml.
     Load (str); Wx. Tousername = XML. selectSingleNode ("xml"). selectSingleNode ("Tousername").
     InnerText; Wx. Fromusername = XML. selectSingleNode ("xml"). selectSingleNode ("Fromusername").
     InnerText; Wx. Msgtype = XML. selectSingleNode ("xml"). selectSingleNode ("Msgtype").
     InnerText; if (WX). Msgtype.trim () = = "Text") {WX. Content = XML. selectSingleNode ("xml"). selectSingleNode ("Content").
     InnerText; } if (WX. Msgtype.trim () = = "Event") {WX. EventName = XML. selectSingleNode ("xml"). selectSingleNode ("Event").
       InnerText; Wx. Eventkey = XML. selectSingleNode ("xml"). selectSingleNode ("Eventkey").
     InnerText; } if (WX. Msgtype.trim () = = "Voice") {WX. Recognition = XML. selectSingleNode ("xml"). selectSingleNode ("Recognition"). InnErtext; } if (WX. Msgtype.trim () = = "image") {wx. MediaId = XML. selectSingleNode ("xml"). selectSingleNode ("MediaId").
    InnerText;
   return WX;

 }

If we are modifying the message accepted code, we can do it, the customer sends a photo to the micro-platform, the program detects the picture, and then, according to MediaID, calls the Getmultimedia method to download the picture to its own server. The work behind, you want to do what.
  Just like the example of the user (the concern), send a picture, and then through the micro-trust platform to our server, there is a situation where the user sends a username: for example, "Hemeng", and then I need to call the existing server in the Hemeng avatar picture feedback to the user, What about this? How do we pass our pictures to the micro-trust platform and pass it on to the user? We used the Upload method:  

<summary>///upload multimedia file, return MediaId///</summary>///<param name= "Access_token" ></param> <param name= "Type" ></param>///<returns></returns> public string Uploadmultimedia (stri
    Ng Access_token, String Type) {string result = "";
    String wxurl = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" + Access_token + "&type=" + type;
    string filepath = Server.MapPath ("image") + "\\hemeng80.jpg";(the address of the local server) writelog ("Upload path:" + filepath);
    WebClient mywebclient = new WebClient ();
    Mywebclient.credentials = CredentialCache.DefaultCredentials;
      try {byte[] Responsearray = Mywebclient.uploadfile (Wxurl, "POST", filepath);
      result = System.Text.Encoding.Default.GetString (Responsearray, 0, responsearray.length);
      Writelog ("Upload result:" + result);
      UPLOADMM _mode = jsonhelper.parsefromjson<uploadmm> (result);
    result = _mode.media_id; catch (ExceptiOn Ex} {result = ' Error: ' + ex.
    message;
    } writelog ("Upload mediaid:" + result);
  return result;
 }

The second parameter, if it is a picture "image", can refer to the document of the micro-letter. The return value of the function is a mediaid so that you can use the function that sends the picture, send to the customer, the function that sends the picture is as follows:

Protected string Sendpictextmessage (Msg _mode, string MediaId)
  {
    string res = string. Format (@ "<xml>
                      <tousername><![ Cdata[{0}]]></tousername>
                      <fromusername><![ cdata[{1}]]></fromusername>
                      <CreateTime>{2}</CreateTime>
                      <msgtype><![ cdata[image]]></msgtype>
                      <Image>
                      <mediaid><![ cdata[{3}]]></mediaid>
                      </Image>
                  </xml> ",
      _mode. Fromusername, _mode. Tousername, DateTime.Now, MediaId);

    return res;
  }

Other video, voice operations are similar, it is no longer redundant introduction. With this knowledge, can we do a lot of application? Of course yes, but our code is not optimized, the structure is unreasonable, do not worry, we will gradually introduce, because we have not fully understand the powerful function of micro-letter.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.