一、添加项目pom文件依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.49</version>
</dependency>
二、API调用的控制层代码编写
package com.wy.gcserver.ocr.controller;
import com.wy.gcserver.ocr.model.ImageScanParam;
import com.wy.gcserver.ocr.service.TencentApiOcrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/tencent/api/ocr")
public class TencentApiOcrController {
@Autowired
private TencentApiOcrService tencentApiOcrService;
/**
* 获取身份证OCR识别信息.
*
* @param param
* @return
*/
@RequestMapping(value = "/getIdcardOcr",method = RequestMethod.POST)
public String getIdcardOcr(@RequestBody ImageScanParam param){
String idCardSide = param.getJob();
String imageStr = param.getInData().getImage();
String result = tencentApiOcrService.getIdcardOcr(idCardSide, imageStr);
return result;
}
/**
* 获取户口本OCR识别信息.
*
* @param param
* @return
*/
@RequestMapping(value = "/getResidenceBookletOCR",method = RequestMethod.POST)
public String getResidenceBookletOCR(@RequestBody ImageScanParam param){
String imageStr = param.getInData().getImage();
String result = tencentApiOcrService.getResidenceBookletOCR(imageStr);
return result;
}
}
三、腾讯云API接口调用Service层代码编写
package com.wy.gcserver.ocr.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
import com.tencentcloudapi.ocr.v20181119.models.ResidenceBookletOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.ResidenceBookletOCRResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* 腾讯云OCR文字识别接口调用.
*/
@Service
public class TencentApiOcrService{
private static Logger logger = LoggerFactory.getLogger(TencentApiOcrService.class);
private static final String FIELD_SECRET_ID = "";//填写secretId
private static final String FIELD_SECRET_KEY = "";//填写secretKey
private static final String FIELD_TENCENT_CLOUD_API = "ocr.tencentcloudapi.com";
private static final String FIELD_REGION = "ap-guangzhou";
/**
* 获取OcrClient.
*
* @return
*/
private OcrClient getOcrClient(){
Credential cred = new Credential(FIELD_SECRET_ID, FIELD_SECRET_KEY);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(FIELD_TENCENT_CLOUD_API);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
OcrClient ocrClient = new OcrClient(cred, FIELD_REGION, clientProfile);
return ocrClient;
}
/**
* 身份证识别.
*
* @param cardSide
* @param imageStr
* @return
*/
public String getIdcardOcr(String cardSide, String imageStr) {
OcrClient ocrClient = getOcrClient();
Map<String, String> params = new HashMap<>();
params.put("ImageBase64", imageStr);
params.put("CardSide", cardSide);
try {
IDCardOCRRequest req = IDCardOCRRequest.fromJsonString(JSON.toJSONString(params), IDCardOCRRequest.class);
IDCardOCRResponse resp = ocrClient.IDCardOCR(req);
HashMap<String, String> dataMap = new HashMap<>();
resp.toMap(dataMap, "");
return JSONObject.toJSONString(dataMap);
} catch (Exception e) {
logger.info(e.getMessage());
}
return null;
}
/**
* 户口本识别.
*
* @param imageStr
* @return
*/
public String getResidenceBookletOCR(String imageStr){
OcrClient ocrClient = getOcrClient();
Map<String, String> params = new HashMap<>();
params.put("ImageBase64", imageStr);
try {
ResidenceBookletOCRRequest req = ResidenceBookletOCRRequest.fromJsonString(JSON.toJSONString(params), ResidenceBookletOCRRequest.class);
ResidenceBookletOCRResponse resp = ocrClient.ResidenceBookletOCR(req);
HashMap<String, String> dataMap = new HashMap<>();
resp.toMap(dataMap, "");
return JSONObject.toJSONString(dataMap);
} catch (Exception e) {
logger.info(e.getMessage());
}
return null;
}
}
参考文献:
腾讯云OCR文字识别API文档概览
https://cloud.tencent.com/document/sdk/Java
https://github.com/TencentCloud/tencentcloud-sdk-java
户口本首页、个人页面测试: