🆕 #1685 小程序增加图像处理相关接口

This commit is contained in:
Binary Wang
2020-08-08 17:14:09 +08:00
parent 542b633c70
commit 14dfe30bf6
12 changed files with 287 additions and 114 deletions

View File

@ -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.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcAiCropResult; import me.chanjar.weixin.common.bean.imgproc.WxImgProcAiCropResult;
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcQrCodeResult; import me.chanjar.weixin.common.bean.imgproc.WxImgProcQrCodeResult;
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcSuperResolutionResult; import me.chanjar.weixin.common.bean.imgproc.WxImgProcSuperResolutionResult;
import java.io.File; import java.io.File;
@ -13,7 +13,7 @@ import java.io.File;
* *
* @author Theo Nie * @author Theo Nie
*/ */
public interface WxMpImgProcService { public interface WxImgProcService {
/** /**
* 二维码/条码识别接口 * 二维码/条码识别接口
@ -27,7 +27,7 @@ public interface WxMpImgProcService {
* @return WxMpImgProcQrCodeResult * @return WxMpImgProcQrCodeResult
* @throws WxErrorException . * @throws WxErrorException .
*/ */
WxMpImgProcQrCodeResult qrCode(String imgUrl) throws WxErrorException; WxImgProcQrCodeResult qrCode(String imgUrl) throws WxErrorException;
/** /**
* 二维码/条码识别接口 * 二维码/条码识别接口
@ -41,7 +41,7 @@ public interface WxMpImgProcService {
* @return WxMpImgProcQrCodeResult * @return WxMpImgProcQrCodeResult
* @throws WxErrorException . * @throws WxErrorException .
*/ */
WxMpImgProcQrCodeResult qrCode(File imgFile) throws WxErrorException; WxImgProcQrCodeResult qrCode(File imgFile) throws WxErrorException;
/** /**
* 图片高清化接口 * 图片高清化接口
@ -55,7 +55,7 @@ public interface WxMpImgProcService {
* @return WxMpImgProcSuperResolutionResult * @return WxMpImgProcSuperResolutionResult
* @throws WxErrorException . * @throws WxErrorException .
*/ */
WxMpImgProcSuperResolutionResult superResolution(String imgUrl) throws WxErrorException; WxImgProcSuperResolutionResult superResolution(String imgUrl) throws WxErrorException;
/** /**
* 图片高清化接口 * 图片高清化接口
@ -69,7 +69,7 @@ public interface WxMpImgProcService {
* @return WxMpImgProcSuperResolutionResult * @return WxMpImgProcSuperResolutionResult
* @throws WxErrorException . * @throws WxErrorException .
*/ */
WxMpImgProcSuperResolutionResult superResolution(File imgFile) throws WxErrorException; WxImgProcSuperResolutionResult superResolution(File imgFile) throws WxErrorException;
/** /**
* 图片智能裁剪接口 * 图片智能裁剪接口
@ -81,7 +81,7 @@ public interface WxMpImgProcService {
* @return WxMpImgProcAiCropResult * @return WxMpImgProcAiCropResult
* @throws WxErrorException . * @throws WxErrorException .
*/ */
WxMpImgProcAiCropResult aiCrop(String imgUrl) throws WxErrorException; WxImgProcAiCropResult aiCrop(String imgUrl) throws WxErrorException;
/** /**
* 图片智能裁剪接口 * 图片智能裁剪接口
@ -93,7 +93,7 @@ public interface WxMpImgProcService {
* @return WxMpImgProcAiCropResult * @return WxMpImgProcAiCropResult
* @throws WxErrorException . * @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 * @return WxMpImgProcAiCropResult
* @throws WxErrorException . * @throws WxErrorException .
*/ */
WxMpImgProcAiCropResult aiCrop(File imgFile) throws WxErrorException; WxImgProcAiCropResult aiCrop(File imgFile) throws WxErrorException;
/** /**
* 图片智能裁剪接口 * 图片智能裁剪接口
@ -117,5 +117,5 @@ public interface WxMpImgProcService {
* @return WxMpImgProcAiCropResult * @return WxMpImgProcAiCropResult
* @throws WxErrorException . * @throws WxErrorException .
*/ */
WxMpImgProcAiCropResult aiCrop(File imgFile, String ratios) throws WxErrorException; WxImgProcAiCropResult aiCrop(File imgFile, String ratios) throws WxErrorException;
} }

View File

@ -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 com.google.gson.annotations.SerializedName;
import lombok.Data; 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.io.Serializable;
import java.util.List; import java.util.List;
@ -11,50 +11,59 @@ import java.util.List;
* @author Theo Nie * @author Theo Nie
*/ */
@Data @Data
public class WxMpImgProcAiCropResult implements Serializable { public class WxImgProcAiCropResult implements Serializable {
private static final long serialVersionUID = -6470673963772979463L; private static final long serialVersionUID = -6470673963772979463L;
@SerializedName("img_size") @SerializedName("img_size")
private ImgSize imgSize; private ImgSize imgSize;
@SerializedName("results") @SerializedName("results")
private List<Results> results; private List<Results> results;
@Override @Override
public String toString() { public String toString() {
return WxMpGsonBuilder.create().toJson(this); return WxGsonBuilder.create().toJson(this);
} }
public static WxMpImgProcAiCropResult fromJson(String json) { public static WxImgProcAiCropResult fromJson(String json) {
return WxMpGsonBuilder.create().fromJson(json, WxMpImgProcAiCropResult.class); return WxGsonBuilder.create().fromJson(json, WxImgProcAiCropResult.class);
} }
@Data @Data
public static class ImgSize { public static class ImgSize implements Serializable {
private static final long serialVersionUID = -6470673963772979463L;
@SerializedName("w") @SerializedName("w")
private int w; private int w;
@SerializedName("h") @SerializedName("h")
private int h; private int h;
@Override @Override
public String toString() { public String toString() {
return WxMpGsonBuilder.create().toJson(this); return WxGsonBuilder.create().toJson(this);
} }
} }
@Data @Data
public static class Results { public static class Results implements Serializable {
private static final long serialVersionUID = -6470673963772979463L;
@SerializedName("crop_left") @SerializedName("crop_left")
private int cropLeft; private int cropLeft;
@SerializedName("crop_top") @SerializedName("crop_top")
private int cropTop; private int cropTop;
@SerializedName("crop_right") @SerializedName("crop_right")
private int cropRight; private int cropRight;
@SerializedName("crop_bottom") @SerializedName("crop_bottom")
private int cropBottom; private int cropBottom;
@Override @Override
public String toString() { public String toString() {
return WxMpGsonBuilder.create().toJson(this); return WxGsonBuilder.create().toJson(this);
} }
} }
} }

View File

@ -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 com.google.gson.annotations.SerializedName;
import lombok.Data; 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.io.Serializable;
import java.util.List; import java.util.List;
/** /**
* 二维码/条码识别返回结果 * 二维码/条码识别返回结果
*
* @author Theo Nie * @author Theo Nie
*/ */
@Data @Data
public class WxMpImgProcQrCodeResult implements Serializable { public class WxImgProcQrCodeResult implements Serializable {
private static final long serialVersionUID = -1194154790100866123L; private static final long serialVersionUID = -1194154790100866123L;
@SerializedName("img_size") @SerializedName("img_size")
private ImgSize imgSize; private ImgSize imgSize;
@SerializedName("code_results") @SerializedName("code_results")
private List<CodeResults> codeResults; private List<CodeResults> codeResults;
@Data @Data
public static class ImgSize implements Serializable{ public static class ImgSize implements Serializable {
private static final long serialVersionUID = -8847603245514017839L; private static final long serialVersionUID = -8847603245514017839L;
@SerializedName("w") @SerializedName("w")
private int w; private int w;
@SerializedName("h") @SerializedName("h")
@ -30,64 +33,71 @@ public class WxMpImgProcQrCodeResult implements Serializable {
@Override @Override
public String toString() { public String toString() {
return WxMpGsonBuilder.create().toJson(this); return WxGsonBuilder.create().toJson(this);
} }
} }
@Data @Data
public static class CodeResults implements Serializable{ public static class CodeResults implements Serializable {
private static final long serialVersionUID = -6138135951229076759L; private static final long serialVersionUID = -6138135951229076759L;
@SerializedName("type_name") @SerializedName("type_name")
private String typeName; private String typeName;
@SerializedName("data") @SerializedName("data")
private String data; private String data;
@SerializedName("pos") @SerializedName("pos")
private Pos pos; private Pos pos;
@Override @Override
public String toString() { public String toString() {
return WxMpGsonBuilder.create().toJson(this); return WxGsonBuilder.create().toJson(this);
} }
@Data @Data
public static class Pos implements Serializable{ public static class Pos implements Serializable {
private static final long serialVersionUID = 7754894061212819602L; private static final long serialVersionUID = 7754894061212819602L;
@SerializedName("left_top") @SerializedName("left_top")
private Coordinate leftTop; private Coordinate leftTop;
@SerializedName("right_top") @SerializedName("right_top")
private Coordinate rightTop; private Coordinate rightTop;
@SerializedName("right_bottom") @SerializedName("right_bottom")
private Coordinate rightBottom; private Coordinate rightBottom;
@SerializedName("left_bottom") @SerializedName("left_bottom")
private Coordinate leftBottom; private Coordinate leftBottom;
@Override @Override
public String toString() { public String toString() {
return WxMpGsonBuilder.create().toJson(this); return WxGsonBuilder.create().toJson(this);
} }
@Data @Data
public static class Coordinate implements Serializable{ public static class Coordinate implements Serializable {
private static final long serialVersionUID = 8930443668927359677L; private static final long serialVersionUID = 8930443668927359677L;
@SerializedName("x") @SerializedName("x")
private int x; private int x;
@SerializedName("y") @SerializedName("y")
private int y; private int y;
@Override @Override
public String toString() { public String toString() {
return WxMpGsonBuilder.create().toJson(this); return WxGsonBuilder.create().toJson(this);
} }
} }
} }
} }
public static WxMpImgProcQrCodeResult fromJson(String json) { public static WxImgProcQrCodeResult fromJson(String json) {
return WxMpGsonBuilder.create().fromJson(json, WxMpImgProcQrCodeResult.class); return WxGsonBuilder.create().fromJson(json, WxImgProcQrCodeResult.class);
} }
@Override @Override
public String toString() { public String toString() {
return WxMpGsonBuilder.create().toJson(this); return WxGsonBuilder.create().toJson(this);
} }
} }

View File

@ -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);
}
}

View File

@ -2,6 +2,7 @@ package cn.binarywang.wx.miniapp.api;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.config.WxMaConfig; 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.api.WxOcrService;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.service.WxService; import me.chanjar.weixin.common.service.WxService;
@ -295,4 +296,12 @@ public interface WxMaService extends WxService {
* @return 。 * @return 。
*/ */
WxOcrService getOcrService(); WxOcrService getOcrService();
/**
* 返回图像处理接口的实现类对象,以方便调用其各个接口.
*
* @return WxImgProcService
*/
WxImgProcService getImgProcService();
} }

View File

@ -8,6 +8,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxImgProcService;
import me.chanjar.weixin.common.enums.WxType; import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.api.WxOcrService; import me.chanjar.weixin.common.api.WxOcrService;
import me.chanjar.weixin.common.bean.WxAccessToken; 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 WxMaLiveService liveService = new WxMaLiveServiceImpl(this);
private final WxMaLiveGoodsService liveGoodsService = new WxMaLiveGoodsServiceImpl(this); private final WxMaLiveGoodsService liveGoodsService = new WxMaLiveGoodsServiceImpl(this);
private final WxOcrService ocrService = new WxMaOcrServiceImpl(this); private final WxOcrService ocrService = new WxMaOcrServiceImpl(this);
private final WxImgProcService imgProcService = new WxMaImgProcServiceImpl(this);
private int retrySleepMillis = 1000; private int retrySleepMillis = 1000;
private int maxRetryTimes = 5; private int maxRetryTimes = 5;
@ -408,4 +410,9 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
return this.ocrService; return this.ocrService;
} }
@Override
public WxImgProcService getImgProcService() {
return this.imgProcService;
}
} }

View File

@ -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);
}
}

View File

@ -1,5 +1,6 @@
package me.chanjar.weixin.mp.api; 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.api.WxOcrService;
import me.chanjar.weixin.common.bean.WxJsapiSignature; import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxNetCheckResult; 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 . * @param imgProcService .
*/ */
void setImgProcService(WxMpImgProcService imgProcService); void setImgProcService(WxImgProcService imgProcService);
/** /**
* 返回评论数据管理接口方法的实现类对象,以方便调用其各个接口. * 返回评论数据管理接口方法的实现类对象,以方便调用其各个接口.

View File

@ -7,6 +7,7 @@ import com.google.gson.JsonObject;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxImgProcService;
import me.chanjar.weixin.common.enums.WxType; import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.api.WxOcrService; import me.chanjar.weixin.common.api.WxOcrService;
import me.chanjar.weixin.common.bean.WxAccessToken; 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 WxMpMarketingService marketingService = new WxMpMarketingServiceImpl(this);
private WxMpCommentService commentService = new WxMpCommentServiceImpl(this); private WxMpCommentService commentService = new WxMpCommentServiceImpl(this);
private WxOcrService ocrService = new WxMpOcrServiceImpl(this); private WxOcrService ocrService = new WxMpOcrServiceImpl(this);
private WxMpImgProcService imgProcService = new WxMpImgProcServiceImpl(this); private WxImgProcService imgProcService = new WxMpImgProcServiceImpl(this);
@Getter @Getter
@Setter @Setter
@ -696,12 +697,12 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
} }
@Override @Override
public WxMpImgProcService getImgProcService() { public WxImgProcService getImgProcService() {
return this.imgProcService; return this.imgProcService;
} }
@Override @Override
public void setImgProcService(WxMpImgProcService imgProcService) { public void setImgProcService(WxImgProcService imgProcService) {
this.imgProcService = imgProcService; this.imgProcService = imgProcService;
} }
} }

View File

@ -2,11 +2,11 @@ package me.chanjar.weixin.mp.api.impl;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException; 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.api.WxMpService;
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcAiCropResult; import me.chanjar.weixin.common.bean.imgproc.WxImgProcAiCropResult;
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcQrCodeResult; import me.chanjar.weixin.common.bean.imgproc.WxImgProcQrCodeResult;
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcSuperResolutionResult; import me.chanjar.weixin.common.bean.imgproc.WxImgProcSuperResolutionResult;
import me.chanjar.weixin.common.requestexecuter.ocr.OcrDiscernRequestExecutor; import me.chanjar.weixin.common.requestexecuter.ocr.OcrDiscernRequestExecutor;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -27,29 +27,31 @@ import static me.chanjar.weixin.mp.enums.WxMpApiUrl.ImgProc.SUPER_RESOLUTION;
* @author Theo Nie * @author Theo Nie
*/ */
@RequiredArgsConstructor @RequiredArgsConstructor
public class WxMpImgProcServiceImpl implements WxMpImgProcService { public class WxMpImgProcServiceImpl implements WxImgProcService {
private final WxMpService wxMpService; private final WxMpService wxMpService;
@Override @Override
public WxMpImgProcQrCodeResult qrCode(String imgUrl) throws WxErrorException { public WxImgProcQrCodeResult qrCode(String imgUrl) throws WxErrorException {
try { try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name()); imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
//ignore //ignore
} }
final String result = this.wxMpService.get(String.format(QRCODE.getUrl(this.wxMpService.getWxMpConfigStorage()), imgUrl), null); String result = this.wxMpService.get(String.format(QRCODE.getUrl(this.wxMpService.getWxMpConfigStorage()), imgUrl),
return WxMpImgProcQrCodeResult.fromJson(result); null);
return WxImgProcQrCodeResult.fromJson(result);
} }
@Override @Override
public WxMpImgProcQrCodeResult qrCode(File imgFile) throws WxErrorException { public WxImgProcQrCodeResult qrCode(File imgFile) throws WxErrorException {
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_QRCODE.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile); String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()),
return WxMpImgProcQrCodeResult.fromJson(result); FILE_QRCODE.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile);
return WxImgProcQrCodeResult.fromJson(result);
} }
@Override @Override
public WxMpImgProcSuperResolutionResult superResolution(String imgUrl) throws WxErrorException { public WxImgProcSuperResolutionResult superResolution(String imgUrl) throws WxErrorException {
try { try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name()); imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) { } 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); 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 @Override
public WxMpImgProcSuperResolutionResult superResolution(File imgFile) throws WxErrorException { 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); String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()),
return WxMpImgProcSuperResolutionResult.fromJson(result); FILE_SUPER_RESOLUTION.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile);
return WxImgProcSuperResolutionResult.fromJson(result);
} }
@Override @Override
public WxMpImgProcAiCropResult aiCrop(String imgUrl) throws WxErrorException { public WxImgProcAiCropResult aiCrop(String imgUrl) throws WxErrorException {
return this.aiCrop(imgUrl, ""); return this.aiCrop(imgUrl, "");
} }
@Override @Override
public WxMpImgProcAiCropResult aiCrop(String imgUrl, String ratios) throws WxErrorException { public WxImgProcAiCropResult aiCrop(String imgUrl, String ratios) throws WxErrorException {
try { try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name()); imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
@ -83,22 +86,24 @@ public class WxMpImgProcServiceImpl implements WxMpImgProcService {
ratios = ""; ratios = "";
} }
final String result = this.wxMpService.get(String.format(AI_CROP.getUrl(this.wxMpService.getWxMpConfigStorage()), imgUrl, ratios), null); final String result = this.wxMpService.get(String.format(AI_CROP.getUrl(this.wxMpService.getWxMpConfigStorage()),
return WxMpImgProcAiCropResult.fromJson(result); imgUrl, ratios), null);
return WxImgProcAiCropResult.fromJson(result);
} }
@Override @Override
public WxMpImgProcAiCropResult aiCrop(File imgFile) throws WxErrorException { public WxImgProcAiCropResult aiCrop(File imgFile) throws WxErrorException {
return this.aiCrop(imgFile, ""); return this.aiCrop(imgFile, "");
} }
@Override @Override
public WxMpImgProcAiCropResult aiCrop(File imgFile, String ratios) throws WxErrorException { public WxImgProcAiCropResult aiCrop(File imgFile, String ratios) throws WxErrorException {
if (StringUtils.isEmpty(ratios)) { if (StringUtils.isEmpty(ratios)) {
ratios = ""; ratios = "";
} }
String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), String.format(FILE_AI_CROP.getUrl(this.wxMpService.getWxMpConfigStorage()), ratios), imgFile); String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()),
return WxMpImgProcAiCropResult.fromJson(result); String.format(FILE_AI_CROP.getUrl(this.wxMpService.getWxMpConfigStorage()), ratios), imgFile);
return WxImgProcAiCropResult.fromJson(result);
} }
} }

View File

@ -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);
}
}

View File

@ -2,13 +2,13 @@ package me.chanjar.weixin.mp.api.impl;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils; 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.WxMpService;
import me.chanjar.weixin.mp.api.test.ApiTestModule; import me.chanjar.weixin.mp.api.test.ApiTestModule;
import me.chanjar.weixin.mp.api.test.TestConstants; import me.chanjar.weixin.mp.api.test.TestConstants;
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcAiCropResult; import me.chanjar.weixin.common.bean.imgproc.WxImgProcAiCropResult;
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcQrCodeResult; import me.chanjar.weixin.common.bean.imgproc.WxImgProcQrCodeResult;
import me.chanjar.weixin.mp.bean.imgproc.WxMpImgProcSuperResolutionResult; import me.chanjar.weixin.common.bean.imgproc.WxImgProcSuperResolutionResult;
import org.testng.annotations.Guice; import org.testng.annotations.Guice;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -29,7 +29,7 @@ public class WxMpImgProcServiceImplTest {
@Test @Test
public void testQrCode() throws WxErrorException { 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(); assertThat(result).isNotNull();
System.out.println(result); System.out.println(result);
} }
@ -38,14 +38,14 @@ public class WxMpImgProcServiceImplTest {
public void testQrCode2() throws Exception { public void testQrCode2() throws Exception {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg"); InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
File tempFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), TestConstants.FILE_JPG); 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(); assertThat(result).isNotNull();
System.out.println(result); System.out.println(result);
} }
@Test @Test
public void testSuperResolution() throws WxErrorException { 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(); assertThat(result).isNotNull();
System.out.println(result); System.out.println(result);
} }
@ -54,21 +54,21 @@ public class WxMpImgProcServiceImplTest {
public void testSuperResolution2() throws Exception { public void testSuperResolution2() throws Exception {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg"); InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
File tempFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), TestConstants.FILE_JPG); 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(); assertThat(result).isNotNull();
System.out.println(result); System.out.println(result);
} }
@Test @Test
public void testAiCrop() throws WxErrorException { 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(); assertThat(result).isNotNull();
System.out.println(result); System.out.println(result);
} }
@Test @Test
public void testAiCrop2() throws WxErrorException { 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(); assertThat(result).isNotNull();
System.out.println(result); System.out.println(result);
} }
@ -77,7 +77,7 @@ public class WxMpImgProcServiceImplTest {
public void testAiCrop3() throws Exception { public void testAiCrop3() throws Exception {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg"); InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
File tempFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), TestConstants.FILE_JPG); 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(); assertThat(result).isNotNull();
System.out.println(result); System.out.println(result);
} }
@ -86,7 +86,7 @@ public class WxMpImgProcServiceImplTest {
public void testAiCrop4() throws Exception { public void testAiCrop4() throws Exception {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg"); InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
File tempFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), TestConstants.FILE_JPG); 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(); assertThat(result).isNotNull();
System.out.println(result); System.out.println(result);
} }
@ -159,8 +159,8 @@ public class WxMpImgProcServiceImplTest {
" }\n" + " }\n" +
"}"; "}";
when(wxService.get(anyString(), anyString())).thenReturn(returnJson); when(wxService.get(anyString(), anyString())).thenReturn(returnJson);
final WxMpImgProcService wxMpImgProcService = new WxMpImgProcServiceImpl(wxService); final WxImgProcService wxMpImgProcService = new WxMpImgProcServiceImpl(wxService);
final WxMpImgProcQrCodeResult result = wxMpImgProcService.qrCode("abc"); final WxImgProcQrCodeResult result = wxMpImgProcService.qrCode("abc");
assertThat(result).isNotNull(); assertThat(result).isNotNull();
System.out.println(result); System.out.println(result);
} }
@ -173,8 +173,8 @@ public class WxMpImgProcServiceImplTest {
" \"media_id\": \"6WXsIXkG7lXuDLspD9xfm5dsvHzb0EFl0li6ySxi92ap8Vl3zZoD9DpOyNudeJGB\"\n" + " \"media_id\": \"6WXsIXkG7lXuDLspD9xfm5dsvHzb0EFl0li6ySxi92ap8Vl3zZoD9DpOyNudeJGB\"\n" +
"}"; "}";
when(wxService.get(anyString(), anyString())).thenReturn(returnJson); when(wxService.get(anyString(), anyString())).thenReturn(returnJson);
final WxMpImgProcService wxMpImgProcService = new WxMpImgProcServiceImpl(wxService); final WxImgProcService wxMpImgProcService = new WxMpImgProcServiceImpl(wxService);
final WxMpImgProcSuperResolutionResult result = wxMpImgProcService.superResolution("abc"); final WxImgProcSuperResolutionResult result = wxMpImgProcService.superResolution("abc");
assertThat(result).isNotNull(); assertThat(result).isNotNull();
System.out.println(result); System.out.println(result);
} }
@ -204,8 +204,8 @@ public class WxMpImgProcServiceImplTest {
" }\n" + " }\n" +
"}"; "}";
when(wxService.get(anyString(), anyString())).thenReturn(returnJson); when(wxService.get(anyString(), anyString())).thenReturn(returnJson);
final WxMpImgProcService wxMpImgProcService = new WxMpImgProcServiceImpl(wxService); final WxImgProcService wxMpImgProcService = new WxMpImgProcServiceImpl(wxService);
final WxMpImgProcAiCropResult result = wxMpImgProcService.aiCrop("abc"); final WxImgProcAiCropResult result = wxMpImgProcService.aiCrop("abc");
assertThat(result).isNotNull(); assertThat(result).isNotNull();
System.out.println(result); System.out.println(result);
} }