Commit ee32b853 authored by Tyh's avatar Tyh

1

parent 46b4fe8c
......@@ -13,7 +13,7 @@
"clean": "rimraf www/*"
},
"dependencies": {
"IdCardPlugin": "file:../card-plugin-test/IdCardPlugin-test",
"autosize": "^3.0.20",
"better-scroll": "^1.10.3",
"cordova-android": "^6.2.0",
......
{
"name": "IdCardPlugin",
"version": "1.0.0",
"description": "",
"cordova": {
"id": "IdCardPlugin",
"platforms": []
},
"keywords": [
"ecosystem:cordova"
],
"author": "",
"license": "ISC"
}
<?xml version='1.0' encoding='utf-8'?>
<plugin id="IdCardPlugin" version="1.0.0" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>IdCardPlugin</name>
<js-module name="IdCardPlugin" src="www/IdCardPlugin.js">
<clobbers target="cordova.plugins.IdCardPlugin" />
</js-module>
<!-- android -->
<platform name="android">
<!-- 业务类指定 -->
<config-file target="res/xml/config.xml" parent="/*">
<feature name="IdCardIdentifyPlugin">
<param name="android-package" value="com.xg.idcard.IdCardIdentifyPlugin"/>
</feature>
</config-file>
<!-- 需要的android权限 -->
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<activity android:name="com.qsign.face.LivenessDetectActivity"/>
</config-file>
<!-- arr引用 -->
<resource-file src="src/android/lib/qsign_facesdk_xg_dev_1.0.0.aar" target="libs/qsign_facesdk_xg_dev_1.0.0.aar" />
<framework src="src/android/lib/idCard.gradle" custom="true" type="gradleReference" />
<source-file src="src/android/java/IdCardIdentifyPlugin.java" target-dir="src/com/xg/idcard/" />
</platform>
</plugin>
身份检测cordova插件
调用方式:
```
cordova.plugins.IdCardPlugin.idCardIdentify({"idNum":"123213213123123123123213","idName":"张三","timeout":10000}, function (success) {alert(success);
}, function (reason) {
alert("Failed: " + reason);
});
```
字段:
idNum : 身份证号码
idName: 用户名称
timeout:检测超时时间
返回值介绍:
​ 检测成功返回值:
​ {"code":1,"message":"检测成功"}
​ 检测失败返回值:
​ 1.{"code":0,"message":"参数错误"}
​ 2.{"code":0,"message":"idNum 或者 idName 为空"}
​ 3.{"code":0,"message":"检测失败"}
​ 4.{"code":0,"message":"未授予相机权限"}
package com.xg.idcard;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Base64;
import com.qsign.face.LivenessDetectActivity;
import com.qsign.face.api.FaceApi;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class IdCardIdentifyPlugin extends CordovaPlugin {
private static final String ID_CARD_IDENTIFY = "IdCardIdentify";
private String idNum;
private String idName;
private int timeOut;
private CallbackContext mCallbackContext;
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
initFace();
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
mCallbackContext = callbackContext;
if (ID_CARD_IDENTIFY.equals(action)) {
if (args == null) {
callbackContext.error(getResult(0,"参数错误"));
return false;
}
JSONObject idInfo = args.optJSONObject(0);
if (idInfo == null) {
callbackContext.error(getResult(0,"参数错误"));
return false;
}
idNum = idInfo.optString("idNum");
idName = idInfo.optString("idName");
if(idNum == null || idNum.isEmpty() || idName == null || idName.isEmpty()){
callbackContext.error(getResult(0,"idNum 或者 idName 为空"));
return false;
}
timeOut = idInfo.optInt("timeout", 10000);
checkCameraPermission();
} else {
callbackContext.error(getResult(0,"参数错误"));
return false;
}
return super.execute(action, args, callbackContext);
}
private void startIdCardCheck(){
Intent intent = new Intent(cordova.getActivity(), LivenessDetectActivity.class);
intent.putExtra("idnum", idNum);
intent.putExtra("idname", idName);
intent.putExtra("timeout", timeOut);
cordova.startActivityForResult(this, intent, 800);
}
private void initFace() {
new Thread(new Runnable() {
@Override
public void run() {
int ret = FaceApi.getInstance().init(cordova.getActivity().getApplicationContext());
}
}).start();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 800:
if (resultCode == Activity.RESULT_OK) {
String isLive = data.getStringExtra("isLiveness");
byte headImage[] = data.getByteArrayExtra("headImage");
String imageStr = null;
if(headImage != null && headImage.length > 0){
imageStr = Base64.encodeToString(headImage, Base64.NO_WRAP);
}
if ("1".equals(isLive)) {
//检测成功
mCallbackContext.success(getResult(1,"检测成功",imageStr));
} else {
//检测失败
mCallbackContext.success(getResult(0,"检测失败"));
}
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private String getResult(int code, String message) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("code", code);
jsonObject.put("message", message);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject.toString();
}
private String getResult(int code, String message,String image) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("code", code);
jsonObject.put("message", message);
jsonObject.put("image", image);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject.toString();
}
private void checkCameraPermission(){
if(cordova.hasPermission(Manifest.permission.CAMERA)){
startIdCardCheck();
}else{
requestCameraPermission();
}
}
private static final int REQUEST_CAMERA_PERMISSION_CODE = 0x01;
private void requestCameraPermission(){
cordova.requestPermission(this,REQUEST_CAMERA_PERMISSION_CODE,Manifest.permission.CAMERA);
}
@Override
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
if(REQUEST_CAMERA_PERMISSION_CODE == requestCode){
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
startIdCardCheck();
}else{
mCallbackContext.success(getResult(0,"未授予相机权限"));
}
}
super.onRequestPermissionResult(requestCode, permissions, grantResults);
}
}
repositories{
jcenter()
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name:"qsign_facesdk_xg_dev_1.0.0",ext: 'aar')
implementation 'com.lzy.net:okgo:3.0.4'
}
android {
packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
}
\ No newline at end of file
var exec = require('cordova/exec');
exports.idCardIdentify = function (arg0, success, error) {
exec(success, error, 'IdCardIdentifyPlugin', 'IdCardIdentify', [arg0]);
};
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment