springboot集成阿里云ocr

本文介绍了如何在SpringBoot项目中集成阿里云OCR服务,包括配置AliyunOcrConfig,编写测试用例,配置spring.factories文件,上传到私服,以及在其他工程中引入并配置阿里云参数。示例展示了在Controller中的简单应用,强调只需注入OCR模块的client并在业务代码中实现识别方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们ocr单独的工程,其他需要的时候直接引入该工程依赖。大致粗略写写,以后用到直接复制。

AliyunOcrProperties.java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * JavaMail 配置属性
 *
 * @author Michelle.Chung
 */
@Data
@ConfigurationProperties(prefix = "ocr.aliyun")
public class AliyunOcrProperties {

    private String accessKeyID;
    private String accessKeySecret;
    private String endpoint;
}

AliyunOcrConfig.java

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.ocr_api20210707.AsyncClient;
import com.xhj.ocr.config.properties.AliyunOcrProperties;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.
### 集成阿里云 OCR API 至 Spring Boot 项目 #### 准备工作 为了在Spring Boot应用中使用阿里云OCR服务,需先完成环境搭建。创建阿里云账户并开通OCR服务,获取`AppCode`用于后续请求认证[^3]。 #### 添加依赖项 在项目的`pom.xml`文件内加入必要的Maven依赖来支持HTTP请求发送: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.0</version> </dependency> <!-- Apache HttpClient --> <dependency> <groupId>org.apache.httpcomponents.client5</groupId> <artifactId>httpclient5</artifactId> <version>5.1</version> </dependency> ``` #### 编写接口调用逻辑 定义一个名为`AliYunOcrService`的服务类处理与阿里云之间的交互操作。此方法接收图像路径参数,并返回解析后的字符串结果。 ```java import com.alibaba.fastjson.JSON; import org.apache.hc.client5.http.classic.methods.HttpPost; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.core5.http.io.entity.EntityUtils; public class AliYunOcrService { private static final String APP_CODE = "your-app-code"; public String recognizeText(String imagePath){ try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost request = new HttpPost("https://dtplus-cn-hangzhou.data.aliyuncs.com/ocr/api/recognition"); // 设置头部信息 request.setHeader("Authorization", "APPCODE " + APP_CODE); request.setHeader("Content-Type", "application/json; charset=UTF-8"); // 构建请求体数据 Map<String, Object> bodyMap = new HashMap<>(); bodyMap.put("image_url", Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(imagePath)))); StringEntity entity = new StringEntity(JSON.toJSONString(bodyMap), Charset.forName("UTF-8")); request.setEntity(entity); try(CloseableHttpResponse response = httpClient.execute(request)){ int statusCode = response.getCode(); if(statusCode == HttpStatus.SC_OK){ return EntityUtils.toString(response.getEntity()); }else{ throw new RuntimeException("Failed with HTTP error code : " + statusCode); } } } catch (Exception e) { throw new RuntimeException(e.getMessage(),e); } } } ``` 上述代码片段展示了如何通过Java程序向阿里云提交待分析的图片文件,并获得其转换为文本形式的结果。注意这里假设输入的是本地存储的照片链接;实际部署时可能需要调整以适应不同的应用场景需求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值