Skip to content

Commit 7ac670b

Browse files
committed
#1002 增加微信OCR身份证识别接口
1 parent 158171c commit 7ac670b

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package me.chanjar.weixin.mp.api;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import me.chanjar.weixin.common.error.WxErrorException;
6+
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrIdCardResult;
7+
8+
import java.io.File;
9+
10+
/**
11+
* 基于小程序或 H5 的身份证、银行卡、行驶证 OCR 识别.
12+
* https://mp.weixin.qq.com/wiki?t=resource/res_main&id=21516712284rHWMX
13+
*
14+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
15+
* @date 2019-06-22
16+
*/
17+
public interface WxMpOcrService {
18+
@AllArgsConstructor
19+
@Getter
20+
enum ImageType {
21+
/**
22+
* 拍照模型,带背景的图片.
23+
*/
24+
PHOTO("photo"),
25+
/**
26+
* 扫描模式,不带背景的图片.
27+
*/
28+
SCAN("scan");
29+
30+
private String type;
31+
}
32+
33+
/**
34+
* 身份证OCR识别接口.
35+
*
36+
* @param imgType 图片类型
37+
* @param imgUrl 图片url地址
38+
* @throws WxErrorException .
39+
*/
40+
WxMpOcrIdCardResult idCard(ImageType imgType, String imgUrl) throws WxErrorException;
41+
42+
/**
43+
* 身份证OCR识别接口.
44+
*
45+
* @param imgType 图片类型
46+
* @param imgFile 图片文件对象
47+
* @throws WxErrorException .
48+
*/
49+
WxMpOcrIdCardResult idCard(ImageType imgType, File imgFile) throws WxErrorException;
50+
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java

+9
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,13 @@ public interface WxMpService {
493493
*/
494494
WxMpWifiService getWifiService();
495495

496+
/**
497+
* 返回WIFI接口方法的实现类对象,以方便调用其各个接口.
498+
*
499+
* @return WxMpWifiService
500+
*/
501+
WxMpOcrService getOcrService();
502+
496503
void setKefuService(WxMpKefuService kefuService);
497504

498505
void setMaterialService(WxMpMaterialService materialService);
@@ -527,6 +534,8 @@ public interface WxMpService {
527534

528535
void setMarketingService(WxMpMarketingService marketingService);
529536

537+
void setOcrService(WxMpOcrService ocrService);
538+
530539
/**
531540
* 返回评论数据管理接口方法的实现类对象,以方便调用其各个接口.
532541
*

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java

+11
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
6363
private WxMpWifiService wifiService = new WxMpWifiServiceImpl(this);
6464
private WxMpMarketingService marketingService = new WxMpMarketingServiceImpl(this);
6565
private WxMpCommentService commentService = new WxMpCommentServiceImpl(this);
66+
private WxMpOcrService ocrService = new WxMpOcrServiceImpl(this);
6667

6768
private Map<String, WxMpConfigStorage> configStorageMap;
6869

@@ -600,6 +601,11 @@ public WxMpWifiService getWifiService() {
600601
return this.wifiService;
601602
}
602603

604+
@Override
605+
public WxMpOcrService getOcrService() {
606+
return this.ocrService;
607+
}
608+
603609
@Override
604610
public WxMpMarketingService getMarketingService() {
605611
return this.marketingService;
@@ -610,6 +616,11 @@ public void setMarketingService(WxMpMarketingService marketingService) {
610616
this.marketingService = marketingService;
611617
}
612618

619+
@Override
620+
public void setOcrService(WxMpOcrService ocrService) {
621+
this.ocrService = ocrService;
622+
}
623+
613624
@Override
614625
public WxMpCommentService getCommentService() {
615626
return this.commentService;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package me.chanjar.weixin.mp.api.impl;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import me.chanjar.weixin.common.error.WxErrorException;
5+
import me.chanjar.weixin.mp.api.WxMpOcrService;
6+
import me.chanjar.weixin.mp.api.WxMpService;
7+
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrIdCardResult;
8+
9+
import java.io.File;
10+
import java.io.UnsupportedEncodingException;
11+
import java.net.URLEncoder;
12+
import java.nio.charset.StandardCharsets;
13+
14+
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.IDCARD;
15+
16+
/**
17+
* ocr 接口实现.
18+
*
19+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
20+
* @date 2019-06-22
21+
*/
22+
@RequiredArgsConstructor
23+
public class WxMpOcrServiceImpl implements WxMpOcrService {
24+
private final WxMpService wxMpService;
25+
26+
@Override
27+
public WxMpOcrIdCardResult idCard(ImageType imgType, String imgUrl) throws WxErrorException {
28+
try {
29+
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
30+
} catch (UnsupportedEncodingException e) {
31+
// ignore cannot happen
32+
}
33+
34+
final String result = this.wxMpService.get(String.format(IDCARD.getUrl(this.wxMpService.getWxMpConfigStorage()),
35+
imgType.getType(), imgUrl), null);
36+
return WxMpOcrIdCardResult.fromJson(result);
37+
}
38+
39+
@Override
40+
public WxMpOcrIdCardResult idCard(ImageType imgType, File imgFile) {
41+
return null;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package me.chanjar.weixin.mp.bean.ocr;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
6+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
7+
8+
import java.io.Serializable;
9+
10+
/**
11+
* OCR身份证识别结果.
12+
*
13+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
14+
* @date 2019-06-23
15+
*/
16+
@Data
17+
public class WxMpOcrIdCardResult implements Serializable {
18+
private static final long serialVersionUID = 8184352486986729980L;
19+
20+
@SerializedName("type")
21+
private String type;
22+
@SerializedName("name")
23+
private String name;
24+
@SerializedName("id")
25+
private String id;
26+
@SerializedName("valid_date")
27+
private String validDate;
28+
29+
public static WxMpOcrIdCardResult fromJson(String json) {
30+
return WxMpGsonBuilder.create().fromJson(json, WxMpOcrIdCardResult.class);
31+
}
32+
33+
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/enums/WxMpApiUrl.java

+20
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,26 @@ public String getUrl(WxMpConfigStorage config) {
426426
}
427427
}
428428

429+
@AllArgsConstructor
430+
enum Ocr implements WxMpApiUrl {
431+
/**
432+
* 身份证识别.
433+
*/
434+
IDCARD(API_DEFAULT_HOST_URL, "/cv/ocr/idcard?type=%s&img_url=%s");
435+
436+
private String prefix;
437+
private String path;
438+
439+
@Override
440+
public String getUrl(WxMpConfigStorage config) {
441+
if (config == null) {
442+
return buildUrl(null, prefix, path);
443+
}
444+
445+
return buildUrl(config.getHostConfig(), prefix, path);
446+
}
447+
}
448+
429449
@AllArgsConstructor
430450
enum Card implements WxMpApiUrl {
431451
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package me.chanjar.weixin.mp.api.impl;
2+
3+
import me.chanjar.weixin.common.error.WxErrorException;
4+
import me.chanjar.weixin.mp.api.WxMpOcrService;
5+
import me.chanjar.weixin.mp.api.WxMpService;
6+
import me.chanjar.weixin.mp.api.test.ApiTestModule;
7+
import me.chanjar.weixin.mp.bean.ocr.WxMpOcrIdCardResult;
8+
import org.testng.annotations.BeforeTest;
9+
import org.testng.annotations.Guice;
10+
import org.testng.annotations.Test;
11+
12+
import javax.inject.Inject;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
import static org.mockito.Matchers.anyString;
16+
import static org.mockito.Mockito.mock;
17+
import static org.mockito.Mockito.when;
18+
19+
/**
20+
* 测试类.
21+
*
22+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
23+
* @date 2019-06-22
24+
*/
25+
@Test
26+
@Guice(modules = ApiTestModule.class)
27+
public class WxMpOcrServiceImplTest {
28+
@Inject
29+
private WxMpService mpService;
30+
31+
@Test
32+
public void testIdCard() throws WxErrorException {
33+
final WxMpOcrIdCardResult result = this.mpService.getOcrService().idCard(WxMpOcrService.ImageType.PHOTO,
34+
"http://www.baidu.com");
35+
assertThat(result).isNotNull();
36+
System.out.println(result);
37+
}
38+
39+
public static class MockTest {
40+
private WxMpService wxService = mock(WxMpService.class);
41+
42+
@Test
43+
public void testIdCard() throws Exception {
44+
String returnJson = "{\"type\":\"Back\",\"name\":\"张三\",\"id\":\"110101199909090099\",\"valid_date\":\"20110101-20210201\"}";
45+
46+
when(wxService.get(anyString(), anyString())).thenReturn(returnJson);
47+
final WxMpOcrServiceImpl wxMpOcrService = new WxMpOcrServiceImpl(wxService);
48+
49+
final WxMpOcrIdCardResult result = wxMpOcrService.idCard(WxMpOcrService.ImageType.PHOTO, "abc");
50+
assertThat(result).isNotNull();
51+
System.out.println(result);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)