mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
@@ -0,0 +1,121 @@
|
||||
package me.chanjar.weixin.mp.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 java.io.File;
|
||||
|
||||
/**
|
||||
* 多项图像处理能力相关的API.
|
||||
* https://developers.weixin.qq.com/doc/offiaccount/Intelligent_Interface/Img_Proc.html
|
||||
*
|
||||
* @author Theo Nie
|
||||
*/
|
||||
public interface WxMpImgProcService {
|
||||
|
||||
/**
|
||||
* 二维码/条码识别接口
|
||||
* 说明:
|
||||
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
|
||||
* 2.文件大小限制:小于2M
|
||||
* 3.支持条码、二维码、DataMatrix和PDF417的识别。
|
||||
* 4.二维码、DataMatrix会返回位置坐标,条码和PDF417暂不返回位置坐标。
|
||||
*
|
||||
* @param imgUrl 图片url地址
|
||||
* @return WxMpImgProcQrCodeResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcQrCodeResult qrCode(String imgUrl) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 二维码/条码识别接口
|
||||
* 说明:
|
||||
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
|
||||
* 2.文件大小限制:小于2M
|
||||
* 3.支持条码、二维码、DataMatrix和PDF417的识别。
|
||||
* 4.二维码、DataMatrix会返回位置坐标,条码和PDF417暂不返回位置坐标。
|
||||
*
|
||||
* @param imgFile 图片文件对象
|
||||
* @return WxMpImgProcQrCodeResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcQrCodeResult qrCode(File imgFile) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 图片高清化接口
|
||||
* 说明:
|
||||
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
|
||||
* 2.文件大小限制:小于2M
|
||||
* 3.目前支持将图片超分辨率高清化2倍,即生成图片分辨率为原图2倍大小
|
||||
* 返回的media_id有效期为3天,期间可以通过“获取临时素材”接口获取图片二进制
|
||||
*
|
||||
* @param imgUrl 图片url地址
|
||||
* @return WxMpImgProcSuperResolutionResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcSuperResolutionResult superResolution(String imgUrl) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 图片高清化接口
|
||||
* 说明:
|
||||
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
|
||||
* 2.文件大小限制:小于2M
|
||||
* 3.目前支持将图片超分辨率高清化2倍,即生成图片分辨率为原图2倍大小
|
||||
* 返回的media_id有效期为3天,期间可以通过“获取临时素材”接口获取图片二进制
|
||||
*
|
||||
* @param imgFile 图片文件对象
|
||||
* @return WxMpImgProcSuperResolutionResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcSuperResolutionResult superResolution(File imgFile) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 图片智能裁剪接口
|
||||
* 说明:
|
||||
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
|
||||
* 2.文件大小限制:小于2M
|
||||
* 3.该接口默认使用最佳宽高比
|
||||
* @param imgUrl 图片url地址
|
||||
* @return WxMpImgProcAiCropResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcAiCropResult aiCrop(String imgUrl) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 图片智能裁剪接口
|
||||
* 说明:
|
||||
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
|
||||
* 2.文件大小限制:小于2M
|
||||
* @param imgUrl 图片url地址
|
||||
* @param ratios 宽高比,最多支持5个,请以英文逗号分隔
|
||||
* @return WxMpImgProcAiCropResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcAiCropResult aiCrop(String imgUrl, String ratios) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 图片智能裁剪接口
|
||||
* 说明:
|
||||
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
|
||||
* 2.文件大小限制:小于2M
|
||||
* 3.该接口默认使用最佳宽高比
|
||||
* @param imgFile 图片文件对象
|
||||
* @return WxMpImgProcAiCropResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcAiCropResult aiCrop(File imgFile) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 图片智能裁剪接口
|
||||
* 说明:
|
||||
* 1.图片支持使用img参数实时上传,也支持使用img_url参数传送图片地址,由微信后台下载图片进行识别
|
||||
* 2.文件大小限制:小于2M
|
||||
* @param imgFile 图片文件对象
|
||||
* @param ratios 宽高比,最多支持5个,请以英文逗号分隔
|
||||
* @return WxMpImgProcAiCropResult
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
WxMpImgProcAiCropResult aiCrop(File imgFile, String ratios) throws WxErrorException;
|
||||
}
|
||||
@@ -522,6 +522,12 @@ public interface WxMpService {
|
||||
*/
|
||||
WxMpOcrService getOcrService();
|
||||
|
||||
/**
|
||||
* 返回图像处理接口的实现类对象,以方便调用其各个接口.
|
||||
* @return WxMpImgProcService
|
||||
*/
|
||||
WxMpImgProcService getImgProcService();
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
@@ -648,6 +654,13 @@ public interface WxMpService {
|
||||
*/
|
||||
void setOcrService(WxMpOcrService ocrService);
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @param imgProcService .
|
||||
*/
|
||||
void setImgProcService(WxMpImgProcService imgProcService);
|
||||
|
||||
/**
|
||||
* 返回评论数据管理接口方法的实现类对象,以方便调用其各个接口.
|
||||
*
|
||||
|
||||
@@ -67,6 +67,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
private WxMpMarketingService marketingService = new WxMpMarketingServiceImpl(this);
|
||||
private WxMpCommentService commentService = new WxMpCommentServiceImpl(this);
|
||||
private WxMpOcrService ocrService = new WxMpOcrServiceImpl(this);
|
||||
private WxMpImgProcService imgProcService = new WxMpImgProcServiceImpl(this);
|
||||
|
||||
private Map<String, WxMpConfigStorage> configStorageMap;
|
||||
|
||||
@@ -647,4 +648,14 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
public void setCommentService(WxMpCommentService commentService) {
|
||||
this.commentService = commentService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpImgProcService getImgProcService() {
|
||||
return this.imgProcService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setImgProcService(WxMpImgProcService imgProcService) {
|
||||
this.imgProcService = imgProcService;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
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.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.mp.util.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;
|
||||
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.ImgProc.AI_CROP;
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.ImgProc.FILE_AI_CROP;
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.ImgProc.FILE_QRCODE;
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.ImgProc.FILE_SUPER_RESOLUTION;
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.ImgProc.QRCODE;
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.ImgProc.SUPER_RESOLUTION;
|
||||
|
||||
/**
|
||||
* 图像处理接口实现.
|
||||
* @author Theo Nie
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class WxMpImgProcServiceImpl implements WxMpImgProcService {
|
||||
private final WxMpService wxMpService;
|
||||
|
||||
@Override
|
||||
public WxMpImgProcQrCodeResult 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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpImgProcSuperResolutionResult superResolution(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(SUPER_RESOLUTION.getUrl(this.wxMpService.getWxMpConfigStorage()), imgUrl), null);
|
||||
return WxMpImgProcSuperResolutionResult.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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpImgProcAiCropResult aiCrop(String imgUrl) throws WxErrorException {
|
||||
return this.aiCrop(imgUrl, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpImgProcAiCropResult 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.wxMpService.get(String.format(AI_CROP.getUrl(this.wxMpService.getWxMpConfigStorage()), imgUrl, ratios), null);
|
||||
return WxMpImgProcAiCropResult.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpImgProcAiCropResult aiCrop(File imgFile) throws WxErrorException {
|
||||
return this.aiCrop(imgFile, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpImgProcAiCropResult 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user