mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-05-04 12:47:46 +08:00
🆕 #1685 小程序增加图像处理相关接口
This commit is contained in:
parent
542b633c70
commit
14dfe30bf6
@ -1,9 +1,9 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
package me.chanjar.weixin.common.api;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcAiCropResult;
|
||||
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcQrCodeResult;
|
||||
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcSuperResolutionResult;
|
||||
import me.chanjar.weixin.common.bean.imgproc.WxImgProcAiCropResult;
|
||||
import me.chanjar.weixin.common.bean.imgproc.WxImgProcQrCodeResult;
|
||||
import me.chanjar.weixin.common.bean.imgproc.WxImgProcSuperResolutionResult;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@ -13,7 +13,7 @@ import java.io.File;
|
||||
*
|
||||
* @author Theo Nie
|
||||
*/
|
||||
public interface WxMpImgProcService {
|
||||
public interface WxImgProcService {
|
||||
|
||||
/**
|
||||
* 二维码/条码识别接口
|
||||
@ -27,7 +27,7 @@ public interface WxMpImgProcService {
|
||||
* @return WxMpImgProcQrCodeResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcQrCodeResult qrCode(String imgUrl) throws WxErrorException;
|
||||
WxImgProcQrCodeResult qrCode(String imgUrl) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 二维码/条码识别接口
|
||||
@ -41,7 +41,7 @@ public interface WxMpImgProcService {
|
||||
* @return WxMpImgProcQrCodeResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcQrCodeResult qrCode(File imgFile) throws WxErrorException;
|
||||
WxImgProcQrCodeResult qrCode(File imgFile) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 图片高清化接口
|
||||
@ -55,7 +55,7 @@ public interface WxMpImgProcService {
|
||||
* @return WxMpImgProcSuperResolutionResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcSuperResolutionResult superResolution(String imgUrl) throws WxErrorException;
|
||||
WxImgProcSuperResolutionResult superResolution(String imgUrl) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 图片高清化接口
|
||||
@ -69,7 +69,7 @@ public interface WxMpImgProcService {
|
||||
* @return WxMpImgProcSuperResolutionResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcSuperResolutionResult superResolution(File imgFile) throws WxErrorException;
|
||||
WxImgProcSuperResolutionResult superResolution(File imgFile) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 图片智能裁剪接口
|
||||
@ -81,7 +81,7 @@ public interface WxMpImgProcService {
|
||||
* @return WxMpImgProcAiCropResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcAiCropResult aiCrop(String imgUrl) throws WxErrorException;
|
||||
WxImgProcAiCropResult aiCrop(String imgUrl) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 图片智能裁剪接口
|
||||
@ -93,7 +93,7 @@ public interface WxMpImgProcService {
|
||||
* @return WxMpImgProcAiCropResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcAiCropResult aiCrop(String imgUrl, String ratios) throws WxErrorException;
|
||||
WxImgProcAiCropResult aiCrop(String imgUrl, String ratios) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 图片智能裁剪接口
|
||||
@ -105,7 +105,7 @@ public interface WxMpImgProcService {
|
||||
* @return WxMpImgProcAiCropResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcAiCropResult aiCrop(File imgFile) throws WxErrorException;
|
||||
WxImgProcAiCropResult aiCrop(File imgFile) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 图片智能裁剪接口
|
||||
@ -117,5 +117,5 @@ public interface WxMpImgProcService {
|
||||
* @return WxMpImgProcAiCropResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcAiCropResult aiCrop(File imgFile, String ratios) throws WxErrorException;
|
||||
WxImgProcAiCropResult aiCrop(File imgFile, String ratios) throws WxErrorException;
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
package me.chanjar.weixin.mp.bean.imgproc;
|
||||
package me.chanjar.weixin.common.bean.imgproc;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
@ -11,50 +11,59 @@ import java.util.List;
|
||||
* @author Theo Nie
|
||||
*/
|
||||
@Data
|
||||
public class WxMpImgProcAiCropResult implements Serializable {
|
||||
public class WxImgProcAiCropResult implements Serializable {
|
||||
private static final long serialVersionUID = -6470673963772979463L;
|
||||
|
||||
@SerializedName("img_size")
|
||||
private ImgSize imgSize;
|
||||
|
||||
@SerializedName("results")
|
||||
private List<Results> results;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return WxMpGsonBuilder.create().toJson(this);
|
||||
return WxGsonBuilder.create().toJson(this);
|
||||
}
|
||||
|
||||
public static WxMpImgProcAiCropResult fromJson(String json) {
|
||||
return WxMpGsonBuilder.create().fromJson(json, WxMpImgProcAiCropResult.class);
|
||||
public static WxImgProcAiCropResult fromJson(String json) {
|
||||
return WxGsonBuilder.create().fromJson(json, WxImgProcAiCropResult.class);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class ImgSize {
|
||||
public static class ImgSize implements Serializable {
|
||||
private static final long serialVersionUID = -6470673963772979463L;
|
||||
|
||||
@SerializedName("w")
|
||||
private int w;
|
||||
|
||||
@SerializedName("h")
|
||||
private int h;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return WxMpGsonBuilder.create().toJson(this);
|
||||
return WxGsonBuilder.create().toJson(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Results {
|
||||
public static class Results implements Serializable {
|
||||
private static final long serialVersionUID = -6470673963772979463L;
|
||||
|
||||
@SerializedName("crop_left")
|
||||
private int cropLeft;
|
||||
|
||||
@SerializedName("crop_top")
|
||||
private int cropTop;
|
||||
|
||||
@SerializedName("crop_right")
|
||||
private int cropRight;
|
||||
|
||||
@SerializedName("crop_bottom")
|
||||
private int cropBottom;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return WxMpGsonBuilder.create().toJson(this);
|
||||
return WxGsonBuilder.create().toJson(this);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,28 +1,31 @@
|
||||
package me.chanjar.weixin.mp.bean.imgproc;
|
||||
package me.chanjar.weixin.common.bean.imgproc;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 二维码/条码识别返回结果
|
||||
*
|
||||
* @author Theo Nie
|
||||
*/
|
||||
@Data
|
||||
public class WxMpImgProcQrCodeResult implements Serializable {
|
||||
|
||||
public class WxImgProcQrCodeResult implements Serializable {
|
||||
private static final long serialVersionUID = -1194154790100866123L;
|
||||
|
||||
@SerializedName("img_size")
|
||||
private ImgSize imgSize;
|
||||
|
||||
@SerializedName("code_results")
|
||||
private List<CodeResults> codeResults;
|
||||
|
||||
@Data
|
||||
public static class ImgSize implements Serializable{
|
||||
public static class ImgSize implements Serializable {
|
||||
private static final long serialVersionUID = -8847603245514017839L;
|
||||
|
||||
@SerializedName("w")
|
||||
private int w;
|
||||
@SerializedName("h")
|
||||
@ -30,64 +33,71 @@ public class WxMpImgProcQrCodeResult implements Serializable {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return WxMpGsonBuilder.create().toJson(this);
|
||||
return WxGsonBuilder.create().toJson(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class CodeResults implements Serializable{
|
||||
public static class CodeResults implements Serializable {
|
||||
private static final long serialVersionUID = -6138135951229076759L;
|
||||
|
||||
@SerializedName("type_name")
|
||||
private String typeName;
|
||||
|
||||
@SerializedName("data")
|
||||
private String data;
|
||||
|
||||
@SerializedName("pos")
|
||||
private Pos pos;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return WxMpGsonBuilder.create().toJson(this);
|
||||
return WxGsonBuilder.create().toJson(this);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Pos implements Serializable{
|
||||
public static class Pos implements Serializable {
|
||||
private static final long serialVersionUID = 7754894061212819602L;
|
||||
@SerializedName("left_top")
|
||||
private Coordinate leftTop;
|
||||
|
||||
@SerializedName("right_top")
|
||||
private Coordinate rightTop;
|
||||
|
||||
@SerializedName("right_bottom")
|
||||
private Coordinate rightBottom;
|
||||
|
||||
@SerializedName("left_bottom")
|
||||
private Coordinate leftBottom;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return WxMpGsonBuilder.create().toJson(this);
|
||||
return WxGsonBuilder.create().toJson(this);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Coordinate implements Serializable{
|
||||
public static class Coordinate implements Serializable {
|
||||
private static final long serialVersionUID = 8930443668927359677L;
|
||||
@SerializedName("x")
|
||||
private int x;
|
||||
|
||||
@SerializedName("y")
|
||||
private int y;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return WxMpGsonBuilder.create().toJson(this);
|
||||
return WxGsonBuilder.create().toJson(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static WxMpImgProcQrCodeResult fromJson(String json) {
|
||||
return WxMpGsonBuilder.create().fromJson(json, WxMpImgProcQrCodeResult.class);
|
||||
public static WxImgProcQrCodeResult fromJson(String json) {
|
||||
return WxGsonBuilder.create().fromJson(json, WxImgProcQrCodeResult.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return WxMpGsonBuilder.create().toJson(this);
|
||||
return WxGsonBuilder.create().toJson(this);
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package me.chanjar.weixin.common.bean.imgproc;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 图片高清化返回结果
|
||||
* @author Theo Nie
|
||||
*/
|
||||
@Data
|
||||
public class WxImgProcSuperResolutionResult implements Serializable {
|
||||
private static final long serialVersionUID = 8007440280170407021L;
|
||||
|
||||
@SerializedName("media_id")
|
||||
private String mediaId;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return WxGsonBuilder.create().toJson(this);
|
||||
}
|
||||
|
||||
public static WxImgProcSuperResolutionResult fromJson(String json) {
|
||||
return WxGsonBuilder.create().fromJson(json, WxImgProcSuperResolutionResult.class);
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package cn.binarywang.wx.miniapp.api;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
||||
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||
import me.chanjar.weixin.common.api.WxImgProcService;
|
||||
import me.chanjar.weixin.common.api.WxOcrService;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.service.WxService;
|
||||
@ -295,4 +296,12 @@ public interface WxMaService extends WxService {
|
||||
* @return 。
|
||||
*/
|
||||
WxOcrService getOcrService();
|
||||
|
||||
/**
|
||||
* 返回图像处理接口的实现类对象,以方便调用其各个接口.
|
||||
*
|
||||
* @return WxImgProcService
|
||||
*/
|
||||
WxImgProcService getImgProcService();
|
||||
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import com.google.common.collect.ImmutableMap;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.api.WxImgProcService;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.api.WxOcrService;
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
@ -59,6 +60,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
|
||||
private final WxMaLiveService liveService = new WxMaLiveServiceImpl(this);
|
||||
private final WxMaLiveGoodsService liveGoodsService = new WxMaLiveGoodsServiceImpl(this);
|
||||
private final WxOcrService ocrService = new WxMaOcrServiceImpl(this);
|
||||
private final WxImgProcService imgProcService = new WxMaImgProcServiceImpl(this);
|
||||
|
||||
private int retrySleepMillis = 1000;
|
||||
private int maxRetryTimes = 5;
|
||||
@ -408,4 +410,9 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
|
||||
return this.ocrService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxImgProcService getImgProcService() {
|
||||
return this.imgProcService;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,131 @@
|
||||
package cn.binarywang.wx.miniapp.api.impl;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.common.api.WxImgProcService;
|
||||
import me.chanjar.weixin.common.bean.imgproc.WxImgProcAiCropResult;
|
||||
import me.chanjar.weixin.common.bean.imgproc.WxImgProcQrCodeResult;
|
||||
import me.chanjar.weixin.common.bean.imgproc.WxImgProcSuperResolutionResult;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.requestexecuter.ocr.OcrDiscernRequestExecutor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
|
||||
/**
|
||||
* 图像处理接口实现.
|
||||
*
|
||||
* @author Theo Nie
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class WxMaImgProcServiceImpl implements WxImgProcService {
|
||||
/**
|
||||
* 二维码/条码识别
|
||||
*/
|
||||
private static final String QRCODE = "/cv/img/qrcode?img_url=%s";
|
||||
|
||||
/**
|
||||
* 二维码/条码识别(文件)
|
||||
*/
|
||||
private static final String FILE_QRCODE = "/cv/img/qrcode";
|
||||
|
||||
/**
|
||||
* 图片高清化
|
||||
*/
|
||||
private static final String SUPER_RESOLUTION = "/cv/img/superresolution?img_url=%s";
|
||||
|
||||
/**
|
||||
* 图片高清化(文件)
|
||||
*/
|
||||
private static final String FILE_SUPER_RESOLUTION = "/cv/img/superresolution";
|
||||
|
||||
/**
|
||||
* 图片智能裁剪
|
||||
*/
|
||||
private static final String AI_CROP = "/cv/img/aicrop?img_url=%s&ratios=%s";
|
||||
|
||||
/**
|
||||
* 图片智能裁剪(文件)
|
||||
*/
|
||||
private static final String FILE_AI_CROP = "/cv/img/aicrop?ratios=%s";
|
||||
private final WxMaService service;
|
||||
|
||||
@Override
|
||||
public WxImgProcQrCodeResult qrCode(String imgUrl) throws WxErrorException {
|
||||
try {
|
||||
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
//ignore
|
||||
}
|
||||
|
||||
final String result = this.service.get(String.format(QRCODE, imgUrl), null);
|
||||
return WxImgProcQrCodeResult.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxImgProcQrCodeResult qrCode(File imgFile) throws WxErrorException {
|
||||
String result = this.service.execute(OcrDiscernRequestExecutor.create(this.service.getRequestHttp()),
|
||||
FILE_QRCODE, imgFile);
|
||||
return WxImgProcQrCodeResult.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxImgProcSuperResolutionResult superResolution(String imgUrl) throws WxErrorException {
|
||||
try {
|
||||
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
//ignore
|
||||
}
|
||||
|
||||
final String result = this.service.get(String.format(SUPER_RESOLUTION, imgUrl), null);
|
||||
return WxImgProcSuperResolutionResult.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxImgProcSuperResolutionResult superResolution(File imgFile) throws WxErrorException {
|
||||
String result = this.service.execute(OcrDiscernRequestExecutor.create(this.service.getRequestHttp()),
|
||||
FILE_SUPER_RESOLUTION, imgFile);
|
||||
return WxImgProcSuperResolutionResult.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxImgProcAiCropResult aiCrop(String imgUrl) throws WxErrorException {
|
||||
return this.aiCrop(imgUrl, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxImgProcAiCropResult aiCrop(String imgUrl, String ratios) throws WxErrorException {
|
||||
try {
|
||||
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
//ignore
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(ratios)) {
|
||||
ratios = "";
|
||||
}
|
||||
|
||||
final String result = this.service.get(String.format(AI_CROP, imgUrl, ratios), null);
|
||||
return WxImgProcAiCropResult.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxImgProcAiCropResult aiCrop(File imgFile) throws WxErrorException {
|
||||
return this.aiCrop(imgFile, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxImgProcAiCropResult aiCrop(File imgFile, String ratios) throws WxErrorException {
|
||||
if (StringUtils.isEmpty(ratios)) {
|
||||
ratios = "";
|
||||
}
|
||||
|
||||
String result = this.service.execute(OcrDiscernRequestExecutor.create(this.service.getRequestHttp()),
|
||||
String.format(FILE_AI_CROP, ratios), imgFile);
|
||||
return WxImgProcAiCropResult.fromJson(result);
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxImgProcService;
|
||||
import me.chanjar.weixin.common.api.WxOcrService;
|
||||
import me.chanjar.weixin.common.bean.WxJsapiSignature;
|
||||
import me.chanjar.weixin.common.bean.WxNetCheckResult;
|
||||
@ -561,9 +562,9 @@ public interface WxMpService extends WxService {
|
||||
/**
|
||||
* 返回图像处理接口的实现类对象,以方便调用其各个接口.
|
||||
*
|
||||
* @return WxMpImgProcService
|
||||
* @return WxImgProcService
|
||||
*/
|
||||
WxMpImgProcService getImgProcService();
|
||||
WxImgProcService getImgProcService();
|
||||
|
||||
/**
|
||||
* .
|
||||
@ -696,7 +697,7 @@ public interface WxMpService extends WxService {
|
||||
*
|
||||
* @param imgProcService .
|
||||
*/
|
||||
void setImgProcService(WxMpImgProcService imgProcService);
|
||||
void setImgProcService(WxImgProcService imgProcService);
|
||||
|
||||
/**
|
||||
* 返回评论数据管理接口方法的实现类对象,以方便调用其各个接口.
|
||||
|
@ -7,6 +7,7 @@ import com.google.gson.JsonObject;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.api.WxImgProcService;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.api.WxOcrService;
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
@ -71,7 +72,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
private WxMpMarketingService marketingService = new WxMpMarketingServiceImpl(this);
|
||||
private WxMpCommentService commentService = new WxMpCommentServiceImpl(this);
|
||||
private WxOcrService ocrService = new WxMpOcrServiceImpl(this);
|
||||
private WxMpImgProcService imgProcService = new WxMpImgProcServiceImpl(this);
|
||||
private WxImgProcService imgProcService = new WxMpImgProcServiceImpl(this);
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ -696,12 +697,12 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpImgProcService getImgProcService() {
|
||||
public WxImgProcService getImgProcService() {
|
||||
return this.imgProcService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setImgProcService(WxMpImgProcService imgProcService) {
|
||||
public void setImgProcService(WxImgProcService imgProcService) {
|
||||
this.imgProcService = imgProcService;
|
||||
}
|
||||
}
|
||||
|
@ -2,11 +2,11 @@ package me.chanjar.weixin.mp.api.impl;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.api.WxMpImgProcService;
|
||||
import me.chanjar.weixin.common.api.WxImgProcService;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcAiCropResult;
|
||||
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcQrCodeResult;
|
||||
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcSuperResolutionResult;
|
||||
import me.chanjar.weixin.common.bean.imgproc.WxImgProcAiCropResult;
|
||||
import me.chanjar.weixin.common.bean.imgproc.WxImgProcQrCodeResult;
|
||||
import me.chanjar.weixin.common.bean.imgproc.WxImgProcSuperResolutionResult;
|
||||
import me.chanjar.weixin.common.requestexecuter.ocr.OcrDiscernRequestExecutor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
@ -27,29 +27,31 @@ import static me.chanjar.weixin.mp.enums.WxMpApiUrl.ImgProc.SUPER_RESOLUTION;
|
||||
* @author Theo Nie
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class WxMpImgProcServiceImpl implements WxMpImgProcService {
|
||||
public class WxMpImgProcServiceImpl implements WxImgProcService {
|
||||
private final WxMpService wxMpService;
|
||||
|
||||
@Override
|
||||
public WxMpImgProcQrCodeResult qrCode(String imgUrl) throws WxErrorException {
|
||||
public WxImgProcQrCodeResult qrCode(String imgUrl) throws WxErrorException {
|
||||
try {
|
||||
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
//ignore
|
||||
}
|
||||
|
||||
final String result = this.wxMpService.get(String.format(QRCODE.getUrl(this.wxMpService.getWxMpConfigStorage()), imgUrl), null);
|
||||
return WxMpImgProcQrCodeResult.fromJson(result);
|
||||
String result = this.wxMpService.get(String.format(QRCODE.getUrl(this.wxMpService.getWxMpConfigStorage()), imgUrl),
|
||||
null);
|
||||
return WxImgProcQrCodeResult.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpImgProcQrCodeResult qrCode(File imgFile) throws WxErrorException {
|
||||
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_QRCODE.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile);
|
||||
return WxMpImgProcQrCodeResult.fromJson(result);
|
||||
public WxImgProcQrCodeResult qrCode(File imgFile) throws WxErrorException {
|
||||
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()),
|
||||
FILE_QRCODE.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile);
|
||||
return WxImgProcQrCodeResult.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpImgProcSuperResolutionResult superResolution(String imgUrl) throws WxErrorException {
|
||||
public WxImgProcSuperResolutionResult superResolution(String imgUrl) throws WxErrorException {
|
||||
try {
|
||||
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
@ -57,22 +59,23 @@ public class WxMpImgProcServiceImpl implements WxMpImgProcService {
|
||||
}
|
||||
|
||||
final String result = this.wxMpService.get(String.format(SUPER_RESOLUTION.getUrl(this.wxMpService.getWxMpConfigStorage()), imgUrl), null);
|
||||
return WxMpImgProcSuperResolutionResult.fromJson(result);
|
||||
return WxImgProcSuperResolutionResult.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpImgProcSuperResolutionResult superResolution(File imgFile) throws WxErrorException {
|
||||
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_SUPER_RESOLUTION.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile);
|
||||
return WxMpImgProcSuperResolutionResult.fromJson(result);
|
||||
public WxImgProcSuperResolutionResult superResolution(File imgFile) throws WxErrorException {
|
||||
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()),
|
||||
FILE_SUPER_RESOLUTION.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile);
|
||||
return WxImgProcSuperResolutionResult.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpImgProcAiCropResult aiCrop(String imgUrl) throws WxErrorException {
|
||||
public WxImgProcAiCropResult aiCrop(String imgUrl) throws WxErrorException {
|
||||
return this.aiCrop(imgUrl, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpImgProcAiCropResult aiCrop(String imgUrl, String ratios) throws WxErrorException {
|
||||
public WxImgProcAiCropResult aiCrop(String imgUrl, String ratios) throws WxErrorException {
|
||||
try {
|
||||
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
@ -83,22 +86,24 @@ public class WxMpImgProcServiceImpl implements WxMpImgProcService {
|
||||
ratios = "";
|
||||
}
|
||||
|
||||
final String result = this.wxMpService.get(String.format(AI_CROP.getUrl(this.wxMpService.getWxMpConfigStorage()), imgUrl, ratios), null);
|
||||
return WxMpImgProcAiCropResult.fromJson(result);
|
||||
final String result = this.wxMpService.get(String.format(AI_CROP.getUrl(this.wxMpService.getWxMpConfigStorage()),
|
||||
imgUrl, ratios), null);
|
||||
return WxImgProcAiCropResult.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpImgProcAiCropResult aiCrop(File imgFile) throws WxErrorException {
|
||||
public WxImgProcAiCropResult aiCrop(File imgFile) throws WxErrorException {
|
||||
return this.aiCrop(imgFile, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpImgProcAiCropResult aiCrop(File imgFile, String ratios) throws WxErrorException {
|
||||
public WxImgProcAiCropResult aiCrop(File imgFile, String ratios) throws WxErrorException {
|
||||
if (StringUtils.isEmpty(ratios)) {
|
||||
ratios = "";
|
||||
}
|
||||
|
||||
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), String.format(FILE_AI_CROP.getUrl(this.wxMpService.getWxMpConfigStorage()), ratios), imgFile);
|
||||
return WxMpImgProcAiCropResult.fromJson(result);
|
||||
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()),
|
||||
String.format(FILE_AI_CROP.getUrl(this.wxMpService.getWxMpConfigStorage()), ratios), imgFile);
|
||||
return WxImgProcAiCropResult.fromJson(result);
|
||||
}
|
||||
}
|
||||
|
@ -1,28 +0,0 @@
|
||||
package me.chanjar.weixin.mp.bean.imgproc;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 图片高清化返回结果
|
||||
* @author Theo Nie
|
||||
*/
|
||||
@Data
|
||||
public class WxMpImgProcSuperResolutionResult implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 8007440280170407021L;
|
||||
@SerializedName("media_id")
|
||||
private String mediaId;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return WxMpGsonBuilder.create().toJson(this);
|
||||
}
|
||||
|
||||
public static WxMpImgProcSuperResolutionResult fromJson(String json) {
|
||||
return WxMpGsonBuilder.create().fromJson(json, WxMpImgProcSuperResolutionResult.class);
|
||||
}
|
||||
}
|
@ -2,13 +2,13 @@ package me.chanjar.weixin.mp.api.impl;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.mp.api.WxMpImgProcService;
|
||||
import me.chanjar.weixin.common.api.WxImgProcService;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.api.test.ApiTestModule;
|
||||
import me.chanjar.weixin.mp.api.test.TestConstants;
|
||||
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcAiCropResult;
|
||||
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcQrCodeResult;
|
||||
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcSuperResolutionResult;
|
||||
import me.chanjar.weixin.common.bean.imgproc.WxImgProcAiCropResult;
|
||||
import me.chanjar.weixin.common.bean.imgproc.WxImgProcQrCodeResult;
|
||||
import me.chanjar.weixin.common.bean.imgproc.WxImgProcSuperResolutionResult;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@ -29,7 +29,7 @@ public class WxMpImgProcServiceImplTest {
|
||||
|
||||
@Test
|
||||
public void testQrCode() throws WxErrorException {
|
||||
final WxMpImgProcQrCodeResult result = this.mpService.getImgProcService().qrCode("https://gitee.com/binary/weixin-java-tools/raw/master/images/qrcodes/mp.png");
|
||||
final WxImgProcQrCodeResult result = this.mpService.getImgProcService().qrCode("https://gitee.com/binary/weixin-java-tools/raw/master/images/qrcodes/mp.png");
|
||||
assertThat(result).isNotNull();
|
||||
System.out.println(result);
|
||||
}
|
||||
@ -38,14 +38,14 @@ public class WxMpImgProcServiceImplTest {
|
||||
public void testQrCode2() throws Exception {
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
|
||||
File tempFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), TestConstants.FILE_JPG);
|
||||
final WxMpImgProcQrCodeResult result = this.mpService.getImgProcService().qrCode(tempFile);
|
||||
final WxImgProcQrCodeResult result = this.mpService.getImgProcService().qrCode(tempFile);
|
||||
assertThat(result).isNotNull();
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuperResolution() throws WxErrorException {
|
||||
final WxMpImgProcSuperResolutionResult result = this.mpService.getImgProcService().superResolution("https://gitee.com/binary/weixin-java-tools/raw/master/images/qrcodes/mp.png");
|
||||
final WxImgProcSuperResolutionResult result = this.mpService.getImgProcService().superResolution("https://gitee.com/binary/weixin-java-tools/raw/master/images/qrcodes/mp.png");
|
||||
assertThat(result).isNotNull();
|
||||
System.out.println(result);
|
||||
}
|
||||
@ -54,21 +54,21 @@ public class WxMpImgProcServiceImplTest {
|
||||
public void testSuperResolution2() throws Exception {
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
|
||||
File tempFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), TestConstants.FILE_JPG);
|
||||
final WxMpImgProcSuperResolutionResult result = this.mpService.getImgProcService().superResolution(tempFile);
|
||||
final WxImgProcSuperResolutionResult result = this.mpService.getImgProcService().superResolution(tempFile);
|
||||
assertThat(result).isNotNull();
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAiCrop() throws WxErrorException {
|
||||
final WxMpImgProcAiCropResult result = this.mpService.getImgProcService().aiCrop("https://gitee.com/binary/weixin-java-tools/raw/master/images/qrcodes/mp.png");
|
||||
final WxImgProcAiCropResult result = this.mpService.getImgProcService().aiCrop("https://gitee.com/binary/weixin-java-tools/raw/master/images/qrcodes/mp.png");
|
||||
assertThat(result).isNotNull();
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAiCrop2() throws WxErrorException {
|
||||
final WxMpImgProcAiCropResult result = this.mpService.getImgProcService().aiCrop("https://gitee.com/binary/weixin-java-tools/raw/master/images/qrcodes/mp.png", "1,2.35");
|
||||
final WxImgProcAiCropResult result = this.mpService.getImgProcService().aiCrop("https://gitee.com/binary/weixin-java-tools/raw/master/images/qrcodes/mp.png", "1,2.35");
|
||||
assertThat(result).isNotNull();
|
||||
System.out.println(result);
|
||||
}
|
||||
@ -77,7 +77,7 @@ public class WxMpImgProcServiceImplTest {
|
||||
public void testAiCrop3() throws Exception {
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
|
||||
File tempFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), TestConstants.FILE_JPG);
|
||||
final WxMpImgProcAiCropResult result = this.mpService.getImgProcService().aiCrop(tempFile);
|
||||
final WxImgProcAiCropResult result = this.mpService.getImgProcService().aiCrop(tempFile);
|
||||
assertThat(result).isNotNull();
|
||||
System.out.println(result);
|
||||
}
|
||||
@ -86,7 +86,7 @@ public class WxMpImgProcServiceImplTest {
|
||||
public void testAiCrop4() throws Exception {
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
|
||||
File tempFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), TestConstants.FILE_JPG);
|
||||
final WxMpImgProcAiCropResult result = this.mpService.getImgProcService().aiCrop(tempFile, "1,2.35,3.5");
|
||||
final WxImgProcAiCropResult result = this.mpService.getImgProcService().aiCrop(tempFile, "1,2.35,3.5");
|
||||
assertThat(result).isNotNull();
|
||||
System.out.println(result);
|
||||
}
|
||||
@ -159,8 +159,8 @@ public class WxMpImgProcServiceImplTest {
|
||||
" }\n" +
|
||||
"}";
|
||||
when(wxService.get(anyString(), anyString())).thenReturn(returnJson);
|
||||
final WxMpImgProcService wxMpImgProcService = new WxMpImgProcServiceImpl(wxService);
|
||||
final WxMpImgProcQrCodeResult result = wxMpImgProcService.qrCode("abc");
|
||||
final WxImgProcService wxMpImgProcService = new WxMpImgProcServiceImpl(wxService);
|
||||
final WxImgProcQrCodeResult result = wxMpImgProcService.qrCode("abc");
|
||||
assertThat(result).isNotNull();
|
||||
System.out.println(result);
|
||||
}
|
||||
@ -173,8 +173,8 @@ public class WxMpImgProcServiceImplTest {
|
||||
" \"media_id\": \"6WXsIXkG7lXuDLspD9xfm5dsvHzb0EFl0li6ySxi92ap8Vl3zZoD9DpOyNudeJGB\"\n" +
|
||||
"}";
|
||||
when(wxService.get(anyString(), anyString())).thenReturn(returnJson);
|
||||
final WxMpImgProcService wxMpImgProcService = new WxMpImgProcServiceImpl(wxService);
|
||||
final WxMpImgProcSuperResolutionResult result = wxMpImgProcService.superResolution("abc");
|
||||
final WxImgProcService wxMpImgProcService = new WxMpImgProcServiceImpl(wxService);
|
||||
final WxImgProcSuperResolutionResult result = wxMpImgProcService.superResolution("abc");
|
||||
assertThat(result).isNotNull();
|
||||
System.out.println(result);
|
||||
}
|
||||
@ -204,8 +204,8 @@ public class WxMpImgProcServiceImplTest {
|
||||
" }\n" +
|
||||
"}";
|
||||
when(wxService.get(anyString(), anyString())).thenReturn(returnJson);
|
||||
final WxMpImgProcService wxMpImgProcService = new WxMpImgProcServiceImpl(wxService);
|
||||
final WxMpImgProcAiCropResult result = wxMpImgProcService.aiCrop("abc");
|
||||
final WxImgProcService wxMpImgProcService = new WxMpImgProcServiceImpl(wxService);
|
||||
final WxImgProcAiCropResult result = wxMpImgProcService.aiCrop("abc");
|
||||
assertThat(result).isNotNull();
|
||||
System.out.println(result);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user