Skip to content

Commit 829ca6f

Browse files
TheoNiebinarywang
authored andcommitted
#1235 微信公众号模块增加图像处理接口
* 图像处理二维码识别接口 * 图像处理图像高清化接口 * 公众号图像处理智能裁剪接口
1 parent 902ba61 commit 829ca6f

File tree

9 files changed

+688
-0
lines changed

9 files changed

+688
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package me.chanjar.weixin.mp.api;
2+
3+
import me.chanjar.weixin.common.error.WxErrorException;
4+
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcAiCropResult;
5+
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcQrCodeResult;
6+
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcSuperResolutionResult;
7+
8+
import java.io.File;
9+
10+
/**
11+
* 多项图像处理能力相关的API.
12+
* https://developers.weixin.qq.com/doc/offiaccount/Intelligent_Interface/Img_Proc.html
13+
*
14+
* @author Theo Nie
15+
*/
16+
public interface WxMpImgProcService {
17+
18+
/**
19+
* 二维码/条码识别接口
20+
* 说明:
21+
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
22+
* 2.文件大小限制:小于2M
23+
* 3.支持条码、二维码、DataMatrix和PDF417的识别。
24+
* 4.二维码、DataMatrix会返回位置坐标,条码和PDF417暂不返回位置坐标。
25+
*
26+
* @param imgUrl 图片url地址
27+
* @return WxMpImgProcQrCodeResult
28+
* @throws WxErrorException .
29+
*/
30+
WxMpImgProcQrCodeResult qrCode(String imgUrl) throws WxErrorException;
31+
32+
/**
33+
* 二维码/条码识别接口
34+
* 说明:
35+
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
36+
* 2.文件大小限制:小于2M
37+
* 3.支持条码、二维码、DataMatrix和PDF417的识别。
38+
* 4.二维码、DataMatrix会返回位置坐标,条码和PDF417暂不返回位置坐标。
39+
*
40+
* @param imgFile 图片文件对象
41+
* @return WxMpImgProcQrCodeResult
42+
* @throws WxErrorException .
43+
*/
44+
WxMpImgProcQrCodeResult qrCode(File imgFile) throws WxErrorException;
45+
46+
/**
47+
* 图片高清化接口
48+
* 说明:
49+
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
50+
* 2.文件大小限制:小于2M
51+
* 3.目前支持将图片超分辨率高清化2倍,即生成图片分辨率为原图2倍大小
52+
* 返回的media_id有效期为3天,期间可以通过“获取临时素材”接口获取图片二进制
53+
*
54+
* @param imgUrl 图片url地址
55+
* @return WxMpImgProcSuperResolutionResult
56+
* @throws WxErrorException .
57+
*/
58+
WxMpImgProcSuperResolutionResult superResolution(String imgUrl) throws WxErrorException;
59+
60+
/**
61+
* 图片高清化接口
62+
* 说明:
63+
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
64+
* 2.文件大小限制:小于2M
65+
* 3.目前支持将图片超分辨率高清化2倍,即生成图片分辨率为原图2倍大小
66+
* 返回的media_id有效期为3天,期间可以通过“获取临时素材”接口获取图片二进制
67+
*
68+
* @param imgFile 图片文件对象
69+
* @return WxMpImgProcSuperResolutionResult
70+
* @throws WxErrorException .
71+
*/
72+
WxMpImgProcSuperResolutionResult superResolution(File imgFile) throws WxErrorException;
73+
74+
/**
75+
* 图片智能裁剪接口
76+
* 说明:
77+
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
78+
* 2.文件大小限制:小于2M
79+
* 3.该接口默认使用最佳宽高比
80+
* @param imgUrl 图片url地址
81+
* @return WxMpImgProcAiCropResult
82+
* @throws WxErrorException .
83+
*/
84+
WxMpImgProcAiCropResult aiCrop(String imgUrl) throws WxErrorException;
85+
86+
/**
87+
* 图片智能裁剪接口
88+
* 说明:
89+
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
90+
* 2.文件大小限制:小于2M
91+
* @param imgUrl 图片url地址
92+
* @param ratios 宽高比,最多支持5个,请以英文逗号分隔
93+
* @return WxMpImgProcAiCropResult
94+
* @throws WxErrorException .
95+
*/
96+
WxMpImgProcAiCropResult aiCrop(String imgUrl, String ratios) throws WxErrorException;
97+
98+
/**
99+
* 图片智能裁剪接口
100+
* 说明:
101+
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
102+
* 2.文件大小限制:小于2M
103+
* 3.该接口默认使用最佳宽高比
104+
* @param imgFile 图片文件对象
105+
* @return WxMpImgProcAiCropResult
106+
* @throws WxErrorException .
107+
*/
108+
WxMpImgProcAiCropResult aiCrop(File imgFile) throws WxErrorException;
109+
110+
/**
111+
* 图片智能裁剪接口
112+
* 说明:
113+
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
114+
* 2.文件大小限制:小于2M
115+
* @param imgFile 图片文件对象
116+
* @param ratios 宽高比,最多支持5个,请以英文逗号分隔
117+
* @return WxMpImgProcAiCropResult
118+
* @throws WxErrorException .
119+
*/
120+
WxMpImgProcAiCropResult aiCrop(File imgFile, String ratios) throws WxErrorException;
121+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,12 @@ public interface WxMpService {
522522
*/
523523
WxMpOcrService getOcrService();
524524

525+
/**
526+
* 返回图像处理接口的实现类对象,以方便调用其各个接口.
527+
* @return WxMpImgProcService
528+
*/
529+
WxMpImgProcService getImgProcService();
530+
525531
/**
526532
* .
527533
*
@@ -648,6 +654,13 @@ public interface WxMpService {
648654
*/
649655
void setOcrService(WxMpOcrService ocrService);
650656

657+
/**
658+
* .
659+
*
660+
* @param imgProcService .
661+
*/
662+
void setImgProcService(WxMpImgProcService imgProcService);
663+
651664
/**
652665
* 返回评论数据管理接口方法的实现类对象,以方便调用其各个接口.
653666
*

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
6767
private WxMpMarketingService marketingService = new WxMpMarketingServiceImpl(this);
6868
private WxMpCommentService commentService = new WxMpCommentServiceImpl(this);
6969
private WxMpOcrService ocrService = new WxMpOcrServiceImpl(this);
70+
private WxMpImgProcService imgProcService = new WxMpImgProcServiceImpl(this);
7071

7172
private Map<String, WxMpConfigStorage> configStorageMap;
7273

@@ -647,4 +648,14 @@ public WxMpCommentService getCommentService() {
647648
public void setCommentService(WxMpCommentService commentService) {
648649
this.commentService = commentService;
649650
}
651+
652+
@Override
653+
public WxMpImgProcService getImgProcService() {
654+
return this.imgProcService;
655+
}
656+
657+
@Override
658+
public void setImgProcService(WxMpImgProcService imgProcService) {
659+
this.imgProcService = imgProcService;
660+
}
650661
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.WxMpImgProcService;
6+
import me.chanjar.weixin.mp.api.WxMpService;
7+
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcAiCropResult;
8+
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcQrCodeResult;
9+
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcSuperResolutionResult;
10+
import me.chanjar.weixin.mp.util.requestexecuter.ocr.OcrDiscernRequestExecutor;
11+
import org.apache.commons.lang3.StringUtils;
12+
13+
import java.io.File;
14+
import java.io.UnsupportedEncodingException;
15+
import java.net.URLEncoder;
16+
import java.nio.charset.StandardCharsets;
17+
18+
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.ImgProc.AI_CROP;
19+
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.ImgProc.FILE_AI_CROP;
20+
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.ImgProc.FILE_QRCODE;
21+
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.ImgProc.FILE_SUPER_RESOLUTION;
22+
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.ImgProc.QRCODE;
23+
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.ImgProc.SUPER_RESOLUTION;
24+
25+
/**
26+
* 图像处理接口实现.
27+
* @author Theo Nie
28+
*/
29+
@RequiredArgsConstructor
30+
public class WxMpImgProcServiceImpl implements WxMpImgProcService {
31+
private final WxMpService wxMpService;
32+
33+
@Override
34+
public WxMpImgProcQrCodeResult qrCode(String imgUrl) throws WxErrorException {
35+
try {
36+
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
37+
} catch (UnsupportedEncodingException e) {
38+
//ignore
39+
}
40+
41+
final String result = this.wxMpService.get(String.format(QRCODE.getUrl(this.wxMpService.getWxMpConfigStorage()), imgUrl), null);
42+
return WxMpImgProcQrCodeResult.fromJson(result);
43+
}
44+
45+
@Override
46+
public WxMpImgProcQrCodeResult qrCode(File imgFile) throws WxErrorException {
47+
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_QRCODE.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile);
48+
return WxMpImgProcQrCodeResult.fromJson(result);
49+
}
50+
51+
@Override
52+
public WxMpImgProcSuperResolutionResult superResolution(String imgUrl) throws WxErrorException {
53+
try {
54+
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
55+
} catch (UnsupportedEncodingException e) {
56+
//ignore
57+
}
58+
59+
final String result = this.wxMpService.get(String.format(SUPER_RESOLUTION.getUrl(this.wxMpService.getWxMpConfigStorage()), imgUrl), null);
60+
return WxMpImgProcSuperResolutionResult.fromJson(result);
61+
}
62+
63+
@Override
64+
public WxMpImgProcSuperResolutionResult superResolution(File imgFile) throws WxErrorException {
65+
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_SUPER_RESOLUTION.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile);
66+
return WxMpImgProcSuperResolutionResult.fromJson(result);
67+
}
68+
69+
@Override
70+
public WxMpImgProcAiCropResult aiCrop(String imgUrl) throws WxErrorException {
71+
return this.aiCrop(imgUrl, "");
72+
}
73+
74+
@Override
75+
public WxMpImgProcAiCropResult aiCrop(String imgUrl, String ratios) throws WxErrorException {
76+
try {
77+
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
78+
} catch (UnsupportedEncodingException e) {
79+
//ignore
80+
}
81+
82+
if (StringUtils.isEmpty(ratios)) {
83+
ratios = "";
84+
}
85+
86+
final String result = this.wxMpService.get(String.format(AI_CROP.getUrl(this.wxMpService.getWxMpConfigStorage()), imgUrl, ratios), null);
87+
return WxMpImgProcAiCropResult.fromJson(result);
88+
}
89+
90+
@Override
91+
public WxMpImgProcAiCropResult aiCrop(File imgFile) throws WxErrorException {
92+
return this.aiCrop(imgFile, "");
93+
}
94+
95+
@Override
96+
public WxMpImgProcAiCropResult aiCrop(File imgFile, String ratios) throws WxErrorException {
97+
if (StringUtils.isEmpty(ratios)) {
98+
ratios = "";
99+
}
100+
101+
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), String.format(FILE_AI_CROP.getUrl(this.wxMpService.getWxMpConfigStorage()), ratios), imgFile);
102+
return WxMpImgProcAiCropResult.fromJson(result);
103+
}
104+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package me.chanjar.weixin.mp.bean.imgproc;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
6+
7+
import java.io.Serializable;
8+
import java.util.List;
9+
10+
/**
11+
* @author Theo Nie
12+
*/
13+
@Data
14+
public class WxMpImgProcAiCropResult implements Serializable {
15+
private static final long serialVersionUID = -6470673963772979463L;
16+
17+
@SerializedName("img_size")
18+
private ImgSize imgSize;
19+
@SerializedName("results")
20+
private List<Results> results;
21+
22+
@Override
23+
public String toString() {
24+
return WxMpGsonBuilder.create().toJson(this);
25+
}
26+
27+
public static WxMpImgProcAiCropResult fromJson(String json) {
28+
return WxMpGsonBuilder.create().fromJson(json, WxMpImgProcAiCropResult.class);
29+
}
30+
31+
@Data
32+
public static class ImgSize {
33+
@SerializedName("w")
34+
private int w;
35+
@SerializedName("h")
36+
private int h;
37+
38+
@Override
39+
public String toString() {
40+
return WxMpGsonBuilder.create().toJson(this);
41+
}
42+
}
43+
44+
@Data
45+
public static class Results {
46+
@SerializedName("crop_left")
47+
private int cropLeft;
48+
@SerializedName("crop_top")
49+
private int cropTop;
50+
@SerializedName("crop_right")
51+
private int cropRight;
52+
@SerializedName("crop_bottom")
53+
private int cropBottom;
54+
55+
@Override
56+
public String toString() {
57+
return WxMpGsonBuilder.create().toJson(this);
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)