腾讯OCR身份证正面识别
开发文档
https://cloud.tencent.com/document/sdk/Java
首先再maven里依赖插件
依赖版本
<tencentcloud-sdk-java>4.0.11</tencentcloud-sdk-java> <tencentcloud-sdk-java-common>3.1.498</tencentcloud-sdk-java-common> <commons-logging>1.2</commons-logging> <okhttp>2.7.5</okhttp> <logging-interceptor>2.7.5</logging-interceptor> <okio>1.12.0</okio> <jaxb-api>2.3.0</jaxb-api> <ini4j>0.5.4</ini4j>
依赖项
<!-- 身份证OCR--> <dependency> <groupId>com.tencentcloudapi</groupId> <artifactId>tencentcloud-sdk-java</artifactId> <version>${tencentcloud-sdk-java}</version> </dependency> <dependency> <groupId>com.tencentcloudapi</groupId> <artifactId>tencentcloud-sdk-java-common</artifactId> <version>${tencentcloud-sdk-java-common}</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>${commons-logging}</version> </dependency> <dependency> <groupId>com.squareup.okhttp</groupId> <artifactId>okhttp</artifactId> <version>${okhttp}</version> </dependency> <dependency> <groupId>com.squareup.okhttp</groupId> <artifactId>logging-interceptor</artifactId> <version>${logging-interceptor}</version> </dependency> <dependency> <groupId>com.squareup.okio</groupId> <artifactId>okio</artifactId> <version>${okio}</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>${jaxb-api}</version> </dependency> <dependency> <groupId>org.ini4j</groupId> <artifactId>ini4j</artifactId> <version>${ini4j}</version> </dependency>
下载好依赖取API秘钥管理生成一个秘钥
https://console.cloud.tencent.com/cam/capi
生成秘钥之后到调试中心生成代码
生成的代码直接拿过来
package com.xxxxxxxxxxx.common.utils; import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; import com.tencentcloudapi.common.exception.TencentCloudSDKException; import com.tencentcloudapi.ocr.v20181119.OcrClient; import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest; import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse; /*** *Date:2022/5/2 18:14 *Description: */ public class IDCardOCR { public static void main(String[] args) { try { // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密 // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取 Credential cred = new Credential("自己的SecretId", "自己的SecretKey"); // 实例化一个http选项,可选的,没有特殊需求可以跳过 HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint("ocr.tencentcloudapi.com"); // 实例化一个client选项,可选的,没有特殊需求可以跳过 ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); // 实例化要请求产品的client对象,clientProfile是可选的 OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile); // 实例化一个请求对象,每个接口都会对应一个request对象 IDCardOCRRequest req = new IDCardOCRRequest(); req.setImageUrl("http://pic1.hebei.com.cn/0/10/45/41/10454121_978799.jpg"); req.setCardSide("FRONT"); // 返回的resp是一个IDCardOCRResponse的实例,与请求对象对应 IDCardOCRResponse resp = client.IDCardOCR(req); // 输出json格式的字符串回包 System.out.println(IDCardOCRResponse.toJsonString(resp)); } catch (TencentCloudSDKException e) { System.out.println(e.toString()); } } }
运行结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2017-05-03 Mac版Intellij IDEA弹窗报this license XXXXXXXX has been cancelled