调用百度文字识别api,识别图片上的文字

package com.dianjue.sso.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dianjue.commonutils.responseinfo.ResponseData;
import com.dianjue.sso.dto.BaiDuAiDto;
import com.dianjue.sso.dto.BaiduResponseVo;
import com.dianjue.sso.dto.Words;
import com.dianjue.sso.entity.BaiduKeyYml;
import com.dianjue.sso.entity.Company;
import com.dianjue.sso.service.BaiDuAiService;
import com.dianjue.sso.utils.BaseImg64;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class BaiDuAiServiceImpl implements BaiDuAiService {

@Autowired
private BaiduKeyYml baiduKeyYml;

@Override
public ResponseData<List> getResultFormBaidu(BaiDuAiDto baiDuAiDto) {
String getAccessTokenUrl = "https://aip.baidubce.com/oauth/2.0/token?"
+ “grant_type=client_credentials” + “&client_id=” + baiduKeyYml.getAppKey()
+ “&client_secret=” + baiduKeyYml.getSecretKey();
try {
URL realUrl = new URL(getAccessTokenUrl);
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestMethod(“GET”);
connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String result = “”;
String line;
while ((line = in.readLine()) != null) {
result += line;
}
JSONObject jsonObject = JSONObject.parseObject(result.toString());
String token = jsonObject.getString(“access_token”);
String baiduHost = String
.format(“https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=%s”, token);
URI requestUrl = new URI(baiduHost);
String param =null;
if(null !=baiDuAiDto.getPathFile()){
File file = new File(baiDuAiDto.getPathFile());
if (!file.exists()) {
return new ResponseData<>(202,“图片不存在”);
}
String image = BaseImg64.getImageStrFromPath(baiDuAiDto.getPathFile());
param = “image=” + image;
}else{
param = “url=” + baiDuAiDto.getImageUrl();
}
StringEntity entity = new StringEntity(param);
HttpPost post = new HttpPost();
post.setURI(requestUrl);
post.setHeader(“Content-Type”, “application/x-www-form-urlencoded”);
post.setEntity(entity);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
String str = EntityUtils.toString(response.getEntity());
JSONObject jsonObject1 = JSONObject.parseObject(str);
log.info(“AAAA:{}”,jsonObject1);
BaiduResponseVo baiduResponseVo = JSONObject
.toJavaObject(jsonObject1, BaiduResponseVo.class);
log.info(“返回的结果是:{}”, baiduResponseVo);
if(null !=baiduResponseVo.getWords_result() && baiduResponseVo.getWords_result().size()>0){
return new ResponseData<>(0, “成功了”, baiduResponseVo.getWords_result());
}else {
return new ResponseData<>(202, “识别失败”);
}
} else {
return new ResponseData<>(202, “识别失败”);
}
} catch (Exception e) {
return new ResponseData<>(202, “识别失败”);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值