整合开始之前需要自己去阿里云注册一个号
主要还是在阿里云的演示里把代码拿了过来做了一些修改,现在可以直接使用,HttpUtils包要在
https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java 这里下载
这个是营业执照的代码
import com.alibaba.fastjson.JSONObject;
import com.example.ocr.model.BusinessLicense;
import com.example.ocr.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@RestController
public class OCRCharterController {
@PostMapping("cccc")
public Object picOCRBack(@RequestParam("pic") MultipartFile file) throws Exception {
String fileStr = "";
if (!file.isEmpty())
{
BASE64Encoder encoder = new BASE64Encoder();
// 通过base64来转化图片
try {
fileStr = (encoder.encode(file.getBytes())).replaceAll("\r\n", "");
} catch (IOException e) {
e.printStackTrace();
}
}
String host = "https://dm-58.data.aliyun.com";
String path = "/rest/160601/ocr/ocr_business_license.json";
String method = "POST";
String appcode = "自己的appcode号";
Map<String, String> headers = new HashMap<>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
//根据API的要求,定义相对应的Content-Type
headers.put("Content-Type", "application/json; charset=UTF-8");
Map<String, String> querys = new HashMap<>();
String bodys = "{\"image\":\""+fileStr+"\"}";
Object object = null;
try {
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
System.out.println(response.toString());
String msg = new String(EntityUtils.toString(response.getEntity()).getBytes(),"UTF-8");
//转json对象到定义的实体类
object = JSONObject.toJavaObject(JSONObject.parseObject(msg), BusinessLicense.class);
System.out.println(object);
return object;
} catch (Exception e) {
e.printStackTrace();
}
return object;
}
}
下边这个就是身份证正面的认证
@RequestMapping(value = "faceCode",method = RequestMethod.POST)
public Object OCRFace(@RequestParam("code") MultipartFile file) throws Exception{
String fileStr = "";
if (!file.isEmpty())
{
BASE64Encoder encoder = new BASE64Encoder();
// 通过base64来转化图片
try {
fileStr = (encoder.encode(file.getBytes())).replaceAll("\r\n", "");
} catch (IOException e) {
e.printStackTrace();
}
}
String host = "http://dm-51.data.aliyun.com";
String path = "/rest/160601/ocr/ocr_idcard.json";
String method = "POST";
String appcode = "xxx";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE
headers.put("Authorization", "APPCODE " + appcode);
//根据API的要求,定义相对应的Content-Type
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
JSONObject requestObj = new JSONObject();
JSONObject configObj = new JSONObject();
configObj.put("side", "face");
requestObj.put("image",fileStr);
requestObj.put("configure",configObj.toString());
IdCard object = null;
try {
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, requestObj.toString());
String msg = new String(EntityUtils.toString(response.getEntity()).getBytes(),"UTF-8");
object = JSONObject.toJavaObject(JSONObject.parseObject(msg),IdCard.class);
System.out.println(object);
return object;
} catch (Exception e) {
e.printStackTrace();
return object;
}
}
反面认证只是需要吧configObj.put(“side”, “face”);里的face参数改为back就ok了
下面是实体类:
这是身份证的
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class IdCard {
//姓名
private String name;
//地址
private String address;
//性别
private String sex;
//身份证号
private String num;
//出生年月
private String birth;
//民族
private String nationality;
//签发机关
private String issue;
//开始时间
@JSONField(name = "start_date")
private String startDate;
//结束时间
@JSONField(name = "end_date")
private String endDate;
//是否识别成功
private boolean success;
//是否复印件
private boolean fake;
}
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class BusinessLicense {
//地址
private String address;
//经营范围
private String business;
//注册资本
private String capital;
//名称
private String name;
//法人
private String person;
//注册资金
@JSONField(name = "reg_num")
private String regNum;
//是否识别成功
private boolean success;
//公司类型
private String type;
//营业终止日期
@JSONField(name = "valid_period")
private String validPeriod;
//注册日期
@JSONField(name = "establish_date")
private String establishDate;
//是否复印件
private boolean fake;
}
ok,搞定。