1、注册百度账号 https://login.bce.baidu.com/
2、定位到 产品服务 / 文字识别 - 概览 页面
3、创建一个应用
4、下载相关的SDK包,在工程项目中引用。如果是maven工程,直接在pom.xml文件中添加依赖包
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.6.1</version>
</dependency>
5、编写测试代码
public class BaiduAPITest {
public static final String APP_ID = "你的应用的 AppID";
public static final String API_KEY = "你的应用的 API Key";
public static final String SECRET_KEY = "你的应用的 Secret Key";
public static void main(String[] args) {
String imagePath = "doc\\pic2.jpg";
System.out.println(generalString(imagePath, true));
}
/**
* 识别图片上的文本内容,转成文字字符串返回
* @param imagePath 图片文件的路径
*/
private static String generalString(String imagePath, boolean isNewline){
try{
HashMap<String, String> options = new HashMap<String, String>();
options.put("language_type", "CHN_ENG"); //CHN_ENG:中英文混合, ENG:英文
options.put("detect_direction", "true"); //是否检测图像朝向,默认不检测,即:false
options.put("detect_language", "true"); //是否检测语言,默认不检测。
options.put("probability", "false"); //是否返回识别结果中每一行的置信度
//通用文字识别
JSONObject jsonObject = getAipOcr().basicGeneral(imagePath, options);
String result = mergeString(jsonObject, isNewline);
return result;
}catch(Exception ex){
ex.printStackTrace();
}
return "";
}
/**
* AipOcr最好单例模式使用
*/
private static AipOcr getAipOcr(){
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
return client;
}
/**
* 从返回的JSONObject对象中取出需要的文字内容,组装成一个大文本内容
* @param jsonObject JSONObject对象
* @param isNewline 每个识别结果字符串的后面是否增加换行符
*/
private static String mergeString(JSONObject jsonObject, boolean isNewline){
if(jsonObject == null){
return "";
}
if(jsonObject.has("words_result") && jsonObject.has("words_result_num")){
int wordsResultNum = jsonObject.getInt("words_result_num");
if(wordsResultNum > 0){
StringBuilder sb = new StringBuilder();
JSONArray jsonArray = jsonObject.getJSONArray("words_result");
int len = jsonArray.length();
for(Iterator<Object> it=jsonArray.iterator(); it.hasNext();){
JSONObject obj = (JSONObject)it.next();
if(isNewline){
sb.append(obj.get("words") + "\n");
}else{
sb.append(obj.get("words"));
}
}
return sb.toString();
}
}else{
return jsonObject.toString();
}
return null;
}
}
6、准备一张jpg图片文件
7、执行测试代码,查看效果
//接口返回的内容
{
"words_result": [
{"words": "命令:yum- y install libpng12"},
{"words": "做完这些我们就可以直接使用Tess4进行图片识别了,目前我只试过字母和数字的"},
{"words": "子,有点渣,请诸位大神不要吐槽,是直接传入图片ur地址解析的"},
{"words": "public static String imagetotel(String imgurl)"},
{"words": "Itesseract instance=new Tesseracto);"},
{"words": "URL url=new URL(imgurl);"}
],
"direction": 0,
"words_result_num": 6,
"language": 3,
"log_id": 5228016013525318579
}
//处理后的内容
命令:yum- y install libpng12
做完这些我们就可以直接使用Tess4进行图片识别了,目前我只试过字母和数字的
子,有点渣,请诸位大神不要吐槽,是直接传入图片ur地址解析的
public static String imagetotel(String imgurl)
Itesseract instance=new Tesseracto);
URL url=new URL(imgurl);