阿里云OCR文字识别API接口使用实例–Java篇
阿里云官方接口:(官方接口+购买地址)
- 0.01元500次对入门的小白很友好(个人感觉)值得一试
官方参数
{
"image": #"图片二进制数据的base64编码/图片url",
"configure":
{
"min_size" : 16, #图片中文字的最小高度,单位像素(此参数目前已经废弃)
"output_prob" : true, #是否输出文字框的概率
"output_keypoints": false, #是否输出文字框角点
"skip_detection": false, #是否跳过文字检测步骤直接进行文字识别
"without_predicting_direction": false, #是否关闭文字行方向预测
"language": "sx" #当skip_detection为true时,该字段才生效,做单行手写识别。
}
}
方法类及工具类
/**
* 传入图片路径,将图片转为二进制数据传出为byte[]
*/
public static byte [] imgToByte(String file){
File img1 = new File(file);
if (!img1.exists())return new byte[0];
try {
InputStream in = new FileInputStream(img1);
BufferedInputStream bufferedInputStream = new BufferedInputStream(in);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte [] buf = new byte[(int) img1.length()];
int len;
if ((len=bufferedInputStream.read(buf))>=0){
byteArrayOutputStream.write(buf, 0 , len);
}
System.out.println("Successfully converted to byte data ! ! !");
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 二进制转base64数据
*/
public static String byteToBase64(byte [] by){
byte [] base64_data = Base64.encodeBase64(by);
String S_B64 = new String(base64_data);
System.out.println("Successfully converted to base64 data ! ! !");
return S_B64;
/**
* 官方代码
*/
public static void base64Recognition(String imgBase64){
String host = "https://tysbgpu.market.alicloudapi.com";
String path = "/api/predict/ocr_general";
String method = "POST";
String appcode = "你自己的appcode";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
//根据API的要求,定义相对应的Content-Type
headers.put("Content-Type", "application/json; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
//configure配置
JSONObject configObj = new JSONObject();
configObj.put("side", "face");
String config_str = configObj.toString();
// 拼装请求body的json字符串
JSONObject requestObj = new JSONObject();
requestObj.put("image", imgBase64);
if(configObj.size() > 0) {
requestObj.put("configure", config_str);
}
String bodys = requestObj.toString();
try {
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
System.out.println(response.toString());
//获取response的body
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// 图片路径
String file_path = "*****.jpg";
// 图片转二进制
byte [] byte_data = byteBase64.imgToByte(file_path);
// 二进制转base64
String imgBase64 = byteBase64.byteToBase64(byte_data);
// 向API发送数据,识别
recognition.base64Recognition(imgBase64);
}
以下为官方的工具类(我给出的只是残缺可按提示下载)
懒人链接:
HttpUtils.java文件
pom.xml文件
/**
* 重要提示如下:
* HttpUtils请从
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
* 下载
*
* 相应的依赖请参照
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
*/
public class HttpUtils {
public static HttpResponse doGet(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpGet request = new HttpGet(buildUrl(host, path, querys));
测试
测试图片:
测试结果: