From f24495b729a82374481a6b8aed401e40f2b4ad49 Mon Sep 17 00:00:00 2001 From: 94zhaoyueran <523764105@qq.com> Date: Wed, 21 Aug 2019 11:45:23 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20#1167=20=E5=A2=9E=E5=8A=A0File?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=BA=AB=E4=BB=BD=E8=AF=81OCR=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mp/api/impl/WxMpOcrServiceImpl.java | 8 ++- .../chanjar/weixin/mp/enums/WxMpApiUrl.java | 4 +- .../OcrDiscernApacheHttpRequestExecutor.java | 57 +++++++++++++++++++ .../ocr/OcrDiscernRequestExecutor.java | 36 ++++++++++++ 4 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/ocr/OcrDiscernApacheHttpRequestExecutor.java create mode 100644 weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/ocr/OcrDiscernRequestExecutor.java diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpOcrServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpOcrServiceImpl.java index 4d3c434ec5..0f52f2aff4 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpOcrServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpOcrServiceImpl.java @@ -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; /** @@ -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); } } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/enums/WxMpApiUrl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/enums/WxMpApiUrl.java index 63cbe95862..503183cf85 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/enums/WxMpApiUrl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/enums/WxMpApiUrl.java @@ -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; diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/ocr/OcrDiscernApacheHttpRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/ocr/OcrDiscernApacheHttpRequestExecutor.java new file mode 100644 index 0000000000..3547b40fa9 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/ocr/OcrDiscernApacheHttpRequestExecutor.java @@ -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 { + + + 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(); + } + } +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/ocr/OcrDiscernRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/ocr/OcrDiscernRequestExecutor.java new file mode 100644 index 0000000000..c66382178a --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/ocr/OcrDiscernRequestExecutor.java @@ -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 implements RequestExecutor { + protected RequestHttp requestHttp; + + public OcrDiscernRequestExecutor(RequestHttp requestHttp) { + this.requestHttp = requestHttp; + } + + @Override + public void execute(String uri, File data, ResponseHandler handler) throws WxErrorException, IOException { + handler.handle(this.execute(uri, data)); + } + + public static RequestExecutor create(RequestHttp requestHttp) { + switch (requestHttp.getRequestType()) { + case APACHE_HTTP: + return new OcrDiscernApacheHttpRequestExecutor(requestHttp); + default: + return null; + } + } +}