对接腾讯OCR识别
- 创建应用 得到密钥和APPID: https://cloud.tencent.com/document/sdk/Java
- 接口文档查看:https://cloud.tencent.com/document/product/866/33524
- 使用代码生成功能:https://console.cloud.tencent.com/api/explorer?Product=ocr&Version=2018-11-19&Action=IDCardOCR&SignVersion=
引入Jar包
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<!-- go to https://search.maven.org/search?q=tencentcloud-sdk-java and get the latest version. -->
<!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,最新版本如下 -->
<version>3.1.322</version>
</dependency>
身份证识别
接口文档地址:https://cloud.tencent.com/document/product/866/33524
API工具:https://console.cloud.tencent.com/api/explorer?Product=ocr&Version=2018-11-19&Action=IDCardOCR&SignVersion=
public static void main(String [] args) {
try{
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
// 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
// Credential cred = new Credential(System.getenv("cloud_secret_id"),
// System.getenv("cloud_secret_key"),
// System.getenv("cloud_token"));
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-guangzhou", clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象
IDCardOCRRequest req = new IDCardOCRRequest();
req.setCardSide("FRONT");
byte[] bytes = Files.readAllBytes(Paths.get("D:\\个人资料\\idcard.jpg"));
req.setImageBase64(Base64.getEncoder().encodeToString(bytes));
// 返回的resp是一个IDCardOCRResponse的实例,与请求对象对应
IDCardOCRResponse resp = client.IDCardOCR(req);
// 输出json格式的字符串回包
System.out.println(IDCardOCRResponse.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
} catch (IOException e) {
System.out.println(e.toString());
}
}
营业执照识别
接口文档:https://cloud.tencent.com/document/product/866/36215
API工具:https://console.cloud.tencent.com/api/explorer?Product=ocr&Version=2018-11-19&Action=BizLicenseOCR&SignVersion=
public static void main(String [] args) {
try{
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-guangzhou", clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象
BizLicenseOCRRequest req = new BizLicenseOCRRequest();
byte[] bytes = Files.readAllBytes(Paths.get("D:\\个人资料\\idcard.jpg"));
req.setImageBase64(Base64.getEncoder().encodeToString(bytes));
// 返回的resp是一个IDCardOCRResponse的实例,与请求对象对应
BizLicenseOCRResponse resp = client.BizLicenseOCR(req);
// 输出json格式的字符串回包
System.out.println(IDCardOCRResponse.toJsonString(resp));
} catch (TencentCloudSDKException | IOException e) {
System.out.println(e.toString());
}
}