开发文档
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()); } } }
运行结果: