#1242 公众号OCR接口补充完善

* 公众号ocr银行卡识别接口

* 公众号ocr行驶证识别接口

* 公众号ocr驾驶证识别接口

* 公众号营业执照Ocr识别接口

* 公众号通用印刷体OCR识别接口

* 去掉身份证Ocr接口中已经废弃的type参数
This commit is contained in:
TheoNie
2019-10-18 17:22:17 +08:00
committed by Binary Wang
parent 67128b02db
commit 48586de7d9
11 changed files with 1071 additions and 32 deletions

View File

@@ -1,8 +1,11 @@
package me.chanjar.weixin.mp.api;
import lombok.AllArgsConstructor;
import lombok.Getter;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrBankCardResult;
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrBizLicenseResult;
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrCommResult;
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrDrivingLicenseResult;
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrDrivingResult;
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrIdCardResult;
import java.io.File;
@@ -15,36 +18,114 @@ import java.io.File;
* @date 2019-06-22
*/
public interface WxMpOcrService {
@AllArgsConstructor
@Getter
enum ImageType {
/**
* 拍照模型,带背景的图片.
*/
PHOTO("photo"),
/**
* 扫描模式,不带背景的图片.
*/
SCAN("scan");
private String type;
}
/**
* 身份证OCR识别接口.
*
* @param imgType 图片类型
* @param imgUrl 图片url地址
* @return WxMpOcrIdCardResult
* @throws WxErrorException .
*/
WxMpOcrIdCardResult idCard(ImageType imgType, String imgUrl) throws WxErrorException;
WxMpOcrIdCardResult idCard(String imgUrl) throws WxErrorException;
/**
* 身份证OCR识别接口.
*
* @param imgType 图片类型
* @param imgFile 图片文件对象
* @return WxMpOcrIdCardResult
* @throws WxErrorException .
*/
WxMpOcrIdCardResult idCard(ImageType imgType, File imgFile) throws WxErrorException;
WxMpOcrIdCardResult idCard(File imgFile) throws WxErrorException;
/**
* 银行卡OCR识别接口
* 文件大小限制小于2M
* @param imgUrl 图片url地址
* @return WxMpOcrBankCardResult
* @throws WxErrorException .
*/
WxMpOcrBankCardResult bankCard(String imgUrl) throws WxErrorException;
/**
* 银行卡OCR识别接口
* 文件大小限制小于2M
* @param imgFile 图片文件对象
* @return WxMpOcrBankCardResult
* @throws WxErrorException .
*/
WxMpOcrBankCardResult bankCard(File imgFile) throws WxErrorException;
/**
* 行驶证OCR识别接口
* 文件大小限制小于2M
* @param imgUrl 图片url地址
* @return WxMpOcrDrivingResult
* @throws WxErrorException .
*/
WxMpOcrDrivingResult driving(String imgUrl) throws WxErrorException;
/**
* 行驶证OCR识别接口
* 文件大小限制小于2M
* @param imgFile 图片文件对象
* @return WxMpOcrDrivingResult
* @throws WxErrorException .
*/
WxMpOcrDrivingResult driving(File imgFile) throws WxErrorException;
/**
* 驾驶证OCR识别接口
* 文件大小限制小于2M
* @param imgUrl 图片url地址
* @return WxMpOcrDrivingLicenseResult
* @throws WxErrorException .
*/
WxMpOcrDrivingLicenseResult drivingLicense(String imgUrl) throws WxErrorException;
/**
* 驾驶证OCR识别接口
* 文件大小限制小于2M
* @param imgFile 图片文件对象
* @return WxMpOcrDrivingLicenseResult
* @throws WxErrorException .
*/
WxMpOcrDrivingLicenseResult drivingLicense(File imgFile) throws WxErrorException;
/**
* 营业执照OCR识别接口
* 文件大小限制小于2M
* @param imgUrl 图片url地址
* @return WxMpOcrBizLicenseResult
* @throws WxErrorException .
*/
WxMpOcrBizLicenseResult bizLicense(String imgUrl) throws WxErrorException;
/**
* 营业执照OCR识别接口
* 文件大小限制小于2M
* @param imgFile 图片文件对象
* @return WxMpOcrBizLicenseResult
* @throws WxErrorException .
*/
WxMpOcrBizLicenseResult bizLicense(File imgFile) throws WxErrorException;
/**
* 通用印刷体OCR识别接口
* 文件大小限制小于2M
* 适用于屏幕截图、印刷体照片等场景
* @param imgUrl 图片url地址
* @return WxMpOcrCommResult
* @throws WxErrorException .
*/
WxMpOcrCommResult comm(String imgUrl) throws WxErrorException;
/**
* 通用印刷体OCR识别接口
* 文件大小限制小于2M
* 适用于屏幕截图、印刷体照片等场景
* @param imgFile 图片文件对象
* @return WxMpOcrCommResult
* @throws WxErrorException .
*/
WxMpOcrCommResult comm(File imgFile) throws WxErrorException;
}

View File

@@ -4,6 +4,11 @@ import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpOcrService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrBankCardResult;
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrBizLicenseResult;
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrCommResult;
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrDrivingLicenseResult;
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrDrivingResult;
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrIdCardResult;
import me.chanjar.weixin.mp.util.requestexecuter.ocr.OcrDiscernRequestExecutor;
@@ -12,7 +17,17 @@ import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.BANK_CARD;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.BIZ_LICENSE;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.COMM;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.DRIVING;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.DRIVING_LICENSE;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.FILEIDCARD;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.FILE_BANK_CARD;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.FILE_BIZ_LICENSE;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.FILE_COMM;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.FILE_DRIVING;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.FILE_DRIVING_LICENSE;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.IDCARD;
/**
@@ -26,7 +41,7 @@ public class WxMpOcrServiceImpl implements WxMpOcrService {
private final WxMpService wxMpService;
@Override
public WxMpOcrIdCardResult idCard(ImageType imgType, String imgUrl) throws WxErrorException {
public WxMpOcrIdCardResult idCard(String imgUrl) throws WxErrorException {
try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
@@ -34,14 +49,108 @@ public class WxMpOcrServiceImpl implements WxMpOcrService {
}
final String result = this.wxMpService.get(String.format(IDCARD.getUrl(this.wxMpService.getWxMpConfigStorage()),
imgType.getType(), imgUrl), null);
imgUrl), null);
return WxMpOcrIdCardResult.fromJson(result);
}
@Override
public WxMpOcrIdCardResult idCard(ImageType imgType, File imgFile) throws WxErrorException {
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), String.format(FILEIDCARD.getUrl(this.wxMpService.getWxMpConfigStorage()),
imgType.getType()), imgFile);
public WxMpOcrIdCardResult idCard(File imgFile) throws WxErrorException {
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILEIDCARD.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile);
return WxMpOcrIdCardResult.fromJson(result);
}
@Override
public WxMpOcrBankCardResult bankCard(String imgUrl) throws WxErrorException {
try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
// ignore cannot happen
}
final String result = this.wxMpService.get(String.format(BANK_CARD.getUrl(this.wxMpService.getWxMpConfigStorage()),
imgUrl), null);
return WxMpOcrBankCardResult.fromJson(result);
}
@Override
public WxMpOcrBankCardResult bankCard(File imgFile) throws WxErrorException {
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_BANK_CARD.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile);
return WxMpOcrBankCardResult.fromJson(result);
}
@Override
public WxMpOcrDrivingResult driving(String imgUrl) throws WxErrorException {
try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
// ignore cannot happen
}
final String result = this.wxMpService.get(String.format(DRIVING.getUrl(this.wxMpService.getWxMpConfigStorage()),
imgUrl), null);
return WxMpOcrDrivingResult.fromJson(result);
}
@Override
public WxMpOcrDrivingResult driving(File imgFile) throws WxErrorException {
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_DRIVING.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile);
return WxMpOcrDrivingResult.fromJson(result);
}
@Override
public WxMpOcrDrivingLicenseResult drivingLicense(String imgUrl) throws WxErrorException {
try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
// ignore cannot happen
}
final String result = this.wxMpService.get(String.format(DRIVING_LICENSE.getUrl(this.wxMpService.getWxMpConfigStorage()),
imgUrl), null);
return WxMpOcrDrivingLicenseResult.fromJson(result);
}
@Override
public WxMpOcrDrivingLicenseResult drivingLicense(File imgFile) throws WxErrorException {
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_DRIVING_LICENSE.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile);
return WxMpOcrDrivingLicenseResult.fromJson(result);
}
@Override
public WxMpOcrBizLicenseResult bizLicense(String imgUrl) throws WxErrorException {
try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
// ignore cannot happen
}
final String result = this.wxMpService.get(String.format(BIZ_LICENSE.getUrl(this.wxMpService.getWxMpConfigStorage()),
imgUrl), null);
return WxMpOcrBizLicenseResult.fromJson(result);
}
@Override
public WxMpOcrBizLicenseResult bizLicense(File imgFile) throws WxErrorException {
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_BIZ_LICENSE.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile);
return WxMpOcrBizLicenseResult.fromJson(result);
}
@Override
public WxMpOcrCommResult comm(String imgUrl) throws WxErrorException {
try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
// ignore cannot happen
}
final String result = this.wxMpService.get(String.format(COMM.getUrl(this.wxMpService.getWxMpConfigStorage()),
imgUrl), null);
return WxMpOcrCommResult.fromJson(result);
}
@Override
public WxMpOcrCommResult comm(File imgFile) throws WxErrorException {
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_COMM.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile);
return WxMpOcrCommResult.fromJson(result);
}
}