mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-29 09:38:19 +08:00
🆕 #1685 小程序增加图像处理相关接口
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user