|
@@ -90,7 +90,7 @@ public class AliyunOcrClient extends OcrApiClient {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public String getSignature(String url, String httpMethod) throws Exception {
|
|
|
+ private String getSignature(String url, String httpMethod) throws Exception {
|
|
|
// 解析url中的参数部分
|
|
|
URL u = new URL(url);
|
|
|
String query = u.getQuery();
|
|
@@ -132,7 +132,7 @@ public class AliyunOcrClient extends OcrApiClient {
|
|
|
action = "RecognizeHandwriting";
|
|
|
break;
|
|
|
default:
|
|
|
- action = "";
|
|
|
+ throw new ParameterException("OcrType is invalid");
|
|
|
}
|
|
|
return new HashMap<String, String>() {{
|
|
|
put("Action", action); // 调用的接口名称,此处以 RecognizeGeneral 为例
|
|
@@ -150,7 +150,7 @@ public class AliyunOcrClient extends OcrApiClient {
|
|
|
/**
|
|
|
* 识别本地文件代码示例。以 RecognizeGeneral 接口为例。
|
|
|
*/
|
|
|
- protected String buildUrl(OcrType type) throws Exception {
|
|
|
+ private String buildUrl(OcrType type) throws Exception {
|
|
|
// 获取公共请求参数
|
|
|
Map<String, String> parametersMap = getCommonParameters(type);
|
|
|
// 初始化请求URL
|
|
@@ -171,9 +171,9 @@ public class AliyunOcrClient extends OcrApiClient {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- protected Request buildRequest(OcrType type, byte[] file) throws Exception {
|
|
|
+ protected Request buildRequest(OcrType type, byte[] image) throws Exception {
|
|
|
return new Request.Builder().url(buildUrl(type))
|
|
|
- .post(RequestBody.create(MediaType.parse("application/octet-stream"), file)).build();
|
|
|
+ .post(RequestBody.create(MediaType.parse("application/octet-stream"), image)).build();
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
@@ -183,7 +183,7 @@ public class AliyunOcrClient extends OcrApiClient {
|
|
|
config.setSecret("twrXT7Dp1kG1bV5HZn6vgpoypu9PnZ");
|
|
|
config.setQps(0);
|
|
|
AliyunOcrClient client = new AliyunOcrClient(config);
|
|
|
- System.out.println(client.call(OcrType.GENERAL,
|
|
|
+ System.out.println(client.forImage(OcrType.GENERAL,
|
|
|
ByteArray.fromFile(new File("/Users/luoshi/Downloads/cache/1-1.jpg")).value()));
|
|
|
}
|
|
|
|