阿里千问ocr识别工具类
This commit is contained in:
@@ -19,6 +19,10 @@
|
|||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>dashscope-sdk-java</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.czg</groupId>
|
<groupId>com.czg</groupId>
|
||||||
<artifactId>cash-common-log</artifactId>
|
<artifactId>cash-common-log</artifactId>
|
||||||
|
|||||||
@@ -13,6 +13,14 @@
|
|||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun</groupId>
|
||||||
|
<artifactId>bailian20231229</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>dashscope-sdk-java</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alipay.sdk</groupId>
|
<groupId>com.alipay.sdk</groupId>
|
||||||
<artifactId>alipay-sdk-java</artifactId>
|
<artifactId>alipay-sdk-java</artifactId>
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
/**
|
||||||
|
* <b>description</b> :
|
||||||
|
* <p>使用凭据初始化账号Client</p>
|
||||||
|
* @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<String, String> 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<String, String> headers = (Map<String, String>) applyInfo.getParam().getHeaders();
|
||||||
|
|
||||||
|
HttpRequest request = HttpRequest.put(applyInfo.getParam().getUrl());
|
||||||
|
|
||||||
|
// 必须一模一样地设置所有 headers(否则签名失效)
|
||||||
|
for (Map.Entry<String, String> 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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -46,6 +46,17 @@
|
|||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun</groupId>
|
||||||
|
<artifactId>bailian20231229</artifactId>
|
||||||
|
<version>2.6.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>dashscope-sdk-java</artifactId>
|
||||||
|
<version>2.22.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.javen205</groupId>
|
<groupId>com.github.javen205</groupId>
|
||||||
<artifactId>IJPay-All</artifactId>
|
<artifactId>IJPay-All</artifactId>
|
||||||
|
|||||||
@@ -18,6 +18,8 @@
|
|||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.belerweb</groupId>
|
<groupId>com.belerweb</groupId>
|
||||||
<artifactId >pinyin4j</artifactId>
|
<artifactId >pinyin4j</artifactId>
|
||||||
|
|||||||
Reference in New Issue
Block a user