天天看點

調用百度文字識别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, “識别失敗”);

}

}

繼續閱讀