OCR身份證識別
官網地址:https://cloud.tencent.com/document/product/866/33524
身份信息認證(二要素核驗)
官網地址:https://cloud.tencent.com/document/product/1007/33188
代碼實現
引入依賴
<dependency><groupId>com.tencentcloudapi</groupId><artifactId>tencentcloud-sdk-java</artifactId><version>4.0.11</version>
</dependency>
工具類
package com.qiangesoft.ocr.utils;import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationResponse;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;/*** 騰訊云ocr識別** @author qiangesoft* @date 2025-03-27*/
public class TencentCloudOcr {private static final String SECRET_ID = "xxx";private static final String SECRET_KEY = "xxx ";private static final String REGION_NAME = "ap-shanghai";/*** 調用OCR識別身份證信息* https://cloud.tencent.com/document/product/866/33524** @param imgUrl* @return* @throws TencentCloudSDKException*/public static IDCardOCRResponse idCardOCR(String imgUrl) throws TencentCloudSDKException {Credential cred = new Credential(SECRET_ID, SECRET_KEY);HttpProfile httpProfile = new HttpProfile();httpProfile.setEndpoint("ocr.tencentcloudapi.com");ClientProfile clientProfile = new ClientProfile();clientProfile.setHttpProfile(httpProfile);OcrClient client = new OcrClient(cred, REGION_NAME, clientProfile);IDCardOCRRequest req = new IDCardOCRRequest();req.setImageUrl(imgUrl);req.setCardSide("FRONT");
// req.setCardSide("BACK");return client.IDCardOCR(req);}/*** 調用身份信息核驗API* https://cloud.tencent.com/document/product/1007/33188** @param name* @param idCard* @return* @throws TencentCloudSDKException*/public static IdCardVerificationResponse idCardVerification(String name, String idCard) throws TencentCloudSDKException {Credential cred = new Credential(SECRET_ID, SECRET_KEY);HttpProfile httpProfile = new HttpProfile();httpProfile.setEndpoint("faceid.tencentcloudapi.com");ClientProfile clientProfile = new ClientProfile();clientProfile.setHttpProfile(httpProfile);FaceidClient client = new FaceidClient(cred, REGION_NAME, clientProfile);IdCardVerificationRequest req = new IdCardVerificationRequest();req.setName(name);req.setIdCard(idCard);return client.IdCardVerification(req);}public static void main(String[] args) throws TencentCloudSDKException {IDCardOCRResponse idCardOCRResponse = idCardOCR("https://www.fsf.com");IdCardVerificationResponse idCardVerificationResponse = idCardVerification("xxx", "22222");}}