首先要满足的条件,已注册华为云账户,并订阅华为云上的文字识别接口,以下以订阅的身份证识别接口为例
package OCRDemo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
/**
* @author abang
* @date 2020/7/3 0003 20:59
*/
public class GetOCR {
public static void main(String[] args) throws IOException {
//请求的region
String region = "cn-north-4";
//用户名
String username = "xxxxxx";
//用户密码
String password = "xxxxxx";
//账户名
String userName = "xxxxxx";
//请求的uri接口
String uri = "/v1.0/ocr/id-card";
//传入image或uri的json字符串(图片为公网的地址,可查询)
String param = "{\"url\":\"http://photocdn.sohu.com/20101021/Img276166786.jpg\"}";
//获取用token
String token = getToken(region,username,password,userName);
//返回请求的图片文字信息
System.out.println(getIdCard(region,token,param,uri));
}
//获取请求链接中用户的token信息
public static String getToken(String region,String username,String password,String userName) throws IOException {
String iam = "https://iam."+region+".myhuaweicloud.com/v3/auth/tokens";
String param = "{\"auth\":{\"identity\":{\"methods\":[\"password\"],\"password\":{\"user\":{\"name\":\"" + username + "\",\"password\":\"" + password + "\",\"domain\":{\"name\":\"" + userName + "\"}}}},\"scope\":{\"project\":{\"name\":\"cn-north-4\"}}}}";
PrintWriter out;
BufferedReader in = null;
String token = "";
String response = "";
try {
//需要请求的url
URL url = new URL(iam);
//打开和URL之间的连接
URLConnection connection = url.openConnection();
//设置通用的请求属性,请求头部分
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0");
// 发送POST请求必须设置如下两行
connection.setDoInput(true);
connection.setDoOutput(true);
// 建立实际的连接
connection.connect();
///获取URLConnection对象对应的输出流
out = new PrintWriter(connection.getOutputStream());
//发送请求参数
out.write(param);
//flush输出流的缓冲
out.flush();
//获取相应头中的token信息
token = connection.getHeaderField("X-Subject-Token");
//定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
//换行打印获取结果
response += "\n" + line;
}
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
//打印出相应头中的信息
//System.out.println(key + "--->" + map.get(key));
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
//返回所要用到的token信息
return token;
}
//请求云上接口,调用接口服务
public static String getIdCard(String region,String token,String param,String uri) throws IOException {
String ocr = "https://ocr."+region+".myhuaweicloud.com"+uri;
String response = "";
BufferedReader in = null;
try {
URL url = new URL(ocr);
URLConnection connection = url.openConnection();
//容易载跟头,表明请求体的部分为json形式
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("X-Auth-Token", token);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.write(param);
out.flush();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
response += "\n" + line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
//返回相应体中的结果,打印出来
return response;
}
}
返回成功