赞
踩
https://console.bce.baidu.com/ai/?_=1579651938462&fromai=1#/ai/ocr/overview/index
根据图片的网络地址,转换其为文本内容。
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.11.3</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier><!-- 指定jdk版本 -->
</dependency>
import java.util.HashMap; import java.util.Iterator; import com.baidu.aip.ocr.AipOcr; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class BaiduAiAPI { //设置APPID/AK/SK public static final String APP_ID = "您的*"; public static final String API_KEY = "您的*"; public static final String SECRET_KEY = "您的*"; public static String imageToWords(String url) { // 初始化一个AipOcr AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY); // 可选:设置网络连接参数 client.setConnectionTimeoutInMillis(2000); client.setSocketTimeoutInMillis(60000); // 调用接口 org.json.JSONObject res = client.generalUrl(url, new HashMap<String, String>()); String json=res.toString(); JSONObject jsonObject = JSONObject.fromObject(json); JSONArray jsonArray = jsonObject.getJSONArray("words_result"); Iterator<JSONObject> it = jsonArray.iterator(); StringBuilder sb=new StringBuilder(); while(it.hasNext()) { JSONObject next = it.next(); sb.append(next.getString("words")); } return sb.toString(); } }
希望以上笔记能够帮助到你!
不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈的积累!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。