第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > java 获取百度云盘图片_java 利用百度云识别图片文字

java 获取百度云盘图片_java 利用百度云识别图片文字

时间:2022-10-24 06:24:24

相关推荐

java 获取百度云盘图片_java 利用百度云识别图片文字

一、百度云使用步骤

1.进入https://console./#/index/overview 登录百度云账号。百度所有账号都是通用的,可以直接利用百度的其他账号(百度网盘、百度贴吧等)进行登录。没有可以进行注册。进入到如下界面,登录成功后,滑动到页面最下边。

首页.png

2.选择文字识别

文字识别.png

选中文字识别进入到如下页面。

文字识别详情.png

3.在应用列表下,创建应用

应用.png

这里需要注意,如果你的是android或者Ios应用则需要把包名和应用名与你填写的保持一致。

4.创建成功后就可以看到我们所需要的API Key和Secret Key 。

成功.png

百度云识别图片文字所需要的API Key 和Secrect Key已经获取成功,下面开始进行图片文字识别的实现。

二、程序设计

1、下载所需要的jar包,在百度云官网下载/sdk#ocr。选择文字识别中的 java SDK 进行下载。解压后获取到所需要的jar包。/docs#/OCR-Java-SDK/top 可参考该页面的使用步骤。

api

2、新建一个java工程。

初始化一个AipOcr

* 初始化AipOcr

* @param appId

* @param apiKey

* @param secretKey

* @return

*/

public static AipOcr getAipOCR(String appId,String apiKey,String secretKey){

AipOcr api = null;

if(appId == null || appId.trim().length() == 0

|| apiKey == null || apiKey.trim().length() == 0

|| secretKey == null || secretKey.trim().length() == 0)

{

logger.info("appID or apiKey or secretKey is error! ");

return api;

}

api = new AipOcr(appId, apiKey, secretKey);

return api;

}

通用文字识别的方法(识别本地图片上的文字)

/**

* 识别本地图片文字

* @param imgUrl

* @return

*/

public static String getOCRText(String imgUrl){

String ocrText = null;

String appId = "你的 App ID";

String apiKey = "你的 Api Key" ;

String secretKey = "你的 Secret Key";

AipOcr api = getAipOCR(appId, apiKey, secretKey);

if(api == null ){

logger.warn("api is null,unable to continue!");

return ocrText;

}

HashMap options = new HashMap();

options.put("detect_direction", "true");

options.put("probability", "true");

options.put("recognize_granularity", "big");

options.put("vertexes_location", "true");

JSONObject res = api.basicAccurateGeneral(imgUrl, options);

if(res == null || res.length() == 0){

ocrText = "There is no text in this picture.";

return ocrText;

}

JSONArray dataArray = res.getJSONArray("words_result");

System.out.println(dataArray);

JSONObject jsonData;

if(dataArray == null || dataArray.length() == 0){

ocrText = "There is no text in this picture.";

return ocrText;

}

for (int i = 0; i < dataArray.length(); i++) {

jsonData = dataArray.getJSONObject(i);

if(jsonData != null){

ocrText += jsonData.getString("words");

}else {

ocrText += "There is no text in this picture.";

}

}

return ocrText;

}

测试图片

test1.png

测试结果

result1.png

/**

* 识别网络图片上的文字

* @param imgUrl

* @return

*/

public static String getOCR(String imgUrl){

String ocrWord="";

if(!imgUrl.startsWith("http"))

return ocrWord;

String appId = "你的 App ID";

String apiKey = "你的 Api Key" ;

String secretKey = "你的 Secret Key";

AipOcr api = getAipOCR(appId, apiKey, secretKey);

if(api == null ){

logger.warn("api is null,unable to continue!");

return ocrWord;

}

HashMap options = new HashMap();

options.put("detect_direction", "true");

options.put("detect_language", "true");

JSONObject res = api.webImageUrl(imgUrl, options );

JSONArray dataArray = res.getJSONArray("words_result");

JSONObject jsonData;

for (int i = 0; i < dataArray.length(); i++) {

jsonData = dataArray.getJSONObject(i);

if(jsonData != null)

ocrWord += jsonData.getString("words");

}

return ocrWord;

}

result2.png

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