本文实例为大家分享了java实现百度云文字识别的接口具体代码,供大家参考,具体内容如下
public class Images {
public static String getResult() {
String otherHost = \"https://aip.baidubce.com/rest/2.0/ocr/v1/general\";
// 本地图片路径
String str=\"你的本地图片路径\"
String filePath = \"str\";
try {
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = 64Util.encode(imgData);
String params = URLEncoder.encode(\"image\", \"UTF-8\") + \"=\" + URLEncoder.encode(imgStr, \"UTF-8\");
/**
* access_token有过期时间, 客户端可自行缓存,过期后重新获取。
*/
String accessToken = getAuth(\"申请的api key\", \"申请的secret key\");
//System.out.println(\"wwwwwwwwwwwwww\");
String result = HttpUtil.post(otherHost, accessToken, params);
//System.out.println(\"sssssssssssssssssss\");
return result;
//System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getAuth(String ak, String sk) {
// 获取token地址
String authHost = \"https://aip.baidubce.com/oauth/2.0/token?\";
String getAccessTokenUrl = authHost
// 1. grant_type为固定参数
+ \"grant_type=client_credentials\"
// 2. 官网获取的 API Key
+ \"&client_id=\" + ak
// 3. 官网获取的 Secret Key
+ \"&client_secret=\" + sk;
try {
URL realUrl = new URL(getAccessTokenUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestMethod(\"GET\");
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.err.println(key + \"--->\" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String result = \"\";
String line;
while ((line = in.readLine()) != null) {
result += line;
}
/**
* 返回结果示例
*/
System.out.println(\"result:\" + result);
JSON json = new JSON (result);
String access_token = json .getString(\"access_token\");
return access_token;
} catch (Exception e) {
System.err.printf(\"获取token失败!\");
e.printStackTrace(System.err);
}
return null;
}
}
测试:
public static void main(String[] args) {
String otherHost = \"https://aip.baidubce.com/rest/2.0/ocr/v1/general\";
// 本地图片路径
String filePath = \"本地图片路径\";
try {
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = 64Util.encode(imgData);
String params = URLEncoder.encode(\"image\", \"UTF-8\") + \"=\" + URLEncoder.encode(imgStr, \"UTF-8\");
*//**
* 线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
*//*
String accessToken = getAuth(\"api key\", \"secret key\");
//System.out.println(\"wwwwwwwwwwwwww\");
String result = HttpUtil.post(otherHost, accessToken, params);
//System.out.println(\"sssssssssssssssssss\");
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
小编再另分享一份网上找到的代码,百度云OCR文字识别功能,作者是:笑释一切。
import java.util.HashMap;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSON ;
import com.baidu.aip.ocr.AipOcr;
/**
* 测试百度云OCR的文字识别功能 <br>
* 打开百度云AI的官网: <br>
* https://console.bce.baidu.com/ai/?_=1517288853048#/ai/ocr/overview/index <br>
*/
public class TestOcr {
//设置APP ID/AK/SK
public static final String APP_ID = \"10736110\";
public static final String API_KEY = \"4nguIG7OdpHZFhdFnz2AbVhx\";
public static final String SECRET_KEY = \"8GnUzj19H0Nie5nOc7HSGSH2VigjU9VL\";
public static void main(String[] args) {
// 初始化一个AipOcr
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
// 传入可选参数调用接口
HashMap<String, String> options = new HashMap<String, String>();
// 是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置
options.put(\"recognize_granularity\", \"big\");
// 识别语言类型,默认为CHN_ENG。可选值包括:
// CHN_ENG:中英文混合;
// ENG:英文;
// POR:葡萄牙语;
// FRE:法语;
// GER:德语;
// ITA:意大利语;
// SPA:西班牙语;
// RUS:俄语;
// JAP:日语;
// KOR:韩语;
options.put(\"language_type\", \"CHN_ENG\");
// 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。
options.put(\"detect_direction\", \"true\");
// 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
options.put(\"detect_language\", \"true\");
// 是否返回文字外接多边形顶点位置,不支持单字位置。默认为false
options.put(\"vertexes_location\", \"false\");
// 是否返回识别结果中每一行的置信度
options.put(\"probability\", \"false\");
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
// 调用接口
String path = \"D:\\\\QQ截图20180130134257.png\";
JSON res = client.accurateGeneral(path, options);
JSONArray myJson = res.getJSONArray(\"words_result\");
Iterator< > iterator = myJson.iterator();
while(iterator.hasNext()){
value = iterator.next();
JSON obj = new JSON (value.toString());
System.out.println(obj.get(\"words\"));
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
上一篇 :
易语言制作语音聊天机器人的代码
下一篇 :
网络安全有多重要?黑客每年带来4500亿美元损失
-
白皮书首发:173位大数据决策者眼中的数据中台是长这样的
2026-05-19栏目: 教程
-
Jmeter的压测使用
2026-05-19栏目: 教程
-
maven常用命令
2026-05-19栏目: 教程
-
Java常用命令之jstat
2026-05-19栏目: 教程
-
阿里云第六代云服务器实例类型、特性及可选区域介绍
2026-05-19栏目: 教程
