Files
yudao-ui-admin-vue3/backend-idcard/README.md
2026-03-03 15:40:21 +08:00

52 lines
1.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 身份证识别(百度 OCR + AES
调用百度身份证 OCR 接口,对图片做 AES 加密上传,对返回的 `result` 密文做 Base64 解码 + AES 解密后解析出所需字段。
## 依赖
- JDK 8+
- [fastjson](https://github.com/alibaba/fastjson)(解析返回 JSON
## 配置
从百度控制台获取:
- **access_token**OAuth2 获取(或使用 API Key + Secret Key 换 token
- **aesKey**16 位 hex 字符串(控制台身份证 OCR 安全设置里)
## 使用示例
```java
// 构造识别器accessToken、aesKey 建议从配置文件或环境变量读取)
IdcardRecognizer recognizer = new IdcardRecognizer(accessToken, aesKey);
// 方式一:本地文件
IdcardRecognizer.IdcardResult result = recognizer.recognize("/path/to/idcard_front.jpg", "front");
if (result != null) {
System.out.println("姓名: " + result.getName());
System.out.println("身份证号: " + result.getIdNumber());
System.out.println("出生: " + result.getBirth());
System.out.println("住址: " + result.getAddress());
// ...
}
// 方式二:上传的图片字节(如 MultipartFile.getBytes()
byte[] imgBytes = ...;
IdcardRecognizer.IdcardResult back = recognizer.recognize(imgBytes, "back");
if (back != null) {
System.out.println("签发机关: " + back.getIssueAuthority());
System.out.println("有效期限: " + back.getValidDate());
}
```
## 返回字段说明
- **正面 (side=front)**`name` 姓名、`gender` 性别、`nation` 民族、`birth` 出生、`address` 住址、`idNumber` 公民身份号码
- **反面 (side=back)**`issueAuthority` 签发机关、`validDate` 有效期限
按百度接口约定,请求时 `id_card_side``front``back`
## 集成到现有后端
`com.ydoyun.ocr` 包拷贝到你的项目中,并加入 fastjson 依赖即可;若项目已使用 Spring可把 `IdcardRecognizer` 做成 Bean`accessToken``aesKey``@Value` 或配置类注入。