阿里云身份证识别调用

2017-07-28  本文已影响204人  51344b645c49

自己做个记录,不想弄样式了。


public static voidmain(String[] args) {

String filePath ="";

String base64Str =getImageBinary(filePath);

String host ="https://dm-51.data.aliyun.com";

String path ="/rest/160601/ocr/ocr_idcard.json";

String method ="POST";

String appcode ="xxxxxxx";

Map headers =newHashMap();

//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105

headers.put("Authorization","APPCODE "+ appcode);

//根据API的要求,定义相对应的Content-Type

headers.put("Content-Type","application/json; charset=UTF-8");

Map querys =newHashMap();

String bodys ="{\"inputs\": [{\"image\": {\"dataType\": 50,\"dataValue\":\""+base64Str+"\"},";

bodys+="\"configure\": {\"dataType\": 50,\"dataValue\":\"{\\\"side\\\":\\\"face\\\"}\"}}]}";

System.out.println(bodys);

try{

/**

* 重要提示如下:

* HttpUtils请从

* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java

* 下载

*

* 相应的依赖请参照

* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml

*/

HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);

System.out.println(response.toString());

//获取response的body

System.out.println(EntityUtils.toString(response.getEntity()));

}catch(Exception e) {

e.printStackTrace();

}

}

staticString getImageBinary(String path){

BASE64Encoder encoder =newsun.misc.BASE64Encoder();

File f =newFile(path);

BufferedImage bi;

try{

bi = ImageIO.read(f);

ByteArrayOutputStream baos =newByteArrayOutputStream();

ImageIO.write(bi,"jpg", baos);

byte[] bytes = baos.toByteArray();

returnencoder.encodeBuffer(bytes).trim();

}catch(IOException e) {

e.printStackTrace();

}

return null;

}

上一篇下一篇

猜你喜欢

热点阅读