1. 将RestTemplate加载到bean工厂
@Configuration
public class RestTemplateConfig {
private Logger logger = LogManager.getLogger(getClass());
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
logger.debug("-----------加载RestTemplate模板----------");
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);
factory.setConnectTimeout(15000);
return factory;
}
}
2.调用接口,获取单位名称,和统一社会信用代码
//营业执照识别
@Test
public void businessLicenseTest() {
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_license?access_token="+access_token;
String image = getImage("g://picture//c2.png");
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/x-www-form-urlencoded");
MultiValueMap postParameters = new LinkedMultiValueMap();
postParameters.add("image", image);
HttpEntity> requestEntity = new HttpEntity>(
postParameters, headers);
String result = template.postForObject(url,requestEntity,String.class);
try {
result = new String (result.getBytes("iso-8859-1"),"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("--------"+result);
printResult(result,"社会信用代码");
}
public void printResult(String result , String item){
JSONObject jsonObject = new JSONObject(result);
JSONObject words_result = (JSONObject)jsonObject.get("words_result");
JSONObject name = (JSONObject)words_result.get(item);
String name1 = (String)name.get("words");
System.err.println(item + "--" + name1);
}
public String getImage(String url){
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try
{
in = new FileInputStream(url);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
String image = Base64.encode(data);
return image;
}
图片
3,结果其他接口调用
package com.zkingcai.scf.report;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.xerces.impl.dv.util.Base64;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import com.github.pagehelper.util.StringUtil;
public class BaiDuOCRTest{
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
String access_token = "24.503a5050fac4e35aff4125ef6eaf2cff.2592000.1517983793.282335-10646277";
String access_token_tu = "24.dc1fd2a0bc0f57fe221c6f1d05c5784a.2592000.1519463229.282335-10748983";
String access_token_face = "24.26502c2e78c6d2d9e437358df55a37f7.2592000.1508652230.282335-10175731";
String access_token_tuxiang = "24.59aae0f81717babfbbca04d456e7d5ac.2592000.1519541300.282335-10752812";
// 通用文字
@Test
public void generalBasic() {
// 一般精度
//String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token="+access_token;
// 高精度
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token="+access_token;
// 生僻字
//String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_enhanced?access_token="+access_token;
String image = "g://picture//spz.png";
JSONObject jsonObject = generalMethod(url,image,"");
JSONArray words_result = (JSONArray)jsonObject.get("words_result");
String str = "";
for(int i = 0; i < words_result.length() ; i ++){
JSONObject object = (JSONObject)words_result.get(i);
str = object.get("words").toString();
System.err.println(str);
}
}
// 网络图片文字
@Test
public void webImageTest() {
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage?access_token="+access_token;
String image = "g://picture//spz.png";
JSONObject jsonObject = generalMethod(url,image,"");
JSONArray words_result = (JSONArray)jsonObject.get("words_result");
String str = "";
for(int i = 0; i < words_result.length() ; i ++){
JSONObject object = (JSONObject)words_result.get(i);
str += object.get("words");
}
System.err.println(str);
Pattern pattern = Pattern.compile("\\d{15}");
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println("注册号---" + matcher.group());
}
}
// 身份证识别
@Test
public void IDCardTest() {
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token="+access_token;
String image = "g://picture//c1.png";
JSONObject jsonObject = generalMethod(url,image,"");
printResult(jsonObject,"公民身份号码");
}
// 银行卡
@Test
public void bankCardTest() {
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard?access_token="+access_token;
String image = "g://picture//bankCard.jpg";
JSONObject jsonObject = generalMethod(url,image,"");
JSONObject words_result = (JSONObject)jsonObject.get("result");
Object bank_name = words_result.get("bank_name");
Object bank_card_number = words_result.get("bank_card_number");
System.err.println("bank_name -- " + bank_name);
System.err.println("bank_card_number -- " + bank_card_number);
}
// 驾驶证
@Test
public void drivingLicenseTest() {
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license?access_token="+access_token;
String image = "g://picture//driver3.jpg";
JSONObject jsonObject = generalMethod(url,image,"");
printResult(jsonObject,"证号");
printResult(jsonObject,"姓名");
}
//行驶证
@Test
public void vehicleLicenseTest() {
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license?access_token="+access_token;
String image = "g://picture//vehicle.jpg";
JSONObject jsonObject = generalMethod(url,image,"");
printResult(jsonObject,"品牌型号");
printResult(jsonObject,"住址");
}
// 车牌
@Test
public void licensePlateTest() {
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token="+access_token;
String image = "g://picture//plate.jpg";
JSONObject jsonObject = generalMethod(url,image,"");
JSONObject words_result = (JSONObject)jsonObject.get("words_result");
String color = (String) words_result.get("color");
String number = (String)words_result.get("number");
System.err.println(color);
System.err.println(number);
}
// 营业执照识别
@Test
public void businessLicenseTest() {
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_license?access_token="+access_token;
String image = "g://picture//c2.png";
JSONObject jsonObject = generalMethod(url,image,"");
printResult(jsonObject,"社会信用代码");
printResult(jsonObject,"单位名称");
}
// 表格文字识别
@Test
public void formTest() {
String url = "https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/request?access_token="+access_token;
String image = "g://picture//form.png";
JSONObject jsonObject = generalMethod(url,image,"");
JSONArray result = (JSONArray)jsonObject.get("result");
for(int i = 0; i < result.length() ; i ++){
JSONObject object = (JSONObject)result.get(i);
String request_id = object.get("request_id").toString();
System.err.println(request_id);
String url2 = "https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/get_request_result?access_token="+access_token;
JSONObject jsonObject2 = generalMethod(url2,"",request_id);
}
}
// 通用票据识别
@Test
public void receiptTest() {
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/receipt?access_token="+access_token;
String image = "g://picture//fp.png";
JSONObject jsonObject = generalMethod(url,image,"");
}
// 公共方法提取
public JSONObject generalMethod(String url,String imageUrl,String request_id) {
requestFactory.setConnectTimeout(10000);
requestFactory.setReadTimeout(10000);
RestTemplate template = new RestTemplate(requestFactory);
String image = "";
if(StringUtil.isEmpty(request_id)){
image = getImage(imageUrl);
}
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/x-www-form-urlencoded");
MultiValueMap postParameters = new LinkedMultiValueMap();
if(StringUtil.isNotEmpty(request_id)){
postParameters.add("request_id", request_id);
}else{
postParameters.add("image", image);
postParameters.add("id_card_side", "front");
}
HttpEntity> requestEntity = new HttpEntity>(
postParameters, headers);
String result = template.postForObject(url,requestEntity,String.class);
try {
result = new String (result.getBytes("iso-8859-1"),"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("--------"+result);
JSONObject jsonObject = new JSONObject(result);
return jsonObject;
}
//菜品图片
@Test
public void dishTest() {
String url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/dish?access_token="+access_token_tu;
String image = "g://picture//ya.jpg";
JSONObject jsonObject = generalMethod(url,image,"");
JSONArray words_result = (JSONArray)jsonObject.get("result");
JSONObject jsonObject2 = (JSONObject)words_result.get(0);
System.err.println("卡路里 ------ " + jsonObject2.get("calorie"));
System.err.println("菜名 ------ " + jsonObject2.get("name"));
}
// 车图片
@Test
public void carTest() {
String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/car?access_token="+access_token_tu;
String image = "g://picture//che1.jpg";
JSONObject jsonObject = generalMethod(url,image,"");
JSONArray words_result = (JSONArray)jsonObject.get("result");
JSONObject jsonObject2 = (JSONObject)words_result.get(0);
System.err.println("车名 ------ " + jsonObject2.get("name"));
}
// 图片审核
@Test
public void tuxiangTest() {
String url = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/user_defined?access_token="+access_token_tuxiang;
String image = "g://picture//zx.jpg";
JSONObject jsonObject = generalMethod(url,image,"");
// JSONArray words_result = (JSONArray)jsonObject.get("result");
// JSONObject jsonObject2 = (JSONObject)words_result.get(0);
// System.err.println("车名 ------ " + jsonObject2.get("name"));
}
//人脸图片
@Test
public void faceTest() {
requestFactory.setConnectTimeout(10000);
requestFactory.setReadTimeout(10000);
RestTemplate template = new RestTemplate(requestFactory);
String url = "https://aip.baidubce.com/rest/2.0/face/v1/detect?access_token="+access_token_face;
String image = getImage("g://picture//huge2.jpg");
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/x-www-form-urlencoded");
MultiValueMap postParameters = new LinkedMultiValueMap();
postParameters.add("image", image);
HttpEntity> requestEntity = new HttpEntity>(
postParameters, headers);
String result = template.postForObject(url,requestEntity,String.class);
try {
result = new String (result.getBytes("iso-8859-1"),"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("--------"+result);
// JSONObject jsonObject = new JSONObject(result);
// JSONArray words_result = (JSONArray)jsonObject.get("result");
// JSONObject jsonObject2 = (JSONObject)words_result.get(0);
// System.err.println("车名 ------ " + jsonObject2.get("name"));
}
//人脸图片对比
@Test
public void faceMathTest() {
requestFactory.setConnectTimeout(10000);
requestFactory.setReadTimeout(10000);
RestTemplate template = new RestTemplate(requestFactory);
String url = "https://aip.baidubce.com/rest/2.0/face/v2/match?access_token="+access_token_face;
String image = getImage("g://picture//huge2.jpg");
String image2 = getImage("g://picture//huge2.jpg");
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/x-www-form-urlencoded");
MultiValueMap postParameters = new LinkedMultiValueMap();
postParameters.add("image", image+","+image2);
HttpEntity> requestEntity = new HttpEntity>(
postParameters, headers);
String result = template.postForObject(url,requestEntity,String.class);
try {
result = new String (result.getBytes("iso-8859-1"),"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("--------"+result);
// JSONObject jsonObject = new JSONObject(result);
// JSONArray words_result = (JSONArray)jsonObject.get("result");
// JSONObject jsonObject2 = (JSONObject)words_result.get(0);
// System.err.println("车名 ------ " + jsonObject2.get("name"));
}
public void printResult(JSONObject jsonObject , String item){
JSONObject words_result = (JSONObject)jsonObject.get("words_result");
JSONObject name = (JSONObject)words_result.get(item);
String name1 = (String)name.get("words");
System.err.println(item + "--" + name1);
}
public String getImage(String url){
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try
{
in = new FileInputStream(url);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
String image = Base64.encode(data);
return image;
}
}
@Test
public void getAuth() {//24.5a127f5a75099bef3934020f6ec88fc6.2592000.1508573319.282335-10172140
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(10000);
requestFactory.setReadTimeout(10000);
RestTemplate template = new RestTemplate(requestFactory);
String url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=YpCGYzILWIqlqWYjg0Gsotxc&client_secret=MDMO05sRjTXctrYqoQRvmxtQrj03wbwq";
String result = template.getForObject(url,String.class);
System.out.println("--------"+result);
}