排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术
分类:
人工智能
html:
<div style="margin-top:50px"> <form method="post" action="/home/DoGeneralBasic" enctype="multipart/form-data"> <table> <tr> <td>图书封面:</td> <td><input type="file" onchange="selectImage(this.files)" accept=""></td> </tr> <tr> <td>书名:</td> <td><input type="text" name="bookname" id="bookname" /></td> </tr> <tr> <td>介绍:</td> <td> <textarea></textarea> </td> </tr> <tr> <td> <input type="submit" value="上传" /> </td> </tr> </table> </form> </div>
js:
<script> function selectImage(files) { let reader = new FileReader(); let file = files[0]; reader.readAsDataURL(file); reader.onload = function () { $.post('/Home/DoGeneral_BasicOnTime', { filecontent: reader.result }, function (result) { //把识别的结果放入输入框 $("#bookname").val(result); }); } } </script>
后台:
/// <summary> /// 通用文字识别即时验证,对应的后台方法 /// </summary> /// <returns></returns> public ActionResult DoGeneral_BasicOnTime() { string base64 = Request.Form["filecontent"]; #region base64需要把前面的申明去掉(当然前缀这个也可以放在前端处理) string base64img = Regex.Replace(base64, "data:image/.*;base64,", ""); #endregion //注意我们token要重写创建一个应用不然没有权限 string token = "你的token"; string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + token; string result = BaiduAIBase64Tool2.Invoke(base64img, host); GeneralBasicModel generalBasicModel = JsonConvert.DeserializeObject<GeneralBasicModel>(result); //给TempData设置内容 //TempData["generalBasicModel"] = generalBasicModel; object content = ""; if (generalBasicModel.words_result_num == 0) { content = "没有识别出来任何内容"; return View(); } if (generalBasicModel.words_result_num == 1) //说明只识别出来一个内容 { content = generalBasicModel.words_result[0].words; } else //说明只识别出来的内容大于1,合并两个内容 { content = generalBasicModel.words_result[0].words + "." + generalBasicModel.words_result[1].words; } return Json(content); }
调用百度AI工具类:
public class BaiduAIBase64Tool2 { public static string Invoke(string base64, string url) { //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 str = "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; } }
欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739
评价