[點(diǎn)晴永久免費(fèi)OA]C#百度OCR身份證圖片識別提取信息
當(dāng)前位置:點(diǎn)晴教程→點(diǎn)晴OA辦公管理信息系統(tǒng)
→『 經(jīng)驗分享&問題答疑 』
之前寫了一篇利用百度OCR進(jìn)行本地和網(wǎng)絡(luò)圖片識別文字的文章,點(diǎn)擊查看。今天補(bǔ)充識別身份證圖片的文章及demo 源碼地址:https://download.csdn.net/download/horseroll/10922409 效果圖展示: 請求參數(shù): 返回參數(shù):
1.先引用AIPSDK.dll和Newtonsoft.Json.dll這兩個DLL 然后在全局變量中聲明變量,在窗體加載事件中new個類,傳入API_KEY和SECRET_KEY string API_KEY = "cohIahxAt7HveHLYSHYK6G5N"; string SECRET_KEY = "e8SAsDIWSK9NPUKviYiPQNlfaVDXQSY5"; private void frm_IDCardImgToTxt_Load(object sender, EventArgs e) { client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超時時間 }
2.傳入圖片地址,識別圖片,返回識別結(jié)果 /// <summary> /// 身份證識別 /// </summary> /// <param name="filename"></param> /// <returns></returns> public string IdcardDemo(string filename) { bool cardjiaodu = cbjiaodu.Text == "true" ? true : false; string cardzhengfan = cbzhengfan.Text == "front" ? "front" : "back"; bool cardfengxian = cbfengxian.Text == "true" ? true : false; var image = File.ReadAllBytes(filename); var idCardSide = "back"; // 調(diào)用身份證識別,可能會拋出網(wǎng)絡(luò)等異常,請使用try/catch捕獲 var result = client.Idcard(image, idCardSide); Console.WriteLine(result); // 如果有可選參數(shù) var options = new Dictionary<string, object>{ {"detect_direction", cardjiaodu.ToString()}, {"detect_risk", cardfengxian.ToString()}, {"id_card_side",cardzhengfan.ToString() } }; // 帶參數(shù)調(diào)用身份證識別 result = client.Idcard(image, idCardSide, options); return result.ToString(); }
3.識別后的json內(nèi)容進(jìn)行輸出 /// <summary> /// 在界面上顯示 /// </summary> /// <param name="returnstr">json內(nèi)容</param> public void WriteTo(string returnstr) { try { ReturnJsonForCard rj = CYQ.Data.Tool.JsonHelper.ToEntity<ReturnJsonForCard>(returnstr); AReturnJsonForCard acj = CYQ.Data.Tool.JsonHelper.ToEntity<AReturnJsonForCard>(rj.words_result); string resultstr = ""; resultstr += "姓名:"+acj.姓名.words + "\r\n"+ "性別:" + acj.性別.words + "\r\n"+ "民族:" + acj.民族.words + "\r\n"+ "出生:" + acj.出生.words + "\r\n"+ "住址:" + acj.住址.words + "\r\n"+ "公民身份號碼:" + acj.公民身份號碼.words + "\r\n"; string card_direction = ""; if(rj.direction=="-1") { card_direction = "未定義"; } else if (rj.direction == "0") { card_direction = "正向"; } else if (rj.direction == "1") { card_direction = "逆時針90度"; } else if (rj.direction == "2") { card_direction = "逆時針180度"; } else if (rj.direction == "3") { card_direction = "逆時針270度"; } resultstr += "圖像方向:" + card_direction + "\r\n";
string card_image_status = ""; if(rj.image_status=="normal") { card_image_status = "識別正常"; } else if(rj.image_status == "reversed_side") { card_image_status = "身份證正反面顛倒"; } else if (rj.image_status == "non_idcard") { card_image_status = "上傳的圖片中不包含身份證"; } else if (rj.image_status == "blurred") { card_image_status = "身份證模糊"; } else if (rj.image_status == "other_type_card") { card_image_status = "其他類型證照"; } else if (rj.image_status == "over_exposure") { card_image_status = "身份證關(guān)鍵字段反光或過曝"; } else if (rj.image_status == "unknown") { card_image_status = "未知狀態(tài)"; }
resultstr += "識別狀態(tài):" + card_image_status + "\r\n";
string card_risk_type = ""; if (rj.risk_type == "normal") { card_risk_type = "正常"; } else if(rj.risk_type == "copy") { card_risk_type = "復(fù)印件"; } else if (rj.risk_type == "temporary") { card_risk_type = "臨時身份證"; } else if (rj.risk_type == "screen") { card_risk_type = "翻拍"; } else if (rj.risk_type == "unknown") { card_risk_type = "其他未知情況"; } resultstr += "識別身份證類型:" + card_risk_type + "\r\n";
resultstr += "圖片編輯工具:" + rj.edit_tool + "\r\n"; richTextBox1.Clear(); richTextBox1.AppendText(resultstr); } catch (Exception ex) { MessageBox.Show("解析出錯,出錯原因:" + ex.Message); } }
4.json字符串的類 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace 百度OCR { public class ReturnJsonForCard { public string log_id { get; set; } public string direction { get; set; } public string image_status { get; set; } public string risk_type { get; set; } public string edit_tool { get; set; } public string words_result_num { get; set; } public string words_result { get; set; } } public class AReturnJsonForCard { public BReturnJsonForCard 住址 { get; set; } public BReturnJsonForCard 出生 { get; set; } public BReturnJsonForCard 姓名 { get; set; } public BReturnJsonForCard 公民身份號碼 { get; set; } public BReturnJsonForCard 性別 { get; set; } public BReturnJsonForCard 民族 { get; set; } } public class BReturnJsonForCard { public string location { get; set; } public string words { get; set; } } } 該文章在 2020/4/24 17:24:57 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |