免费使用识别身份证信息、银行卡、驾驶证、行驶证、车牌、营业执照、网络图片文字等。。。。
身份证效果展示(其他不展示)
前提
-
注册注册百度云,注册之后登录。
-
点击右上角的头像,跳转页面之后,点击左上角的菜单,选择文字识别。
-
点击创建应用,按照要求填写信息。到达这个界面。
-
按照领取免费使用资源领取刚刚创建应用的使用资源。
-
开始进行实现功能。
功能实现
1. 导入依赖百度jar
<!--百度api识别-->
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.16.3</version>
</dependency>
2. 写一个测试类
APP_ID、 API_KEY、SECRET_KEY更换成自己的。
import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
/**
* @author lenovo
*/
@RestController
@RequestMapping("/baidu")
public class BaiduController {
/**
* 识别身份证信息、银行卡、驾驶证、行驶证、车牌、营业执照、网络图片文字等。。。。
*/
@GetMapping("/carId")
public void export1(){
String APP_ID="xxxx";
String API_KEY="xxxxxxxxxxx";
String SECRET_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
//身份证图片
String frontImgPath="F:/baidujdktemplant/carId_1.jpg";
String backImgPath="F:/baidujdktemplant/carId_2.jpg";
//银行卡图片
String bankImgPath="F:/baidujdktemplant/carId_2.jpg";
//驾驶证图片
String drivingImgPath="F:/baidujdktemplant/carId_2.jpg";
//行驶证图片
String vehicleImgPath="F:/baidujdktemplant/carId_2.jpg";
//车牌
String plateImgPath="F:/baidujdktemplant/carId_2.jpg";
//营业执照
String businessImgPath="F:/baidujdktemplant/carId_2.jpg";
//网络图片文字(可以是本地图片、网络图片)
String webImgPath="F:/baidujdktemplant/carId_2.jpg";
// 初始化一个AipOcr
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
// 传入可选参数调用接口
HashMap<String, String> options = new HashMap<String, String>();
options.put("detect_direction", "true");
options.put("detect_risk", "false");
// 识别身份证正面(正面图片为本地图片,即D:\1.png)
JSONObject frontres = client.idcard(frontImgPath, "front", options);
System.out.println(frontres.toString(2));
// 识别身份证反面(反面图片为本地图片,即D:\2.png)
JSONObject backres = client.idcard(backImgPath, "back", options);
System.out.println(backres.toString(2));
// 识别银行卡
JSONObject res = client.bankcard(bankImgPath, options);
System.out.println(res.toString(2));
// 识别银行卡、
JSONObject bank = client.bankcard(bankImgPath, options);
System.out.println(bank.toString(2));
// 识别驾驶证、
JSONObject driving = client.drivingLicense(drivingImgPath, options);
System.out.println(driving.toString(2));
// 识别行驶证、
JSONObject vehicle = client.vehicleLicense(vehicleImgPath, options);
System.out.println(vehicle.toString(2));
// 识别车牌、
JSONObject plate = client.plateLicense(plateImgPath, options);
System.out.println(plate.toString(2));
// 识别营业执照、
JSONObject business = client.businessLicense(businessImgPath, options);
System.out.println(business.toString(2));
// 识别网络图片文字
JSONObject web = client.webImage(webImgPath, options);
System.out.println(web.toString(2));
}
}
3. 调用该接口输出结果。
4. 错误代码请参考
官方错误代码
如果是错误代码18,说明领取资源有问题。