第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > 百度云-人工智能-识别图片中的文字

百度云-人工智能-识别图片中的文字

时间:2020-01-02 21:50:05

相关推荐

百度云-人工智能-识别图片中的文字

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 mons.io.FileUtils;import mons.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/SKpublic 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");}// 初始化一个AipOcrAipOcr 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="/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);}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。