排名
3
文章
317
粉丝
22
评论
14
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术

code:
- /// <summary>
- /// 图片无损放大
- /// </summary>
- /// <returns></returns>
- public ActionResult ImageQualityEnhance()
- {
- //生成一个不重复的随机数作为图片的新名字
- string newName = Guid.NewGuid().ToString();
- //把-替换为空
- newName = newName.Replace("-", "");
- string host = "https://aip.baidubce.com/rest/2.0/image-process/v1/image_quality_enhance?access_token=" + AccessToken.getAccessToken().access_token;
- string result = BaiduAIBase64Tool.Invoke(host, "c:/img/5b4b48c1Nc91949da.jpg");
- ImageQualityEnhanceModel imageQualityEnhanceModel = JsonConvert.DeserializeObject<ImageQualityEnhanceModel>(result);
- //存储放大后的图片
- base64TOImg(imageQualityEnhanceModel.image, Server.MapPath("~/img/" + newName + ".jpg"));
-
- return View();
- }
-
- /// <summary>
- /// 把base64转化成图片并保存
- /// </summary>
- /// <param name="base64str"></param>
- private void base64TOImg(String base64str, string url)
- {
- Byte[] streamByte = Convert.FromBase64String(base64str);
- System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
- System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
- img.Save(url);
- }
model:
- public class ImageQualityEnhanceModel
- {
- public string log_id { get; set; }
- public string image { get; set; }
- }
调用工具类:
- public class BaiduAIBase64Tool
- {
- public static string Invoke(string url, string imgurl)
- {
- //string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard?access_token=" + "sdfdsf";
- Encoding encoding = Encoding.Default;
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- request.Method = "post";
- request.KeepAlive = true;
- // 图片的base64编码
- string base64 = getFileBase64(imgurl);
- String str = "multi_detect=true&image=" + HttpUtility.UrlEncode(base64);
- byte[] buffer = encoding.GetBytes(str);
- request.ContentLength = buffer.Length;
- request.GetRequestStream().Write(buffer, 0, buffer.Length);
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- string result = reader.ReadToEnd();
- return result;
- }
-
- private static String getFileBase64(String fileName)
- {
- FileStream filestream = new FileStream(fileName, FileMode.Open);
- byte[] arr = new byte[filestream.Length];
- filestream.Read(arr, 0, (int)filestream.Length);
- string baser64 = Convert.ToBase64String(arr);
- filestream.Close();
- return baser64;
- }
- }
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)
评价