java实现百度云OCR⽂字识别⾼精度OCR识别⾝份证信息本⽂为⼤家分享了java实现百度云OCR识别的具体代码,⾼精度OCR识别⾝份证信息,供⼤家参考,具体内容如下
1.通⽤OCR⽂字识别
这种OCR只能按照识别图⽚中的⽂字,且是按照⾏识别返回结果,精度较低。
⾸先引⼊依赖包:
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.6.0</version>
</dependency>
通过OCR⼯具类:
package util;
import com.AipOcr;
import org.json.JSONObject;
import java.util.HashMap;
public class OcrApi {
private static final String APP_ID = "你的 App ID";
private static final String API_KEY = "Xb12m5t4jS2n7";
private static final String SECRET_KEY = "9XVx9GPcSbSUTZ";
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(
"lichunyu1234.oss-cn-shanghai.aliyuncs/1.png", options);
String(2);
}
public static void main(String[] args) {
System.out.println(result(getAipClient()));
}
}
结果如下,识别有两⾏信息(words即是识别的信息):
2.⾼精度OCR识别⾝份证信息
这种就⽐较⾼精度,且按照分类显⽰,返回数据更友好,⾼可⽤。
2.1 接⼝说明及请求参数是地址官⽅截图如下:
2.2 OCR⾝份证识别⼯具类
package util;
import com.alibaba.druid.util.Base64;
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
public class OcrUtil {
// Access_Token获取
private static final String ACCESS_TOKEN_HOST = "aip.baidubce/oauth/2.0/token?";  // ⾝份证识别请求URL
private static final String OCR_HOST = "aip.baidubce/rest/2.0/ocr/v1/idcard?";
// apiKey,secretKey
private static final String API_KEY ="Xb12m5t4jS";
private static final String SECRET_KEY = "9XVx9GPcSbSUT";
// 获取百度云OCR的授权access_token
public static String getAccessToken() {
return getAccessToken(API_KEY, SECRET_KEY);
}
/**
* 获取百度云OCR的授权access_token
* @param apiKey
* @param secretKey
* @return
*/
public static String getAccessToken(String apiKey, String secretKey) {
String accessTokenURL = ACCESS_TOKEN_HOST
// 1. grant_type为固定参数
+ "grant_type=client_credentials"
// 2. 官⽹获取的 API Key
+ "&client_id=" + apiKey
// 3. 官⽹获取的 Secret Key
+ "&client_secret=" + secretKey;
try {
URL url = new URL(accessTokenURL);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// 获取响应头
Map<String, List<String>> map = HeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "---->" + (key));
}fastjson字符串转数组
// 定义 BufferedReader输⼊流来读取URL的响应
BufferedReader bufferedReader = new BufferedReader(new InputStream()));
StringBuilder result = new StringBuilder();
String inputLine;
while ((inputLine = adLine()) != null) {
result.append(inputLine);
}
JSONObject jsonObject = JSONObject.String());
String("access_token");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取⾝份证识别后的数据
* @param imageUrl
* @param idCardSide
* @return
*/
public static String getStringIdentityCard(File imageUrl, String idCardSide) {
// ⾝份证OCR的http URL+鉴权token
String OCRUrl = OCR_HOST+"access_token="+getAccessToken();
System.out.println(OCRUrl);
System.out.println("***************************************************");
System.out.println(getAccessToken());
// 对图⽚进⾏base64处理
String image = encodeImageToBase64(imageUrl);
// 请求参数
String requestParam = "detect_direction=true&id_card_side="+idCardSide+"&image="+image;
try {
// 请求OCR地址
URL url = new URL(OCRUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求⽅法为POST
connection.setRequestMethod("POST");
// 设置请求头
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("apiKey", API_KEY);
connection.setDoOutput(true);
// 定义 BufferedReader输⼊流来读取URL的响应
BufferedReader bufferedReader = new BufferedReader(new InputStream(), StandardCharsets.UTF_8));      StringBuilder result = new StringBuilder();
String inputLine;
while ((inputLine = adLine()) != null) {
result.append(inputLine);
}
bufferedReader.close();
String();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/
**
* 对图⽚url进⾏Base64编码处理
* @param imageUrl
* @return
*/
public static String encodeImageToBase64(File imageUrl) {
// 将图⽚⽂件转化为字节数组字符串,并对其进⾏Base64编码处理
byte[] data = null;
try {
InputStream inputStream = new FileInputStream(imageUrl);
data = new byte[inputStream.available()];
inputStream.close();
// 对字节数组Base64编码
de(Base64.byteArrayToBase64(data), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 提取OCR识别⾝份证有效信息
* @param
* @return
*/
public static Map<String, String> getIdCardInfo(MultipartFile image, int idCardSide) {
String value = getStringIdentityCard(image, idCardSide);
String side;
if (idCardSide == 1) {
side = "正⾯";
}else {
side = "背⾯";
}
Map<String, String> map = new HashMap<>();
JSONObject jsonObject = JSONObject.parseObject(value);
JSONObject words_result = JSONObject("words_result");
if (words_result == null || words_result.isEmpty()) {
throw new MyException("请提供⾝份证"+side+"图⽚");
}
for (String key : words_result.keySet()) {
JSONObject result = JSONObject(key);
String info = String("words");
switch (key) {
case "姓名":
map.put("name", info);
break;
case "性别":
map.put("sex", info);
break;
case "民族":
map.put("nation", info);
break;
case "出⽣":
map.put("birthday", info);
break;
case "住址":
map.put("address", info);
break;
case "公民⾝份号码":
map.put("idNumber", info);
break;
case "签发机关":
map.put("issuedOrganization", info);
break;
case "签发⽇期":
map.put("issuedAt", info);
break;
case "失效⽇期":
map.put("expiredAt", info);
break;
}
}
return map;
}
}
官⽅返回⽰例:
对于⾝份证识别有个⼤坑:
1.有的base64编码后有头部“Base64:”要去掉,阿⾥巴巴的base64可以正常使⽤。
2.OCR识别官⽅只说明图⽚要Base64编码,但是实际上还是要再UrlEncode再编码⼀次才可以。以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。