1
0
mirror of synced 2026-10-07 23:39:43 +08:00

🆕 #4028 【公众号】实现 OCR 菜单识别的接口

This commit is contained in:
Copilot
2026-07-14 14:29:19 +08:00
committed by GitHub
parent 83d7744425
commit 124ff523f3
7 changed files with 176 additions and 1 deletions

View File

@@ -143,4 +143,24 @@ public class WxMpOcrServiceImpl implements WxOcrService {
FILE_COMM.getUrl(this.mainService.getWxMpConfigStorage()), imgFile);
return WxOcrCommResult.fromJson(result);
}
@Override
public WxOcrMenuResult menu(String imgUrl) throws WxErrorException {
try {
imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
// ignore cannot happen
}
final String result = this.mainService.post(String.format(MENU.getUrl(this.mainService.getWxMpConfigStorage()),
imgUrl), (String) null);
return WxOcrMenuResult.fromJson(result);
}
@Override
public WxOcrMenuResult menu(File imgFile) throws WxErrorException {
String result = this.mainService.execute(OcrDiscernRequestExecutor.create(this.mainService.getRequestHttp()),
FILE_MENU.getUrl(this.mainService.getWxMpConfigStorage()), imgFile);
return WxOcrMenuResult.fromJson(result);
}
}

View File

@@ -533,7 +533,17 @@ public interface WxMpApiUrl {
/**
* 通用印刷体OCR识别(文件)
*/
FILE_COMM(API_DEFAULT_HOST_URL, "/cv/ocr/comm");
FILE_COMM(API_DEFAULT_HOST_URL, "/cv/ocr/comm"),
/**
* 菜单OCR识别
*/
MENU(API_DEFAULT_HOST_URL, "/cv/ocr/menu?img_url=%s"),
/**
* 菜单OCR识别(文件)
*/
FILE_MENU(API_DEFAULT_HOST_URL, "/cv/ocr/menu");
private final String prefix;
private final String path;

View File

@@ -11,6 +11,7 @@ import me.chanjar.weixin.common.bean.ocr.WxOcrCommResult;
import me.chanjar.weixin.common.bean.ocr.WxOcrDrivingLicenseResult;
import me.chanjar.weixin.common.bean.ocr.WxOcrDrivingResult;
import me.chanjar.weixin.common.bean.ocr.WxOcrIdCardResult;
import me.chanjar.weixin.common.bean.ocr.WxOcrMenuResult;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
@@ -137,6 +138,22 @@ public class WxMpOcrServiceImplTest {
System.out.println(result);
}
@Test
public void testMenu() throws WxErrorException {
final WxOcrMenuResult result = this.service.getOcrService().menu("https://res.wx.qq.com/op_res/apCy0YbnEdjYsa_cjW6x3FlpCc20uQ-2BYE7aXnFsrB-ALHZNgdKXhzIUcrRnDoL");
assertThat(result).isNotNull();
System.out.println(result);
}
@Test
public void testMenu2() throws Exception {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
File tempFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), TestConstants.FILE_JPG);
final WxOcrMenuResult result = this.service.getOcrService().menu(tempFile);
assertThat(result).isNotNull();
System.out.println(result);
}
private InputStream getImageStream(String url) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
@@ -390,5 +407,46 @@ public class WxMpOcrServiceImplTest {
assertThat(result).isNotNull();
System.out.println(result);
}
@Test
public void testMenu() throws Exception {
String returnJson = "{\n" +
" \"errcode\": 0, \n" +
" \"errmsg\": \"ok\", \n" +
" \"items\": [\n" +
" {\n" +
" \"text\": \"红烧肉\", \n" +
" \"pos\": {\n" +
" \"left_top\": {\n" +
" \"x\": 575, \n" +
" \"y\": 519\n" +
" }, \n" +
" \"right_top\": {\n" +
" \"x\": 744, \n" +
" \"y\": 519\n" +
" }, \n" +
" \"right_bottom\": {\n" +
" \"x\": 744, \n" +
" \"y\": 532\n" +
" }, \n" +
" \"left_bottom\": {\n" +
" \"x\": 573, \n" +
" \"y\": 532\n" +
" }\n" +
" }\n" +
" }\n" +
" ], \n" +
" \"img_size\": {\n" +
" \"w\": 1280, \n" +
" \"h\": 720\n" +
" }\n" +
"}";
when(wxService.post(anyString(), anyString())).thenReturn(returnJson);
final WxMpOcrServiceImpl wxMpOcrService = new WxMpOcrServiceImpl(wxService);
final WxOcrMenuResult result = wxMpOcrService.menu("abc");
assertThat(result).isNotNull();
System.out.println(result);
}
}
}