目錄
1.申請騰訊OCR權(quán)限
2.代碼思路
3.Postman測試?
1.申請騰訊OCR權(quán)限
?獲取 secretId 和 secretKey,見上文
從0到1,申請cos服務(wù)器并上傳圖片到cos文件服務(wù)器-CSDN博客https://blog.csdn.net/m0_55627541/article/details/133902798
2.代碼思路
入?yún)⒂袃蓚€(gè)值,第一個(gè)為圖片的云服務(wù)器路徑,第二個(gè)為版面(正面/反面)
controller
/**
* 身份證識別
* @param path
* @return
*/
@PostMapping("/IDCardOCR")
public Result IDCardOCR(String path,Integer cardSide) {
IDCardResponse idCardResponse = idCardOCRService.identifyIDCardByUrl(path, cardSide);
return Result.success("校驗(yàn)成功",idCardResponse);
}
serviceImpl文章來源:http://www.zghlxwxcb.cn/news/detail-721940.html
package com.zsp.quartz.common;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
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.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
import com.zsp.quartz.Exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* 騰訊身份證識別接口
*/
@Service
@Slf4j
public class TencentIDCardOCRServiceImpl implements IDCardOCRService {
private static String secretId = "xxx";
private static String secretKey = "xxx";
private static String regionName = "ap-shanghai";
@Override
public IDCardResponse identifyIDCardByUrl(String imgUrl, Integer cardSide) {
IDCardOCRRequest req = new IDCardOCRRequest();
req.setImageUrl(imgUrl);
if(cardSide != null) {
if (cardSide == 1) {
req.setCardSide("FRONT");
} else if (cardSide == 2) {
req.setCardSide("BACK");
}
}
return idCardAction(req);
}
private IDCardResponse idCardAction(IDCardOCRRequest req) {
Credential cred = new Credential(secretId, secretKey);
// 實(shí)例化一個(gè)http選項(xiàng),可選的,沒有特殊需求可以跳過
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("ocr.tencentcloudapi.com");
// 實(shí)例化一個(gè)client選項(xiàng),可選的,沒有特殊需求可以跳過
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
OcrClient client = new OcrClient(cred, regionName,clientProfile);
IDCardOCRResponse res = null;
try {
res = client.IDCardOCR(req);
} catch (TencentCloudSDKException e) {
throw new CustomException("校驗(yàn)失敗");
}
if(res != null) {
IDCardResponse idCardResponse = new IDCardResponse();
idCardResponse.setAddress(res.getAddress());
idCardResponse.setAuthority(res.getAuthority());
if(!StrUtil.isEmpty(res.getBirth())) {
idCardResponse.setBirth(DateUtil.parseDate(res.getBirth()));
}
idCardResponse.setIdNum(res.getIdNum());
idCardResponse.setName(res.getName());
idCardResponse.setNation(res.getNation());
if("男".equals(res.getSex())) {
idCardResponse.setSex(1);
} else if("女".equals(res.getSex())) {
idCardResponse.setSex(2);
} else {
idCardResponse.setSex(0);
}
idCardResponse.setValidDate(res.getValidDate());
return idCardResponse;
}
return null;
}
}
VO層文章來源地址http://www.zghlxwxcb.cn/news/detail-721940.html
package com.zsp.quartz.common;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.util.Date;
@Data
public class IDCardResponse {
/**
* 姓名(人像面)
*/
@JSONField(name = "Name")
private String name;
/**
* 性別(人像面)
*/
@JSONField(name = "Sex")
private Integer sex;
/**
* 民族(人像面)
*/
@JSONField(name = "Nation")
private String nation;
/**
* 出生日期(人像面)
*/
@JSONField(name = "Birth")
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date birth;
/**
* 地址(人像面)
*/
@JSONField(name = "Address")
private String address;
/**
* 身份證號(人像面)
*/
@JSONField(name = "IdNum")
private String idNum;
/**
* 發(fā)證機(jī)關(guān)(國徽面)
*/
@JSONField(name = "Authority")
private String authority;
/**
* 證件有效期(國徽面)
*/
@JSONField(name = "ValidDate")
private String validDate;
}
3.Postman測試
到了這里,關(guān)于從0-1,使用騰訊OCR進(jìn)行身份證識別的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!