准备工作:在libs下添加 alicloud-Android-apigateway-sdk-1.0.1.jar,commons-codec-1.10-1.jar

在build.gradle添加  compile'com.squareup.okhttp3:okhttp:3.4.1'

在onCreate,或者afterViews初始化API网关

private void initGatewaySdk() {
// 初始化API网关
ApiGatewayClient.init(getApplicationContext(), false);
}

调用拍照功能

private void getPhotoCard(){
final String status = Environment.getExternalStorageState();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, CAMERA_REQUEST_CODE);
}
if (status.equals(Environment.MEDIA_MOUNTED)) {
defaultPhotoAddress = PHOTO_DIR + "/" + getPhotoName();
PreferenceUtils.modifyStringValueInPreferences(CardActivity.this, Preferences.IMAGE_3, defaultPhotoAddress);
imageUri = Uri.fromFile(new File(defaultPhotoAddress)); Intent intentPhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture
intentPhoto.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intentPhoto, CAMERA_REQUEST_CODE); } else {
Toast.makeText(CardActivity.this, "没有sd卡", Toast.LENGTH_SHORT).show();
}
} 可以对照片进行裁剪
private void cropImageUri(Uri desUri, int outputX, int outputY, int requestCode){
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(desUri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 5);
intent.putExtra("aspectY", 3);
intent.putExtra("outputX", outputX);
intent.putExtra("outputY", outputY);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, desUri);
intent.putExtra("return-data", false);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, requestCode);
} 用base64上传照片
private void trygetCardNum() {
if (NetworkUtils.isConnectWithTip(this, "您未连接网络,无法获取数据")) {
LoadingUtil.show(this);
String imgBase64 = "";
try {
defaultPhotoAddress = PreferenceUtils.getStringValueInPreferences(this, Preferences.IMAGE_3);
File file = new File(defaultPhotoAddress);
byte[] content = new byte[(int) file.length()];
FileInputStream finputstream = new FileInputStream(file);
finputstream.read(content);
finputstream.close();
imgBase64 = new String(Base64.encodeBase64(content));
// imgBase64 = Base64Img.Bitmap2StrByBase64(bitmap);
if(imgBase64!=null||!imgBase64.equals("")){
PreferenceUtils.modifyStringValueInPreferences(CardActivity.this, Preferences.IMAGE_3, null);
}
} catch (IOException e) {
e.printStackTrace();
return;
} // 获取服务
RpcService rpcService = ApiGatewayClient.getRpcService();
final ApiRequest apiRequest = new ApiRequest();
// 设置请求地址、Path及Method
apiRequest.setAddress("https://dm-51.data.aliyun.com");
apiRequest.setPath("/rest/160601/ocr/ocr_idcard.json");
apiRequest.setMethod(HttpMethod.POST);
// 按照文档设置二进制形式Body,支持设置Query参数、Header参数、Form形式Body
apiRequest.setStringBody("{\"inputs\":[{\"image\":{\"dataType\":50,\"dataValue\":\""+imgBase64+"\"},\"configure\":{\"dataType\":50,\"dataValue\":\"{\\\"side\\\":\\\"face\\\"}\"}}]}");
// 设置支持自签等形式的证书,如果服务端证书合法请勿设置该值,仅在开发测试或者非常规场景下设置。
apiRequest.setTrustServerCertificate(true);
// 设置超时
apiRequest.setTimeout(10000);
rpcService.call(apiRequest, new ApiResponseCallback() {
@Override
public void onSuccess(ApiResponse apiResponse) {
// 处理apiResponse
LoadingUtil.dismiss();
String s = apiResponse.getStringBody();
NumBean result = JSONObject.parseObject(s, NumBean.class);
String dataValue = result.getOutputs().get(0).getOutputValue().getDataValue();
DataValueBean dataValueBean = JSONObject.parseObject(dataValue, DataValueBean.class);
Number = dataValueBean.getNum();
name = dataValueBean.getName();
address = dataValueBean.getAddress();
birth = dataValueBean.getBirth();
nationality = dataValueBean.getNationality();
sex = dataValueBean.getSex();
if(dataValueBean.getError_msg()==null){
runOnUiThread(new Runnable() {
public void run() {
if(Number == null||Number.equals("")||name == null||name.equals("")||address == null||address.equals("")||birth == null||birth.equals("")||sex == null||sex.equals("")){
Toast.makeText(CardActivity.this, "扫描失败,请重试", Toast.LENGTH_LONG).show();
}else {
//扫描成功
}
}
});
}else {
errString = dataValueBean.getError_msg();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(CardActivity.this, "扫描失败,请重试", Toast.LENGTH_LONG).show();
}
});
}
}
@Override
public void onException(ApiInvokeException e) {
// 处理异常
LoadingUtil.dismiss();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(CardActivity.this, "扫描失败,请重试", Toast.LENGTH_LONG).show();
}
});
}
});
}
} 附:
public class NumBean implements Serializable{

    private List<OutputsBean> outputs;

    public List<OutputsBean> getOutputs() {
return outputs;
} public void setOutputs(List<OutputsBean> outputs) {
this.outputs = outputs;
} public static class OutputsBean { private String outputLabel;
private OutputMultiBean outputMulti;
private OutputValueBean outputValue; public String getOutputLabel() {
return outputLabel;
} public void setOutputLabel(String outputLabel) {
this.outputLabel = outputLabel;
} public OutputMultiBean getOutputMulti() {
return outputMulti;
} public void setOutputMulti(OutputMultiBean outputMulti) {
this.outputMulti = outputMulti;
} public OutputValueBean getOutputValue() {
return outputValue;
} public void setOutputValue(OutputValueBean outputValue) {
this.outputValue = outputValue;
} public static class OutputMultiBean {
} public static class OutputValueBean { private int dataType;
private String dataValue; public int getDataType() {
return dataType;
} public void setDataType(int dataType) {
this.dataType = dataType;
} public String getDataValue() {
return dataValue;
} public void setDataValue(String dataValue) {
this.dataValue = dataValue;
}
}
}
}
public class DataValueBean implements Serializable{

    private String address;
private String birth;
private String config_str;
private String error_msg;
private String name;
private String nationality;
private String num;
private String request_id;
private String sex;
private boolean success; public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getBirth() {
return birth;
} public void setBirth(String birth) {
this.birth = birth;
} public String getConfig_str() {
return config_str;
} public void setConfig_str(String config_str) {
this.config_str = config_str;
} public String getError_msg() {
return error_msg;
} public void setError_msg(String error_msg) {
this.error_msg = error_msg;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getNationality() {
return nationality;
} public void setNationality(String nationality) {
this.nationality = nationality;
} public String getNum() {
return num;
} public void setNum(String num) {
this.num = num;
} public String getRequest_id() {
return request_id;
} public void setRequest_id(String request_id) {
this.request_id = request_id;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public boolean isSuccess() {
return success;
} public void setSuccess(boolean success) {
this.success = success;
}
} 在AndroidManifest.xml下添加 Appkey
<meta-data android:name="com.alibaba.apigateway.appKey" android:value="" />
<meta-data android:name="com.alibaba.apigateway.appSecret" android:value="" />
 

Android如何用阿里云的API进行身份证识别的更多相关文章

  1. C#调用阿里云CDN API刷新缓存

    使用CDN必须要解决CDN缓存的问题,要么在每次更新文件时生成不同的URL,要么在每次更新文件时刷新CDN缓存.我们在一个实际应用场景中用到了后者,所以需要调用阿里云CDN的API进行缓存刷新的操作. ...

  2. nodejs版 阿里云开放api签名算法

    阿里云 API 签名 github:https://github.com/liuyinglong/aliyun; npm :https://www.npmjs.com/package/aliyun-a ...

  3. 阿里云Open API自动化脚本—ECS公网IP转化弹性公网IP

    1.OpenAPI Explorer 记录一下使用阿里云 Open API 自动化/脚本化 “ECS 公网 IP 转化弹性公网 IP”的实现 全过程.原博客地址:https://www.markedi ...

  4. 阿里云DNS api接口 shell 更改DNS解析

    可定时任务检查域名解析,调用alidns.sh更新DNS解析 #!/bin/bash # alidns.sh #https://www.cnblogs.com/elvi/p/11663910.html ...

  5. C#-Api:身份证识别

    ylbtech-C#-Api:身份证识别 通过POST上传base64格式的图片内容,可识别二代身份证.驾照.行驶证.军官证.中华人民共和国往来港澳通行证.台湾居民往来大陆通行证.大陆居民往来台湾通行 ...

  6. springmvc学习笔记--支持文件上传和阿里云OSS API简介

    前言: Web开发中图片上传的功能很常见, 本篇博客来讲述下springmvc如何实现图片上传的功能. 主要讲述依赖包引入, 配置项, 本地存储和云存储方案(阿里云的OSS服务). 铺垫: 文件上传是 ...

  7. 基于阿里云 DNS API 实现的 DDNS 工具

    0.简要介绍 0.1 思路说明 AliDDNSNet 是基于 .NET Core 开发的动态 DNS 解析工具,借助于阿里云的 DNS API 来实现域名与动态 IP 的绑定功能.工具核心就是调用了阿 ...

  8. 微信小程序里如何用阿里云上传视频,图片。。

    纯手写,踩了半天多的坑干出来了... 网上也有对于阿里云如何在微信小程序里使用,但是很不全,包括阿里云文档的最佳实践里. 话不多说上代码了. upvideo(){ var aliOssParams = ...

  9. python 调用阿里云服务器api创建服务器

    首先安装阿里云SDK pip install aliyun-python-sdk-core pip install aliyun-python-sdk-ecs 可以配合jenkins传递参数 #!/u ...

随机推荐

  1. 重入锁ReentrantLock用法以及如何实现重进入

    在java多线程中,可以使用synchronized实现线程之间的同步互斥,但在jdk1.5中增加了ReentrantLock类也能达到同样的效果,而且在使用上更加灵活,扩展功能上更加强大. 创建My ...

  2. 【HDU2007】平方和与立方和

    http://acm.hdu.edu.cn/showproblem.php?pid=2007 垃圾水题 随便搜了几个公式(但我实际写的暴力...题目保证不爆int,说明n,m<=10^3) 1^ ...

  3. lucene 5可以运行的demo

    package hello; import java.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.ap ...

  4. codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)

    题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...

  5. 内部锁之一:锁介绍(偏向锁 & 轻量级锁 & 重量级锁 & 各自优缺点及场景)

    一.内部锁介绍 上篇文章<Synchronized之二:synchronized的实现原理>中向大家介绍了Synchronized原理及优化锁.现在我们应该知道,Synchronized是 ...

  6. Collection View Programming Guide for iOS---(二)----Collection View Basics

      Collection View Basics Collection View 基础 To present its content onscreen, a collection view coope ...

  7. 020--python函数基础知识考试(包括:函数_递归等知识)

    1.列举布尔值为 False 的值 空,None,0, False, '', [], {}, () 2.写函数: 根据范围获取其中 3 和 7 整除的所有数的和,并返回调用者:符合条件的数字个数以及符 ...

  8. bzoj 4197: [Noi2015]寿司晚宴【状压dp】

    一个数内可能多个的质因数只有小于根号n的,500内这样的数只有8个,所以考虑状压 把2~n的数处理出小于根号500的质因数集压成s,以及大质数p(没有就是1),然后按p排序 根据题目要求,拥有一个质因 ...

  9. 函数和代码复用 --Python

    1.关于递归函数的描述,以下选项中正确的是 A.包含一个循环结构 B.函数比较复杂 C.函数内部包含对本函数的再次调用 D.函数名称作为返回值 答案:D 答案解析:递归函数是指函数内部包含对本函数的再 ...

  10. 安全性测试入门 (三):CSRF 跨站请求伪造攻击和防御

    本篇继续对于安全性测试话题,结合DVWA进行研习. CSRF(Cross-site request forgery):跨站请求伪造 1. 跨站请求伪造攻击 CSRF则通过伪装成受信任用户的请求来利用受 ...