Skip to content

Commit

Permalink
#1167 增加File类型身份证OCR识别方法
Browse files Browse the repository at this point in the history
  • Loading branch information
94zhaoyueran authored and binarywang committed Aug 21, 2019
1 parent 156fc77 commit 92118c2
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 24 deletions.
29 changes: 9 additions & 20 deletions .gitee/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
强烈建议大家到 github 相关页面提交问题,方便统一查询管理,具体页面地址: https://github.com/Wechat-Group/WxJava/issues
强烈建议大家到 `github` 相关页面提交问题,方便统一查询管理,具体页面地址:https://github.com/Wechat-Group/WxJava/issues

当然如果必须在这里提问,请务必按以下格式填写,谢谢配合~

# 问题标题(提问前,请确保阅读过项目首页说明以及wiki文档相关内容)
# 提问前,请确保阅读过项目首页说明以及wiki开发文档相关内容,尤其是常见问题部分。完成内容后,请务必移除包括本句在内的无用内容,以免影响阅读,谢谢合作~
# 另外如果确认属于bug,而且已明确如何修复,请参考贡献指南直接提交PR,省的浪费时间在这里描述问题,非常感谢配合

### 简要描述
__明确描述下你所遇到的问题,(友情提示,如果确认属于bug,请参考贡献指南直接提交PR,省的花费时间在这里描述问题,非常感谢配合)__

__简单概括描述下你所遇到的问题。__

### 模块版本情况
* `WxJava` 模块名:
* `WxJava` 版本号:

* WxJava 模块名:
* WxJava 版本号:


### 期待结果
__尽量详细描述__

### 实际情况
__尽量详细描述__

### 重现步骤
1.
2.
3.

### 详细描述
__尽量详细描述。请不要使用截图,尽量使用文字描述,代码直接贴上来,日志则请附在后面所示区域。__

### 日志
__将日志放在 [pastebin](https://paste.ubuntu.com/) 或者其他地方,并将url地址贴在这里__
__将日志放在 [`Pastebin`](https://paste.ubuntu.com/) 或者其他地方,并将其url地址贴在这里__
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
1. 入群技术交流:若想获得QQ群/微信群/企业微信/钉钉企业群等信息的,请使用微信扫描上面的微信公众号二维码关注 `WxJava` 后点击相关菜单即可,同时也可以在微信中搜索 `weixin-java-tools``WxJava` 后选择正确的公众号进行关注,该公众号会及时通知SDK相关更新信息,并不定期分享微信Java开发相关技术知识;
1. 付费QQ群:(**注意:刚入群会有5分钟禁言,稍等片刻即可正常发言**[![加入QQ群](https://img.shields.io/badge/QQ群-343954419-blue.svg)](http://shang.qq.com/wpa/qunwpa?idkey=731dc3e7ea31ebe25376cc1a791445468612c63fd0e9e05399b088ec81fd9e15)[![加入QQ群](https://img.shields.io/badge/QQ群-343954419-blue.svg)](http://jq.qq.com/?_wv=1027&k=40lRskK),或者请自行搜索群号`343954419`进行添加;当然由于某种原因无法入群的,可关注公众号后获取其他群的加入方式;
1. 钉钉企业群:[请点击链接申请加入](https://h5.dingtalk.com/inviteColleague/index.html#/invite/9ed100cc4a/E1DF918E32E398D191E7FE61FE0552A6) 或者 [用手机钉钉APP扫码](https://gitee.com/binary/weixin-java-tools/raw/master/images/qrcodes/ding.jpg) 申请加入。
1. 微信开发新手或者Java开发新手在群内提问或新开Issue提问前,请先阅读[【提问的智慧】](http://www.binarywang.com/article/smart-questions),并确保已查阅过 [【开发文档Wiki】](https://github.com/wechat-group/WxJava/wiki) ,避免浪费大家的宝贵时间;
1. 微信开发新手或者Java开发新手在群内提问或新开Issue提问前,请先阅读[【提问的智慧】](https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way/blob/master/README-zh_CN.md),并确保已查阅过 [【开发文档Wiki】](https://github.com/wechat-group/WxJava/wiki) ,避免浪费大家的宝贵时间;
1. 寻求帮助时需贴代码或大长串异常信息的,请利用 http://paste.ubuntu.com

--------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import me.chanjar.weixin.mp.api.WxMpOcrService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrIdCardResult;
import me.chanjar.weixin.mp.util.requestexecuter.ocr.OcrDiscernRequestExecutor;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.FILEIDCARD;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.IDCARD;

/**
Expand All @@ -37,7 +39,9 @@ public WxMpOcrIdCardResult idCard(ImageType imgType, String imgUrl) throws WxErr
}

@Override
public WxMpOcrIdCardResult idCard(ImageType imgType, File imgFile) {
return null;
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);
return WxMpOcrIdCardResult.fromJson(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,9 @@ enum Ocr implements WxMpApiUrl {
/**
* 身份证识别.
*/
IDCARD(API_DEFAULT_HOST_URL, "/cv/ocr/idcard?type=%s&img_url=%s");
IDCARD(API_DEFAULT_HOST_URL, "/cv/ocr/idcard?type=%s&img_url=%s"),

FILEIDCARD(API_DEFAULT_HOST_URL, "/cv/ocr/idcard?type=%s");

private String prefix;
private String path;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package me.chanjar.weixin.mp.util.requestexecuter.ocr;

import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.File;
import java.io.IOException;

/**
* @author : zhayueran
* @Date: 2019/6/27 14:06
* @Description:
*/
public class OcrDiscernApacheHttpRequestExecutor extends OcrDiscernRequestExecutor<CloseableHttpClient, HttpHost> {


public OcrDiscernApacheHttpRequestExecutor(RequestHttp requestHttp) {
super(requestHttp);
}

@Override
public String execute(String uri, File file) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (requestHttp.getRequestHttpProxy() != null) {
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
httpPost.setConfig(config);
}
if (file != null) {
HttpEntity entity = MultipartEntityBuilder
.create()
.addBinaryBody("file", file)
.setMode(HttpMultipartMode.RFC6532)
.build();
httpPost.setEntity(entity);
}
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return responseContent;
} finally {
httpPost.releaseConnection();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package me.chanjar.weixin.mp.util.requestexecuter.ocr;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.ResponseHandler;

import java.io.File;
import java.io.IOException;

/**
* @author : zhayueran
* @Date: 2019/6/27 15:06
* @Description:
*/
public abstract class OcrDiscernRequestExecutor<H, P> implements RequestExecutor<String, File> {
protected RequestHttp<H, P> requestHttp;

public OcrDiscernRequestExecutor(RequestHttp requestHttp) {
this.requestHttp = requestHttp;
}

@Override
public void execute(String uri, File data, ResponseHandler<String> handler) throws WxErrorException, IOException {
handler.handle(this.execute(uri, data));
}

public static RequestExecutor<String, File> create(RequestHttp requestHttp) {
switch (requestHttp.getRequestType()) {
case APACHE_HTTP:
return new OcrDiscernApacheHttpRequestExecutor(requestHttp);
default:
return null;
}
}
}

0 comments on commit 92118c2

Please sign in to comment.