|
@@ -0,0 +1,152 @@
|
|
|
|
|
+package com.yingpaipay.business.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.http.Header;
|
|
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
|
|
+import com.yingpaipay.business.domain.bo.AppletScanBo;
|
|
|
|
|
+import com.yingpaipay.business.domain.dto.TextInR;
|
|
|
|
|
+import com.yingpaipay.business.service.ITextInService;
|
|
|
|
|
+import com.yingpaipay.common.file.config.TextInConfig;
|
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
|
+import org.dromara.common.core.exception.BusinessException;
|
|
|
|
|
+import org.dromara.common.json.utils.JsonUtils;
|
|
|
|
|
+import org.jspecify.annotations.NonNull;
|
|
|
|
|
+import org.springframework.http.MediaType;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.*;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
|
+import java.nio.file.Path;
|
|
|
|
|
+import java.util.Base64;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
|
|
+import java.util.zip.ZipInputStream;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
|
+public class TextInServiceImpl implements ITextInService {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String APP_ID = "efb7393aac470f5af564ad91b2a79d2e";
|
|
|
|
|
+ private static final String SECRET_CODE = "027e0c7ceedf5c8b583ec53debbe9420";
|
|
|
|
|
+
|
|
|
|
|
+ private final TextInConfig config;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String imageToPdf(AppletScanBo bo) {
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = HttpRequest.post(config.getImageToPdf())
|
|
|
|
|
+ .header("x-ti-app-id", APP_ID)
|
|
|
|
|
+ .header("x-ti-secret-code", SECRET_CODE)
|
|
|
|
|
+ .body(JsonUtils.toJsonString(bo))
|
|
|
|
|
+ .execute();
|
|
|
|
|
+
|
|
|
|
|
+ TextInR result = getR(response);
|
|
|
|
|
+
|
|
|
|
|
+ return result.getResult().toString();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static @NonNull TextInR getR(HttpResponse response) {
|
|
|
|
|
+ if (response.getStatus() != 200) {
|
|
|
|
|
+ throw new BusinessException("网络异常");
|
|
|
|
|
+ }
|
|
|
|
|
+ String body = response.body();
|
|
|
|
|
+ TextInR result = JsonUtils.parseObject(body, TextInR.class);
|
|
|
|
|
+ if (result == null) {
|
|
|
|
|
+ throw new BusinessException("识别失败");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (result.getCode() != 200) {
|
|
|
|
|
+ throw new BusinessException(result.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String imageToWord(String image) {
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = HttpRequest.post(config.getImageToWord())
|
|
|
|
|
+ .header("x-ti-app-id", APP_ID)
|
|
|
|
|
+ .header("x-ti-secret-code", SECRET_CODE)
|
|
|
|
|
+ .header(Header.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE)
|
|
|
|
|
+ .body(Base64.getDecoder().decode(image))
|
|
|
|
|
+ .execute();
|
|
|
|
|
+
|
|
|
|
|
+ TextInR result = getR(response);
|
|
|
|
|
+
|
|
|
|
|
+ return JsonUtils.parseMap(JsonUtils.toJsonString(result.getResult())).getStr("docx");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String wordToImage(String docx) {
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = HttpRequest.post(config.getWordToImage())
|
|
|
|
|
+ .header("x-ti-app-id", APP_ID)
|
|
|
|
|
+ .header("x-ti-secret-code", SECRET_CODE)
|
|
|
|
|
+ .header(Header.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE)
|
|
|
|
|
+ .body(docx)
|
|
|
|
|
+ .execute();
|
|
|
|
|
+
|
|
|
|
|
+ TextInR result = getR(response);
|
|
|
|
|
+ String zipBase64 = result.getResult().toString();
|
|
|
|
|
+
|
|
|
|
|
+ if (zipBase64.contains(",")) {
|
|
|
|
|
+ zipBase64 = zipBase64.substring(zipBase64.indexOf(",") + 1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ byte[] zipData = Base64.getDecoder().decode(zipBase64);
|
|
|
|
|
+
|
|
|
|
|
+ Path tempDir = Files.createTempDirectory("word_to_image_");
|
|
|
|
|
+
|
|
|
|
|
+ try (ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zipData))) {
|
|
|
|
|
+ ZipEntry entry;
|
|
|
|
|
+ while ((entry = zis.getNextEntry()) != null) {
|
|
|
|
|
+ String name = entry.getName();
|
|
|
|
|
+ if (name.contains("..")) {
|
|
|
|
|
+ continue; // 跳过非法路径
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Path filePath = tempDir.resolve(name).normalize();
|
|
|
|
|
+
|
|
|
|
|
+ Files.createDirectories(filePath.getParent());
|
|
|
|
|
+
|
|
|
|
|
+ if (!entry.isDirectory()) {
|
|
|
|
|
+ try (OutputStream fos = Files.newOutputStream(filePath)) {
|
|
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
|
|
+ int len;
|
|
|
|
|
+ while ((len = zis.read(buffer)) > 0) {
|
|
|
|
|
+ fos.write(buffer, 0, len);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ zis.closeEntry();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
|
+ throw new RuntimeException("Base64 格式无效", e);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ throw new RuntimeException("解压失败", e);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String wordToPdf(String docx) {
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = HttpRequest.post(config.getWordToPdf())
|
|
|
|
|
+ .header("x-ti-app-id", APP_ID)
|
|
|
|
|
+ .header("x-ti-secret-code", SECRET_CODE)
|
|
|
|
|
+ .header(Header.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE)
|
|
|
|
|
+ .body(Base64.getDecoder().decode(docx))
|
|
|
|
|
+ .execute();
|
|
|
|
|
+
|
|
|
|
|
+ TextInR result = getR(response);
|
|
|
|
|
+
|
|
|
|
|
+ return result.getResult().toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|