基本信息
源码名称:识别 身份证图片中的文字(C#源码)
源码大小:9.18M
文件格式:.rar
开发语言:C#
更新时间:2019-07-26
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
选择身份证 正面 和反面,识别身份证中的 姓名、性别、民族、出生、地址、号码、签发机关、有效期等
选择身份证 正面 和反面,识别身份证中的 姓名、性别、民族、出生、地址、号码、签发机关、有效期等
示例中的身份证原图如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tesseract;
namespace ICRS
{
public partial class Form1 : Form
{
private string appKey="mfM9NFqxwxWbMcIbRnYgUMTY";
private string appSec = "2EXBhpDZueL7kXQd1GI8PNrSb9S9q8j1";
private string token = "24.9d44ca2865c47e0e4487f825a8488b58.2592000.1566021773.282335-16835038";
private string pageStr = "";
public Form1()
{
InitializeComponent();
pageStr = this.Text;
GetAcctoken();
}
/// <summary>
/// 获取token
/// </summary>
private void GetAcctoken()
{
this.Text = pageStr "(初始化...)";
var url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={0}&client_secret={1}";
url = string.Format(url, appKey, appSec);
var res = Common.GetResponse(url);
var obj = Common.DeserializeObject(res);
if(obj==null)
{
this.Text = pageStr;
MessageBox.Show("网络联接失败", "错误18066831920");
return;
}
if(obj["access_token"] ==null)
{
this.Text = pageStr;
MessageBox.Show("网络联接失败", "错误18066831920");
return;
}
token = obj["access_token"].ToString();
this.Text = pageStr;
}
private string ImgToBase64String(Bitmap bitMap)
{
var imgData = (string)null;
using (var ms = new System.IO.MemoryStream())
{
//将图片对象转为内存流
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
//将内存流转为字节数字
var subf = ms.GetBuffer();
imgData = Convert.ToBase64String(subf);
}
return imgData;
}
/// <summary>
/// 上传身份证正面照片
/// </summary>
private void label9_Click(object sender, EventArgs e)
{
try
{
var ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
this.Text = pageStr "(处理中...)";
this.pictureBox1.ImageLocation = ofd.FileName;
this.label9.Visible = false;
var strbaser64 = ImgToBase64String(new Bitmap(ofd.FileName)); // 图片的base64编码
var host = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" token;
var res = Common.PostResponse(host, "id_card_side=front&image=" System.Web.HttpUtility.UrlEncode(strbaser64, System.Text.Encoding.UTF8));
var obj = Common.DeserializeObject(res);
if (obj == null)
{
this.Text = pageStr;
MessageBox.Show("处理失败", "错误18066831920");
return;
}
if (obj["words_result"] == null)
{
this.Text = pageStr;
MessageBox.Show("处理失败", "错误18066831920");
return;
}
if (obj["words_result"]["姓名"] != null)
{
this.txtName.Text = obj["words_result"]["姓名"]["words"].ToString();
}
if (obj["words_result"]["性别"] != null)
{
this.txtSex.Text = obj["words_result"]["性别"]["words"].ToString();
}
if (obj["words_result"]["民族"] != null)
{
this.txtNation.Text = obj["words_result"]["民族"]["words"].ToString();
}
if (obj["words_result"]["出生"] != null)
{
this.txtBirsday.Text = DateTime.ParseExact(obj["words_result"]["出生"]["words"].ToString(), "yyyyMMdd", null).ToString("yyyy年MM月dd日");
}
if (obj["words_result"]["住址"] != null)
{
this.txtAddress.Text = obj["words_result"]["住址"]["words"].ToString();
}
if (obj["words_result"]["公民身份号码"] != null)
{
this.txtCardNo.Text = obj["words_result"]["公民身份号码"]["words"].ToString();
}
this.Text = pageStr;
}
}
catch
{
MessageBox.Show("请选择正确的照片!", "提示18066831920");
}
}
/// <summary>
/// 上传身份证背面照片
/// </summary>
private void label10_Click(object sender, EventArgs e)
{
try
{
var ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
this.Text = pageStr "(处理中...)";
this.pictureBox2.ImageLocation = ofd.FileName;
this.label10.Visible = false;
var strbaser64 = ImgToBase64String(new Bitmap(ofd.FileName)); // 图片的base64编码
var host = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" token;
var res = Common.PostResponse(host, "id_card_side=back&image=" System.Web.HttpUtility.UrlEncode(strbaser64, System.Text.Encoding.UTF8));
var obj = Common.DeserializeObject(res);
if (obj == null)
{
this.Text = pageStr;
MessageBox.Show("处理失败", "错误18066831920");
return;
}
if (obj["words_result"] == null)
{
this.Text = pageStr;
MessageBox.Show("处理失败", "错误18066831920");
return;
}
if (obj["words_result"]["失效日期"] != null && obj["words_result"]["签发日期"] != null)
{
this.txtValidDate.Text = DateTime.ParseExact(obj["words_result"]["签发日期"]["words"].ToString(), "yyyyMMdd", null).ToString("yyyy.MM.dd") "至" DateTime.ParseExact(obj["words_result"]["失效日期"]["words"].ToString(), "yyyyMMdd", null).ToString("yyyy.MM.dd");
}
if (obj["words_result"]["签发机关"] != null)
{
this.txtFzjg.Text = obj["words_result"]["签发机关"]["words"].ToString();
}
this.Text = pageStr;
}
}
catch
{
MessageBox.Show("请选择正确的照片!", "提示18066831920");
}
}
}
}