|
@@ -1,6 +1,7 @@
|
|
|
-package com.qmth.ops.biz.ai.client.aliyun;
|
|
|
+package com.qmth.ops.biz.ai.client.aliyun.ocr;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.qmth.boot.core.ai.model.ocr.OcrType;
|
|
|
import com.qmth.boot.core.exception.ParameterException;
|
|
|
import com.qmth.boot.core.exception.ReentrantException;
|
|
|
import com.qmth.boot.core.exception.StatusException;
|
|
@@ -9,6 +10,10 @@ import com.qmth.boot.tools.models.ByteArray;
|
|
|
import com.qmth.boot.tools.uuid.FastUUID;
|
|
|
import com.qmth.ops.biz.ai.client.OcrApiClient;
|
|
|
import com.qmth.ops.biz.ai.client.OcrApiConfig;
|
|
|
+import com.qmth.ops.biz.ai.client.aliyun.AliyunError;
|
|
|
+import okhttp3.MediaType;
|
|
|
+import okhttp3.Request;
|
|
|
+import okhttp3.RequestBody;
|
|
|
|
|
|
import javax.crypto.Mac;
|
|
|
import javax.crypto.SecretKey;
|
|
@@ -26,11 +31,8 @@ import java.util.*;
|
|
|
|
|
|
public class AliyunOcrClient extends OcrApiClient {
|
|
|
|
|
|
- private String action;
|
|
|
-
|
|
|
- public AliyunOcrClient(OcrApiConfig config, String action) {
|
|
|
+ public AliyunOcrClient(OcrApiConfig config) {
|
|
|
super(config);
|
|
|
- this.action = action;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -44,7 +46,7 @@ public class AliyunOcrClient extends OcrApiClient {
|
|
|
if (data != null) {
|
|
|
try {
|
|
|
error = mapper.readValue(data, AliyunError.class);
|
|
|
- } catch (Exception e) {
|
|
|
+ } catch (Exception ignore) {
|
|
|
}
|
|
|
}
|
|
|
switch (statusCode) {
|
|
@@ -120,7 +122,18 @@ public class AliyunOcrClient extends OcrApiClient {
|
|
|
*
|
|
|
* @return 公共请求参数组成的字典
|
|
|
*/
|
|
|
- private Map<String, String> getCommonParameters(String action) {
|
|
|
+ private Map<String, String> getCommonParameters(OcrType type) {
|
|
|
+ String action;
|
|
|
+ switch (type) {
|
|
|
+ case GENERAL:
|
|
|
+ action = "RecognizeGeneral";
|
|
|
+ break;
|
|
|
+ case HANDWRITING:
|
|
|
+ action = "RecognizeHandwriting";
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ action = "";
|
|
|
+ }
|
|
|
return new HashMap<String, String>() {{
|
|
|
put("Action", action); // 调用的接口名称,此处以 RecognizeGeneral 为例
|
|
|
put("Version", "2021-07-07"); // API版本。OCR的固定值:2021-07-07
|
|
@@ -137,10 +150,9 @@ public class AliyunOcrClient extends OcrApiClient {
|
|
|
/**
|
|
|
* 识别本地文件代码示例。以 RecognizeGeneral 接口为例。
|
|
|
*/
|
|
|
- @Override
|
|
|
- protected String buildUrl() throws Exception {
|
|
|
+ protected String buildUrl(OcrType type) throws Exception {
|
|
|
// 获取公共请求参数
|
|
|
- Map<String, String> parametersMap = getCommonParameters(action);
|
|
|
+ Map<String, String> parametersMap = getCommonParameters(type);
|
|
|
// 初始化请求URL
|
|
|
StringBuilder urlBuilder = new StringBuilder(getConfig().getUrl());
|
|
|
urlBuilder.append("?");
|
|
@@ -158,14 +170,21 @@ public class AliyunOcrClient extends OcrApiClient {
|
|
|
return url;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ protected Request buildRequest(OcrType type, byte[] file) throws Exception {
|
|
|
+ return new Request.Builder().url(buildUrl(type))
|
|
|
+ .post(RequestBody.create(MediaType.parse("application/octet-stream"), file)).build();
|
|
|
+ }
|
|
|
+
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
OcrApiConfig config = new OcrApiConfig();
|
|
|
config.setUrl("https://ocr-api.cn-hangzhou.aliyuncs.com/");
|
|
|
config.setKey("LTAI5t6D5p62tDjYgwSz2mTR");
|
|
|
config.setSecret("twrXT7Dp1kG1bV5HZn6vgpoypu9PnZ");
|
|
|
config.setQps(0);
|
|
|
- AliyunOcrClient client = new AliyunOcrClient(config, "RecognizeHandwriting");
|
|
|
- System.out.println(client.call(ByteArray.fromFile(new File("/Users/luoshi/Downloads/cache/1-1.jpg")).value()));
|
|
|
+ AliyunOcrClient client = new AliyunOcrClient(config);
|
|
|
+ System.out.println(client.call(OcrType.GENERAL,
|
|
|
+ ByteArray.fromFile(new File("/Users/luoshi/Downloads/cache/1-1.jpg")).value()));
|
|
|
}
|
|
|
|
|
|
}
|