From d11866ad816c3a528342524c3f15976f55c177f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9D=BE?= <8605635+zhang3064194730@user.noreply.gitee.com> Date: Mon, 24 Nov 2025 17:29:06 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=BF=E9=87=8C=E5=8D=83=E9=97=AEocr?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cash-api/account-server/pom.xml | 4 + cash-common/cash-common-tools/pom.xml | 8 ++ .../main/java/com/czg/utils/AliOcrUtil.java | 133 ++++++++++++++++++ cash-dependencies/pom.xml | 11 ++ cash-service/account-service/pom.xml | 2 + 5 files changed, 158 insertions(+) create mode 100644 cash-common/cash-common-tools/src/main/java/com/czg/utils/AliOcrUtil.java diff --git a/cash-api/account-server/pom.xml b/cash-api/account-server/pom.xml index 007aa79f0..523fdd2db 100644 --- a/cash-api/account-server/pom.xml +++ b/cash-api/account-server/pom.xml @@ -19,6 +19,10 @@ + + com.alibaba + dashscope-sdk-java + com.czg cash-common-log diff --git a/cash-common/cash-common-tools/pom.xml b/cash-common/cash-common-tools/pom.xml index 52f9b09c0..2faba48ed 100644 --- a/cash-common/cash-common-tools/pom.xml +++ b/cash-common/cash-common-tools/pom.xml @@ -13,6 +13,14 @@ jar + + com.aliyun + bailian20231229 + + + com.alibaba + dashscope-sdk-java + com.alipay.sdk alipay-sdk-java diff --git a/cash-common/cash-common-tools/src/main/java/com/czg/utils/AliOcrUtil.java b/cash-common/cash-common-tools/src/main/java/com/czg/utils/AliOcrUtil.java new file mode 100644 index 000000000..7e545a6a4 --- /dev/null +++ b/cash-common/cash-common-tools/src/main/java/com/czg/utils/AliOcrUtil.java @@ -0,0 +1,133 @@ +package com.czg.utils; + +import cn.hutool.core.io.FileUtil; +import cn.hutool.crypto.digest.DigestUtil; +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import com.alibaba.dashscope.app.Application; +import com.alibaba.dashscope.app.ApplicationParam; +import com.alibaba.dashscope.app.ApplicationResult; +import com.alibaba.dashscope.app.RagOptions; +import com.alibaba.dashscope.exception.InputRequiredException; +import com.alibaba.dashscope.exception.NoApiKeyException; +import com.aliyun.bailian20231229.Client; +import com.aliyun.bailian20231229.models.ApplyFileUploadLeaseResponse; +import com.aliyun.bailian20231229.models.ApplyFileUploadLeaseResponseBody; +import com.czg.exception.CzgException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.io.File; +import java.util.List; +import java.util.Map; + +/** + * 支付宝通用SDK工具类 + * + * @author tankaikai + * @since 2024-09-23 16:15 + */ + +@Slf4j +@Component +public class AliOcrUtil { + /** + * description : + *

使用凭据初始化账号Client

+ * @return Client + * + * @throws Exception + */ + public static com.aliyun.bailian20231229.Client createClient() { + + com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config() + // 配置 AccessKey ID,请确保代码运行环境设置了环境变量。 + .setAccessKeyId("LTAI5t8dEAHVd3HqaaS48fK9") + // 配置 AccessKey Secret,请确保代码运行环境设置了环境变量。 + .setAccessKeySecret("fzfNGiuvpC21yuT3OgbJ7MJJT0dPX0") + .setEndpoint("bailian.cn-beijing.aliyuncs.com"); + + try { + return new Client(config); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + + public static ApplyFileUploadLeaseResponseBody.ApplyFileUploadLeaseResponseBodyData applyFileUpload(File file) { + String md5 = DigestUtil.md5Hex(file); + System.out.println(md5); + com.aliyun.bailian20231229.Client client = createClient(); + com.aliyun.bailian20231229.models.ApplyFileUploadLeaseRequest applyFileUploadLeaseRequest = new com.aliyun.bailian20231229.models.ApplyFileUploadLeaseRequest() + .setFileName(file.getName()) + .setMd5(md5) + .setSizeInBytes("100000") + .setCategoryType("SESSION_FILE") + .setUseInternalEndpoint(false); + com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); + java.util.Map headers = new java.util.HashMap<>(); + try { + // 复制代码运行请自行打印 API 的返回值 + ApplyFileUploadLeaseResponse aDefault = client.applyFileUploadLeaseWithOptions("default", "llm-9zg04s7wlbvi32tq", applyFileUploadLeaseRequest, headers, runtime); + if (!"Success".equals(aDefault.getBody().getCode())) { + throw new CzgException(aDefault.getBody().getMessage()); + } + return aDefault.getBody().getData(); + } catch (Exception error) { + throw new RuntimeException(error); + } + } + + public static String uploadFile(File file) { + + ApplyFileUploadLeaseResponseBody.ApplyFileUploadLeaseResponseBodyData applyInfo = applyFileUpload(file); + + // 获取预签名要求的所有头 + Map headers = (Map) applyInfo.getParam().getHeaders(); + + HttpRequest request = HttpRequest.put(applyInfo.getParam().getUrl()); + + // 必须一模一样地设置所有 headers(否则签名失效) + for (Map.Entry entry : headers.entrySet()) { + request.header(entry.getKey(), entry.getValue()); + } + + // 设置文件内容 + request.body(FileUtil.readBytes(file)); + + HttpResponse resp = request.execute(); + log.info(resp.body()); + return applyInfo.getFileUploadLeaseId(); + } + + + public static void appCall(File file) { + String id = uploadFile(file); + ApplicationParam param = ApplicationParam.builder() + .apiKey("sk-2343af4413834ad1ab43b036e3a903de") + .appId("3493340ef5e146c487364395fbca7bf3") + .prompt("开始处理") + .ragOptions(RagOptions.builder() + .sessionFileIds(List.of(id)) + .build()) + .build(); + + Application application = new Application(); + ApplicationResult result = null; + try { + result = application.call(param); + } catch (Exception e) { + throw new RuntimeException(e); + } + System.out.printf("%s\n", + result.getOutput().getText()); + } + + + static void main() throws Exception { + appCall(new File("C:\\Users\\Administrator\\Downloads\\微信图片_20251121101326_2015_354.jpg")); + } + +} diff --git a/cash-dependencies/pom.xml b/cash-dependencies/pom.xml index a844e7a5d..b5163a004 100644 --- a/cash-dependencies/pom.xml +++ b/cash-dependencies/pom.xml @@ -46,6 +46,17 @@ + + com.aliyun + bailian20231229 + 2.6.1 + + + com.alibaba + dashscope-sdk-java + 2.22.1 + + com.github.javen205 IJPay-All diff --git a/cash-service/account-service/pom.xml b/cash-service/account-service/pom.xml index ca3fe5b45..b42ba1897 100644 --- a/cash-service/account-service/pom.xml +++ b/cash-service/account-service/pom.xml @@ -18,6 +18,8 @@ + + com.belerweb pinyin4j