Эх сурвалжийг харах

修改BaiduOcr接口的返回内容,将多个段落的识别文字用\n拼接,保留原始段落信息

luoshi 8 сар өмнө
parent
commit
c3b8afa6f6

+ 6 - 6
src/main/java/com/qmth/ops/biz/ai/client/baidu/ocr/BaiduOcrClient.java

@@ -38,7 +38,7 @@ public class BaiduOcrClient extends OcrApiClient {
         if (result != null && result.getWordsList() != null) {
             if (!result.getWordsList().isEmpty()) {
                 return mapper.readValue(data, BaiduOcrResult.class).getWordsList().stream().map(BaiduOcrWords::getWords)
-                        .collect(Collectors.joining(" "));
+                        .collect(Collectors.joining("\n"));
                 //.replaceAll("☰", "").replaceAll("≡", "");
             } else {
                 return StringUtils.EMPTY;
@@ -78,7 +78,7 @@ public class BaiduOcrClient extends OcrApiClient {
         format.setTimeZone(TimeZone.getTimeZone("UTC"));
         return new Request.Builder().url(url).addHeader(BceV1Signer.X_BCE_DATE, format.format(new Date()))
                 .addHeader(HttpHeaders.HOST, HttpUrl.parse(url).host())
-                .addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded").post(buildForm(type, image))
+                .addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded").post(buildBody(type, image))
                 .build();
     }
 
@@ -92,7 +92,7 @@ public class BaiduOcrClient extends OcrApiClient {
         return url.toString();
     }
 
-    private FormBody buildForm(OcrType type, byte[] image) {
+    private FormBody buildBody(OcrType type, byte[] image) {
         FormBody.Builder builder = new FormBody.Builder();
         builder.add("image", CodecUtils.toBase64(image));
         //if (type == OcrType.HANDWRITING) {
@@ -113,12 +113,12 @@ public class BaiduOcrClient extends OcrApiClient {
     public static void main(String[] args) throws Exception {
         OcrApiConfig config = new OcrApiConfig();
         config.setUrl("https://aip.baidubce.com/rest/2.0/ocr/v1/");
-        config.setKey("");
-        config.setSecret("");
+        config.setKey("90a8e5e586924cb7937526b68e441aeb");
+        config.setSecret("a13ced7ae3804b2a84ca4abd005f488f");
         config.setQps(10);
         BaiduOcrClient client = new BaiduOcrClient(config);
         System.out.println(client.forImage(OcrType.HANDWRITING,
-                ByteArray.fromFile(new File("/Users/luoshi/Downloads/test.jpg")).value()));
+                ByteArray.fromFile(new File("/Users/luoshi/Downloads/test2.jpg")).value()));
     }
 
 }