注册百度账号及新增一个应用,获取百度API所需的AppID,API Key,Secret Key三个参数。
- 访问http://ai.baidu.com,使用百度账号登录后,出现如下界面


- 下载百度SDK

- 集成百度SDK
- 身份证中文字识别代码如下,简单几行代码就搞定,具体如下:
代码展示
| package com.chinalife.maced_bdsdk.util; |
| |
| import com.baidu.aip.ocr.AipOcr; |
| import org.json.JSONObject; |
| |
| import java.util.HashMap; |
| |
| |
| |
| |
| |
| public class Sample { |
| |
| public static final String APP_ID = "23552975"; |
| public static final String API_KEY = "CS6wkhbrmjhiu9KUx2f6E51t"; |
| public static final String SECRET_KEY = "useckvAI6kfMLhBDGOjwfAVrpgSNNI5o"; |
| |
| |
| |
| |
| public static void main(String[] args) { |
| |
| |
| AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY); |
| |
| |
| client.setConnectionTimeoutInMillis(2000); |
| client.setSocketTimeoutInMillis(60000); |
| |
| |
| HashMap<String, String> options = new HashMap<>(); |
| options.put("detect_direction", "true"); |
| options.put("detect_risk", "false"); |
| |
| JSONObject frontres = client.idcard("C:\\Users\\yy150\\Pictures\\正.jpg", "front", options); |
| System.out.println(frontres.toString(2)); |
| |
| JSONObject backres = client.idcard("C:\\Users\\yy150\\Pictures\\反.jpg", "back", options); |
| System.out.println(backres.toString(2)); |
| } |
| |
| } |
- 运行Sample中main()方法,运行后的结果,大致如下:
- 备注:识别出来的文字,为了安全起见,用***********代替。
| |
| { |
| "words_result": { |
| "姓名": { |
| "words": "百度熊", |
| "location": { |
| "top": 34, |
| "left": 82, |
| "width": 58, |
| "height": 22 |
| } |
| }, |
| "民族": { |
| "words": "汉", |
| "location": { |
| "top": 74, |
| "left": 171, |
| "width": 17, |
| "height": 19 |
| } |
| }, |
| "住址": { |
| "words": "北京市海淀区上地十号七栋2单元110室", |
| "location": { |
| "top": 149, |
| "left": 76, |
| "width": 203, |
| "height": 41 |
| } |
| }, |
| "公民身份号码": { |
| "words": "532101198906010015", |
| "location": { |
| "top": 240, |
| "left": 137, |
| "width": 256, |
| "height": 20 |
| } |
| }, |
| "出生": { |
| "words": "19890601", |
| "location": { |
| "top": 110, |
| "left": 78, |
| "width": 152, |
| "height": 17 |
| } |
| }, |
| "性别": { |
| "words": "男", |
| "location": { |
| "top": 74, |
| "left": 77, |
| "width": 18, |
| "height": 19 |
| } |
| } |
| }, |
| "log_id": 1350711547869003776, |
| "words_result_num": 6, |
| "idcard_number_type": 1, |
| "image_status": "normal", |
| "direction": 0 |
| } |
| |
| |
| { |
| "words_result": { |
| "失效日期": { |
| "words": "***********", |
| "location": { |
| "height": 22, |
| "width": 90, |
| "left": 308, |
| "top": 263 |
| } |
| }, |
| "签发机关": { |
| "words": "***********", |
| "location": { |
| "height": 30, |
| "width": 265, |
| "left": 201, |
| "top": 215 |
| } |
| }, |
| "签发日期": { |
| "words": "***********", |
| "location": { |
| "height": 22, |
| "width": 95, |
| "left": 198, |
| "top": 258 |
| } |
| } |
| }, |
| "direction": 0, |
| "words_result_num": 3, |
| "image_status": "normal", |
| "log_id": 3369545259709262582 |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~