🆕 #1527 微信小程序增加OCR身份证识别和银行卡识别等接口

This commit is contained in:
Binary Wang
2020-07-05 21:32:50 +08:00
parent b75569498c
commit 978ada7373
25 changed files with 756 additions and 176 deletions

View File

@@ -1,131 +0,0 @@
package me.chanjar.weixin.mp.api;
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;
/**
* 基于小程序或 H5 的身份证、银行卡、行驶证 OCR 识别.
* https://mp.weixin.qq.com/wiki?t=resource/res_main&id=21516712284rHWMX
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2019-06-22
*/
public interface WxMpOcrService {
/**
* 身份证OCR识别接口.
*
* @param imgUrl 图片url地址
* @return WxMpOcrIdCardResult
* @throws WxErrorException .
*/
WxMpOcrIdCardResult idCard(String imgUrl) throws WxErrorException;
/**
* 身份证OCR识别接口.
*
* @param imgFile 图片文件对象
* @return WxMpOcrIdCardResult
* @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

@@ -1,5 +1,6 @@
package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.api.WxOcrService;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxNetCheckResult;
import me.chanjar.weixin.common.error.WxErrorException;
@@ -555,7 +556,7 @@ public interface WxMpService extends WxService {
*
* @return WxMpWifiService
*/
WxMpOcrService getOcrService();
WxOcrService getOcrService();
/**
* 返回图像处理接口的实现类对象,以方便调用其各个接口.
@@ -688,7 +689,7 @@ public interface WxMpService extends WxService {
*
* @param ocrService .
*/
void setOcrService(WxMpOcrService ocrService);
void setOcrService(WxOcrService ocrService);
/**
* .

View File

@@ -10,6 +10,7 @@ import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.api.WxOcrService;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxNetCheckResult;
@@ -70,7 +71,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
private final WxMpWifiService wifiService = new WxMpWifiServiceImpl(this);
private WxMpMarketingService marketingService = new WxMpMarketingServiceImpl(this);
private WxMpCommentService commentService = new WxMpCommentServiceImpl(this);
private WxMpOcrService ocrService = new WxMpOcrServiceImpl(this);
private WxOcrService ocrService = new WxMpOcrServiceImpl(this);
private WxMpImgProcService imgProcService = new WxMpImgProcServiceImpl(this);
@Getter
@@ -663,7 +664,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
}
@Override
public WxMpOcrService getOcrService() {
public WxOcrService getOcrService() {
return this.ocrService;
}
@@ -678,7 +679,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
}
@Override
public void setOcrService(WxMpOcrService ocrService) {
public void setOcrService(WxOcrService ocrService) {
this.ocrService = ocrService;
}

View File

@@ -2,14 +2,14 @@ package me.chanjar.weixin.mp.api.impl;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpOcrService;
import me.chanjar.weixin.common.api.WxOcrService;
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.common.bean.ocr.WxOcrBankCardResult;
import me.chanjar.weixin.common.bean.ocr.WxOcrBizLicenseResult;
import me.chanjar.weixin.common.bean.ocr.WxOcrCommResult;
import me.chanjar.weixin.common.bean.ocr.WxOcrDrivingLicenseResult;
import me.chanjar.weixin.common.bean.ocr.WxOcrDrivingResult;
import me.chanjar.weixin.common.bean.ocr.WxOcrIdCardResult;
import me.chanjar.weixin.mp.util.requestexecuter.ocr.OcrDiscernRequestExecutor;
import java.io.File;
@@ -37,11 +37,11 @@ import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.IDCARD;
* @date 2019-06-22
*/
@RequiredArgsConstructor
public class WxMpOcrServiceImpl implements WxMpOcrService {
public class WxMpOcrServiceImpl implements WxOcrService {
private final WxMpService mainService;
@Override
public WxMpOcrIdCardResult idCard(String imgUrl) throws WxErrorException {
public WxOcrIdCardResult idCard(String imgUrl) throws WxErrorException {
try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
@@ -50,18 +50,18 @@ public class WxMpOcrServiceImpl implements WxMpOcrService {
final String result = this.mainService.get(String.format(IDCARD.getUrl(this.mainService.getWxMpConfigStorage()),
imgUrl), null);
return WxMpOcrIdCardResult.fromJson(result);
return WxOcrIdCardResult.fromJson(result);
}
@Override
public WxMpOcrIdCardResult idCard(File imgFile) throws WxErrorException {
public WxOcrIdCardResult idCard(File imgFile) throws WxErrorException {
String result = this.mainService.execute(OcrDiscernRequestExecutor.create(this.mainService.getRequestHttp()),
FILEIDCARD.getUrl(this.mainService.getWxMpConfigStorage()), imgFile);
return WxMpOcrIdCardResult.fromJson(result);
return WxOcrIdCardResult.fromJson(result);
}
@Override
public WxMpOcrBankCardResult bankCard(String imgUrl) throws WxErrorException {
public WxOcrBankCardResult bankCard(String imgUrl) throws WxErrorException {
try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
@@ -70,18 +70,18 @@ public class WxMpOcrServiceImpl implements WxMpOcrService {
final String result = this.mainService.get(String.format(BANK_CARD.getUrl(this.mainService.getWxMpConfigStorage()),
imgUrl), null);
return WxMpOcrBankCardResult.fromJson(result);
return WxOcrBankCardResult.fromJson(result);
}
@Override
public WxMpOcrBankCardResult bankCard(File imgFile) throws WxErrorException {
public WxOcrBankCardResult bankCard(File imgFile) throws WxErrorException {
String result = this.mainService.execute(OcrDiscernRequestExecutor.create(this.mainService.getRequestHttp()),
FILE_BANK_CARD.getUrl(this.mainService.getWxMpConfigStorage()), imgFile);
return WxMpOcrBankCardResult.fromJson(result);
return WxOcrBankCardResult.fromJson(result);
}
@Override
public WxMpOcrDrivingResult driving(String imgUrl) throws WxErrorException {
public WxOcrDrivingResult driving(String imgUrl) throws WxErrorException {
try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
@@ -90,18 +90,18 @@ public class WxMpOcrServiceImpl implements WxMpOcrService {
final String result = this.mainService.get(String.format(DRIVING.getUrl(this.mainService.getWxMpConfigStorage()),
imgUrl), null);
return WxMpOcrDrivingResult.fromJson(result);
return WxOcrDrivingResult.fromJson(result);
}
@Override
public WxMpOcrDrivingResult driving(File imgFile) throws WxErrorException {
public WxOcrDrivingResult driving(File imgFile) throws WxErrorException {
String result = this.mainService.execute(OcrDiscernRequestExecutor.create(this.mainService.getRequestHttp()),
FILE_DRIVING.getUrl(this.mainService.getWxMpConfigStorage()), imgFile);
return WxMpOcrDrivingResult.fromJson(result);
return WxOcrDrivingResult.fromJson(result);
}
@Override
public WxMpOcrDrivingLicenseResult drivingLicense(String imgUrl) throws WxErrorException {
public WxOcrDrivingLicenseResult drivingLicense(String imgUrl) throws WxErrorException {
try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
@@ -110,18 +110,18 @@ public class WxMpOcrServiceImpl implements WxMpOcrService {
final String result = this.mainService.get(String.format(DRIVING_LICENSE.getUrl(this.mainService.getWxMpConfigStorage()),
imgUrl), null);
return WxMpOcrDrivingLicenseResult.fromJson(result);
return WxOcrDrivingLicenseResult.fromJson(result);
}
@Override
public WxMpOcrDrivingLicenseResult drivingLicense(File imgFile) throws WxErrorException {
public WxOcrDrivingLicenseResult drivingLicense(File imgFile) throws WxErrorException {
String result = this.mainService.execute(OcrDiscernRequestExecutor.create(this.mainService.getRequestHttp()),
FILE_DRIVING_LICENSE.getUrl(this.mainService.getWxMpConfigStorage()), imgFile);
return WxMpOcrDrivingLicenseResult.fromJson(result);
return WxOcrDrivingLicenseResult.fromJson(result);
}
@Override
public WxMpOcrBizLicenseResult bizLicense(String imgUrl) throws WxErrorException {
public WxOcrBizLicenseResult bizLicense(String imgUrl) throws WxErrorException {
try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
@@ -130,18 +130,18 @@ public class WxMpOcrServiceImpl implements WxMpOcrService {
final String result = this.mainService.get(String.format(BIZ_LICENSE.getUrl(this.mainService.getWxMpConfigStorage()),
imgUrl), null);
return WxMpOcrBizLicenseResult.fromJson(result);
return WxOcrBizLicenseResult.fromJson(result);
}
@Override
public WxMpOcrBizLicenseResult bizLicense(File imgFile) throws WxErrorException {
public WxOcrBizLicenseResult bizLicense(File imgFile) throws WxErrorException {
String result = this.mainService.execute(OcrDiscernRequestExecutor.create(this.mainService.getRequestHttp()),
FILE_BIZ_LICENSE.getUrl(this.mainService.getWxMpConfigStorage()), imgFile);
return WxMpOcrBizLicenseResult.fromJson(result);
return WxOcrBizLicenseResult.fromJson(result);
}
@Override
public WxMpOcrCommResult comm(String imgUrl) throws WxErrorException {
public WxOcrCommResult comm(String imgUrl) throws WxErrorException {
try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
@@ -150,13 +150,13 @@ public class WxMpOcrServiceImpl implements WxMpOcrService {
final String result = this.mainService.get(String.format(COMM.getUrl(this.mainService.getWxMpConfigStorage()),
imgUrl), null);
return WxMpOcrCommResult.fromJson(result);
return WxOcrCommResult.fromJson(result);
}
@Override
public WxMpOcrCommResult comm(File imgFile) throws WxErrorException {
public WxOcrCommResult comm(File imgFile) throws WxErrorException {
String result = this.mainService.execute(OcrDiscernRequestExecutor.create(this.mainService.getRequestHttp()),
FILE_COMM.getUrl(this.mainService.getWxMpConfigStorage()), imgFile);
return WxMpOcrCommResult.fromJson(result);
return WxOcrCommResult.fromJson(result);
}
}