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

一、百度云使用步骤

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

594734f64c55?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

首页.png

2.选择文字识别

594734f64c55?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

文字识别.png

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

594734f64c55?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

文字识别详情.png

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

594734f64c55?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

应用.png

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

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

594734f64c55?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

成功.png

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

二、程序设计

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

594734f64c55?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

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;

}

测试图片

594734f64c55?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

test1.png

测试结果

594734f64c55?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

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;

}

594734f64c55?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

result2.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值