百度雲-人工智慧-識別圖片中的文字

NO IMAGE

1、注入依賴

<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.1.1</version>
</dependency>

2、書寫工具類

import java.io.File;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jboss.logging.Logger;
import org.json.JSONObject;
import com.baidu.aip.ocr.AipOcr;
import com.google.gson.Gson;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
public class ImgToWordUtils {
private static Logger logger=Logger.getLogger(ImgToWordUtils.class);
private static Gson gson=new Gson();
//設定APPID/AK/SK
public static final String APP_ID = "10767511";
public static final String API_KEY = "mxPWgbVWMzylM5Le5RzF0q6i";
public static final String SECRET_KEY = "oNb3T6MRhVZm76GiHNfFKbL1DZV50TxE";
public static List<String> getWords(String path) throws Exception {
if(StringUtils.isEmpty(path)){
throw new RuntimeException("image url is empty");
}
// 初始化一個AipOcr
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
// 可選:設定網路連線引數
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
// 呼叫介面
JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
String response=res.toString(2);
List<String> resList=new ArrayList<>();
Map map=gson.fromJson(response,Map.class);
if(map.size()>0){
Object resultObj=map.get("words_result");
if(resultObj!=null){
List<Map<String,Object>> list=(List<Map<String, Object>>) resultObj;
for(Map<String,Object> mapSingle:list){
Object obj=mapSingle.get("words");
if(obj!=null){
resList.add(word);
}
}
}else{
throw new RuntimeException("image words is empty");
}
}
return resList;
}
public static void main(String[] args) throws Exception {
String path="http://webshot.21cnjy.com/q/6e/24/49cc0606de1e2ec39ec9f605ed3f_6528403ex.png?hash=291383e42457ebc1e9b5485e02e124da&sign=20d5e45edce882f1863dc84edeb3d27b";
List<String> list=executeChange(path);
for(String str:list){
System.err.println("-------");
System.err.println(str);
}
}
public static String saveImage(String path) throws Exception{
if(StringUtils.isEmpty(path)){
throw new RuntimeException("image url is empty");
}
HttpResponse response=HttpRequest.get(path).send();
if(response.statusCode()==200){
File fileDir=new File("/imgtoword");
FileUtils.forceMkdir(fileDir);
String name=UUID.randomUUID().toString().replace("-", "") ".png";
name=fileDir "/" name;
File saveFile=new File(name);
if(!saveFile.exists()){
FileOutputStream out=new FileOutputStream(saveFile);
out.write(response.bodyBytes());
out.close();
}
return name;
}
return null;
}
public static List<String> executeChange(String path) throws Exception{
if(StringUtils.isEmpty(path)){
throw new RuntimeException("image url is empty");
}
logger.info("請求圖片前:" path);
String savePath=saveImage(path);
if(StringUtils.isEmpty(savePath)){
throw new RuntimeException("image url is empty");
}else{
return getWords(savePath);
}
}
}