1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 调用腾讯云OCR接口识别身份证和户口本

调用腾讯云OCR接口识别身份证和户口本

时间:2023-03-02 21:49:54

相关推荐

调用腾讯云OCR接口识别身份证和户口本

一、添加项目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 {@Autowiredprivate 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 mon.Credential;import mon.profile.ClientProfile;import mon.profile.HttpProfile;import com.tencentcloudapi.ocr.v1119.OcrClient;import com.tencentcloudapi.ocr.v1119.models.IDCardOCRRequest;import com.tencentcloudapi.ocr.v1119.models.IDCardOCRResponse;import com.tencentcloudapi.ocr.v1119.models.ResidenceBookletOCRRequest;import com.tencentcloudapi.ocr.v1119.models.ResidenceBookletOCRResponse;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Service;import java.util.HashMap;import java.util.Map;/*** 腾讯云OCR文字识别接口调用.*/@Servicepublic class TencentApiOcrService{private static Logger logger = LoggerFactory.getLogger(TencentApiOcrService.class);private static final String FIELD_SECRET_ID = "";//填写secretIdprivate static final String FIELD_SECRET_KEY = "";//填写secretKeyprivate static final String FIELD_TENCENT_CLOUD_API = "";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文档概览

/document/sdk/Java

/TencentCloud/tencentcloud-sdk-java

户口本首页、个人页面测试:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。