In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use .net code to deal with pictures in the development of Wechat public platform. I think it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
For example, someone takes a picture of our public WeChat account and sends it to us, and then we process the photo, such as ocr recognition (which will then be reduced to this example), or face recognition, or photo forensics. These functions are very useful. So we need to analyze this process now. Wechat platform certainly can not help us with OCR or face recognition and other functions, to do these functions first to get pictures! The photos taken by users are first uploaded to wenxin's server, and then there is a mediaID, which we can use to download to our own server and process, send the results to Wechat platform, and finally feedback from Wechat platform to users (followers). Wechat's development documentation has given the way to download resources, I transformed into. Net, as follows:
/ / download and save the multimedia file, and return to the multimedia save path / public string GetMultimedia (string 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;} catch (Exception ex) {savepath = ex.ToString ();}} return file;}
The above two parameters are easy to understand, the first is ACCESS_TOKEN, which has been mentioned a lot before, and the second is the resource id on the Wechat server, that is, mediaID. If we want to download resources on Wechat servers, we must know id. But how did MEDIA_ID come into being? I'll first modify the previous message entity class and add the MediaId attribute
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 modify GetWxMessage () to 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 modify the code accepted by the message, the customer sends a photo to the Wechat platform, the program detects the picture, and then according to MediaId, call the GetMultimedia method to download the picture to their own server. For the rest of the job, you can do whatever you want.
The example just now seems to be that the user (follower) sends a picture and then sends it to our server through the Wechat platform. There is also a situation in which the user sends a user name: for example, "hemeng", and then I need to call the picture of the hemeng profile picture that already exists on the server and give it back to the user. What should I do? How to send our pictures to the Wechat platform and then to the users? We used the upload method:
/ upload multimedia files and return MediaId / public string UploadMultimedia (string ACCESS_TOKEN, string Type) {string result = "; string wxurl =" address of https://cache.yisu.com/upload/information/20201208/265/31136";( 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 (result); result = _ mode.media_id;} catch (Exception ex) {result = "Error:" + ex.Message } WriteLog ("upload MediaId:" + result); return result;}
If the second parameter is the image "image", you can refer to Wechat's documentation. The return value of the function is a MediaId, so you can send it to the customer using the function that sends the image. The function to send the picture is as follows:
Protected string sendPicTextMessage (Msg _ mode, string MediaId) {string res = string.Format (@ "{2}", _ mode.FromUserName, _ mode.ToUserName) DateTime.Now, MediaId) Return res;}
The operation of other videos and voice is similar, so it is no longer redundant. With this knowledge, can we do a lot of applications? Of course it is, but our code is not optimized enough, the structure is unreasonable, there is no hurry, we will introduce it gradually, because we have not fully understood the powerful functions of Wechat.
This is the end of the article on "how to use .net code to deal with pictures in the development of Wechat public platform". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.