uniapp生成android,集成商汤科技的android的sdk识别身份证信息
uniapp使用Dcloudx软件的“原生打包”,然后集成到android项目中(参考了DCloudx的例子),发布成android包。
signingConfigs {
config {
keyAlias 'ecodeocrtests'// 别名
keyPassword 'admin1'// 密码
storeFile file('test.keystore')// key路径
storePassword 'admin1'// 密码
v1SigningEnabled true
v2SigningEnabled true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.facebook.fresco:fresco:2.5.0'
implementation "com.facebook.fresco:animated-gif:2.5.0"
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.alibaba:fastjson:1.1.46.android'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
//rx
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.14'
implementation 'com.drewnoakes:metadata-extractor:2.12.0'
}
defaultConfig {
// ...省略代码
ndk {
abiFilters 'x86', 'armeabi-v7a', 'arm64-v8a'
}
}
// java中发送
Map<String,Object> params=new HashMap<>();
params.put("code", "success");
params.put("msg", "识别成功");
mWXSDKInstance.fireGlobalEventCallback("portraitEvent", params);
// 页面中接收
// 身份证人像照识别,返回数据监听
plus.globalEvent.addEventListener('portraitEvent', ret => {
modal.toast({
message: ret.msg,
duration: 5
});
this.idCardInfo.mName = ret.idCardInfo.mName;
// 将接收的图片byte数组转为arrayBuffer
let arrayBuffer = new Int8Array(ret.idCardInfo.mResultFrontImageData).buffer
// 由arrayBuffer转为base64字符串
const base64 = uni.arrayBufferToBase64(arrayBuffer)
// 拼接base64字符串显示图片
this.idCardInfo.portraitImgData = 'data:image/jpeg;base64,' + base64
});
由于1中uniapp和android代码配置有问题
android包中的api,在androidx包中都有对应的实现,百度查一查,就可以完美修改
新增的页面需要在AndroidManifest.xml中声明后才能使用,不然会报错。
public class DataHelper {
private static final DataHelper helper= new DataHelper();
public static DataHelper getInstance() {return helper;}
Map<String, Object> data = new HashMap<String, Object>();
public void saveData(String id, Object object) {
data.put(id, object);
}
public Object getData(String id) {
return data.get(id);
}
public Object getDataAndRemove(String id) {
Object obj = data.get(id);
data.remove(id);
return obj;
}
}
参考:https://blog.csdn.net/u012241616/article/details/108803154
let arrayBuffer = new Int8Array(ret.idCardInfo.mResultFrontImageData).buffer
// 由arrayBuffer转为base64字符串
const base64 = uni.arrayBufferToBase64(arrayBuffer)
// 拼接base64字符串显示图片
this.idCardInfo.portraitImgData = 'data:image/jpeg;base64,' + base64