第一步:进入身份认证首页
https://ai.baidu.com/tech/ocr_cards/idcard
点击立即使用进入管理中心页面
第二步:点击创建应用,选择身份识别等完成创建
第三步:在百度ocr中领取免费资源,找到身份识别,完成资源的领取
第四步:完成相关依赖引入
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
第五步:
1.只是通用ocr文字识别则完成
下面的java编写
import java.util.HashMap;
import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;
/**
* @author sun 通用OCR文字识别
* @date 2022-11-11 20:10
* @Decsription: com.ocr.util
* @version: 1.0
*/
public class OcrApi {
private static final String APP_ID = "";
private static final String API_KEY = "";
private static final String SECRET_KEY = "";
private static AipOcr getAipClient() {
return getAipClient(API_KEY, SECRET_KEY);
}
public static AipOcr getAipClient(String apiKey, String secretKey) {
AipOcr client = new AipOcr(APP_ID, apiKey, secretKey);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
return client;
}
public static String result(AipOcr client) {
// 传入可选参数调用接口
HashMap<String, String> options = new HashMap<>();
options.put("language_type", "CHN_ENG");
options.put("detect_direction", "true");
options.put("detect_language", "true");
options.put("probability", "true");
JSONObject res = client.basicGeneralUrl(
"图片路径", options);
return res.toString(2);
}
public static void main(String[] args) {
System.out.println(result(getAipClient()));
}
}
2.高精度OCR识别身份证信息
一。获取Access_token
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import org.json.JSONObject;
public class AccessTokenUtils {
private static String APIKEY = "";
private static String SecretKEY = "";
// 获取Token路径
private static String PATH = "https://aip.baidubce.com/oauth/2.0/token?";
public static String getAuth() {
// 获取token地址
String getAccessTokenUrl = PATH
// 1. grant_type为固定参数
+ "grant_type=client_credentials"
// 2. 官网获取的 API Key
+ "&client_id=" + APIKEY
// 3. 官网获取的 Secret Key
+ "&client_secret=" + SecretKEY;
try {
URL realUrl = new URL(getAccessTokenUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String result = "";
String line;
while ((line = in.readLine()) != null) {
result += line;
}
JSONObject jsonObject = new JSONObject(result);
String access_token = jsonObject.getString("access_token");
return access_token;
} catch (Exception e) {
System.err.printf("获取token失败!");
e.printStackTrace(System.err);
}
return null;
}
}
二。本地上传需要将图片转为Base64码,Url图片可以直接传网络地址
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import sun.misc.BASE64Encoder;
public class BaseImg64Utils {
/**
* 将一张本地图片转化成Base64字符串
* @param imgPath 本地图片地址
* @return 图片转化base64后再UrlEncode结果
*/
public static String getImageStrFromPath(String imgPath) {
InputStream in = null;
byte[] data = null;
// 读取图片字节数组
try {
in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
// 返回Base64编码过的字节数组字符串
return encoder.encode(data).replaceAll("\r\n", "").replaceAll("\\+", "%2B");
}
}
三。调用API接口的方法,获取识别结果
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class XszOcrUtils {
//注意注意:这个采用的是身份识别请求地址,如果想用驾驶证请换一个
private static final String POST_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token="+ AccessTokenUtils.getAuth();
//必传参数:id_card_side=frontfront:身份证含照片的一面 -back:身份证带国徽的一面 自动检测身份证正反面,如果传参指定方向与图片相反,支持正常识别,返回参数image_status字段为"reversed_side"
/**
* 识别本地图片的文字
*
* @param path 本地图片地址
* @return 识别结果,为json格式
* @throws URISyntaxException URI打开异常
* @throws IOException io流异常
*/
public static String checkFile(String path) throws URISyntaxException, IOException {
File file = new File(path);
if (!file.exists()) {
throw new NullPointerException("图片不存在");
}
String image = BaseImg64Utils.getImageStrFromPath(path);
String param = "image=" + image + "&id_card_side=front";
return post(param);
}
/**
* @param url 图片url
* @return 识别结果,为json格式
*/
public static String checkUrl(String url) throws IOException, URISyntaxException {
String param = "url=" + url;
return post(param);
}
/**
* 通过传递参数:url和image进行文字识别
*
* @param param 区分是url还是image识别
* @return 识别结果
* @throws URISyntaxException URI打开异常
* @throws IOException IO流异常
*/
private static String post(String param) throws URISyntaxException, IOException {
// 开始搭建post请求
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost();
URI url = new URI(POST_URL);
post.setURI(url);
// 设置请求头,请求头必须为application/x-www-form-urlencoded,因为是传递一个很长的字符串,不能分段发送
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
StringEntity entity = new StringEntity(param);
post.setEntity(entity);
HttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
String str;
try {
/* 读取服务器返回过来的json字符串数据 */
str = EntityUtils.toString(response.getEntity());
return str;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return null;
}
public static void main(String[] args) throws URISyntaxException, IOException {
String checkFile = checkFile("E:\\tmp_40d75be6049049f841cbbee213743430d699596c8c08ae47ac76a60f36de6189.jpeg");
System.out.println("========" + checkFile);
}
其中需要对上述代码进行修改的地方1.在AccessTokenUtils中对APIKEY,SecretKEY在百度ocr官网进行查找编写。2.在XszOcrUtils文件中对main方法中checkFile中路径地址改成本地图片路径
其中遇到的问题:1.import sun.misc.BASE64Encoder;报错原因在于jdk版本没有配置好,应该使用1.8版本