C# 调百度图片识别接口

前端

<div class="list-wrapper">
                <label class="label">资产图片</label>
                <div class="input-wrapper">
                    <asp:FileUpload runat="server" ID="loginUpload" onchange="loginPath.value=this.value" Style="display: none" />
                    <input id="loginPath" class="filePath" type="text" readonly="readonly" runat="server" />
                    <input id="loginBtn" class="uploadBtn" type="button" value="上传图片" onclick="loginUpload.click()" />
                </div>
</div>
<div class="btnWrapper">
                 <asp:Button ID="Button_GetData" runat="server" CssClass="bttn btnSubmit" Text="文字识别" OnClick="Button_GetData_Click"  />
</div>

用js将图片文件上传到项目路径下,监听文本框文件路径变化,点击按钮触发事件

 protected void Button_GetData_Click(object sender, EventArgs e)
        {
            //OpenFileDialog ofd = new OpenFileDialog();
            //string _path = ofd.FileName;
            string tips = string.Empty;
            string fileName_login = loginPath.Value.Trim();
            //上传图片处理
            if (loginUpload.FileName != "")
            {
                bool login_res = ImagesHandle.uploadImg(loginUpload, out fileName_login, out tips);
                string path = "../../images/icon/" + fileName_login;
                BankBindZheng(path);
                if (!login_res)
                {
                    MessageBox.ResponseScript(this.Page, "dialogPackage.multilevel('" + tips + "',function(){},1999)");
                    return;
                }
            }
        }

判断上传的图片是否正确

public static class ImagesHandle
    {
        //上传图片
        public static bool uploadImg(FileUpload hc, out string fileName, out string tips)
        {
            bool res = false;
            //上传图片处理
            string fileType = string.Empty;
            string saveName = string.Empty;
            tips = string.Empty;
            fileName = string.Empty;
            if (hc.FileName != "")
            {
                fileName = Utils.GuidString();
                fileType = Path.GetExtension(hc.FileName).ToString().ToLower();  //文件扩展名
                string savePath = string.Empty;
                if (fileType == ".jpeg" || fileType == ".jpg" || fileType == ".png")
                {
                    var dirPath = HttpContext.Current.Server.MapPath("../../images/icon/");
                    if (!Directory.Exists(dirPath))
                    {
                        Directory.CreateDirectory(dirPath);
                    }
                    savePath = dirPath + fileName + fileType;                   //储存路径
                    try
                    {
                        hc.SaveAs(savePath);
                        fileName += fileType;
                        res = true;
                        tips = "图片保存成功!";
                    }
                    catch (Exception ex)
                    {
                        Log.Write(ex);
                        res = false;
                        tips = "图片保存失败!";
                    }
                }
                else
                    tips = "图片类型不正确,请重新选择图片";
            }
            else
            {
                res = true;
            }
            return res;
        }
    }
 protected void BankBindZheng(string pName)
        {
            //自己去百度申请appid和key
            var APP_ID = "";
            var API_KEY = "";
            var SECRET_KEY = "";
            var images = System.Web.HttpContext.Current.Server.MapPath(pName);
            var image = File.ReadAllBytes(images);

            var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);
           
            try
            {
                //银行卡识别接口,这里的Bankcard式接口名,不同证件可以更改
                var result = client.Bankcard(image);

                // 如果有可选参数
                var options = new Dictionary<string, object>{
                        {"detect_direction", "true"},
                        {"detect_risk", "false"}
                    };
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);

            }

下面的方法是对 接口返回数据的解析;

public static bool GetValue(string json, string key, out string value)
        {
            //解析失败的默认返回值
            value = "";
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            try
            {
                Dictionary<string, object> obj_json = serializer.DeserializeObject(json) as Dictionary<string, object>;
                if (obj_json.ContainsKey(key))
                {
                    value = serializer.Serialize(obj_json[key]);
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool GetValue(string json, int index, out string value)
        {
            //解析失败的默认返回值
            value = "";
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            try
            {
                object[] obj_json = serializer.DeserializeObject(json) as object[];
                if (obj_json.Length > index)
                {
                    value = serializer.Serialize(obj_json[index]);
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值