安卓使用Kotlin调用身份证阅读器SDK读取身份证、社保卡信息

步骤一:在app/build.gradle.kts下面添加东信身份证阅读器的读卡库

dependencies {
    implementation(files("libs/DonseeDevice.aar"))
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)

}

步骤二:在MainActivity.kt里面引用下面包和类名

package com.example.donseekotlintest
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.ComponentActivity
import com.donsee.bean.IDCardInfo
import com.donsee.bean.SciCardInfo
import com.donsee.devices.CardReader
import com.donsee.devices.CardReaderException

步骤三:在MainActivity.kt的onCreate里面实例化cardReader调用身份证读卡库

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    cardReader = CardReader(this@MainActivity)
    setContentView(R.layout.layout)
}

步骤四:可以开始进行读卡操作了

private var result: Long = -100
private var cardReader: CardReader? = null

//读卡读卡器端口
//成功返回0,非0失败
//支持广东东信智能科技有限公司www.eastcoms.com全系列免驱身份证读卡器产品

fun openreader(v: View?) {
    val textView = findViewById<TextView>(R.id.msgText)
    try {
        result=cardReader!!.Donsee_Open("USB")
        if (result.toInt() ==0){
            cardReader!!.Donsee_Beep(50)
            textView.setText("打开读卡器成功,返回值:$result")
        }
        else{
            //Toast.makeText(this, "打开读卡器失败,返回值:$result", Toast.LENGTH_SHORT).show()
            textView.setText("打开读卡器失败,返回值:$result")
        }
    } catch (e: CardReaderException) {
        throw e
    }
}
//身份证
//成功返回0,非0失败
//广东东信智能科技有限公司EST-100身份证阅读器

fun readidcard(v: View?) {
    val textView = findViewById<TextView>(R.id.msgText)
    val idCardInfo = IDCardInfo()
    result = cardReader!!.Donsee_ReadIDCard(1, idCardInfo)
    if (result.toInt() == 0) {
        try { //这个try是给性能没那么好的cpu预留的  没啥特殊需求就留着
            Thread.sleep(100)
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
        cardReader!!.Donsee_Beep(50)
        if (idCardInfo.certType == "0") {
            idCardInfo.certType = "居民身份证"
        } else if (idCardInfo.certType == "I") {
            idCardInfo.certType = "外国人永久居留证"
        } else if (idCardInfo.certType == "J") {
            idCardInfo.certType = "港澳台居民居住证"
        } else if (idCardInfo.certType == "Y") {
            idCardInfo.certType = "新版外国人永居证"
        }
        if (TextUtils.isEmpty(idCardInfo.nation)) {  //外国人身份证没有名族,防止出现null
            idCardInfo.nation = ""
        }
        if (TextUtils.isEmpty(idCardInfo.address)) { //外国人身份证没有地址,防止出现null
            idCardInfo.address = ""
        }
        if (TextUtils.isEmpty(idCardInfo.name)) {
            idCardInfo.name = "无中文姓名"
        }
        val infor = """
            姓名: ${idCardInfo.name}
            英文姓名: ${idCardInfo.enFullName}${idCardInfo.reserveName}
            性别: ${idCardInfo.sex}
            民族: ${idCardInfo.nation}
            住址: ${idCardInfo.address}
            出生日期: ${idCardInfo.birthDate}
            发证日期: ${idCardInfo.issueDate}
            有效日期: ${idCardInfo.expireDate}
            证件号码: ${idCardInfo.idNO}
            签发机关: ${idCardInfo.organs}
            证件类型: ${idCardInfo.certType}
            
            """.trimIndent()
        textView.text = "读身份证成功:\n$infor"
        val photoImage = findViewById<ImageView>(R.id.photo_image)
        val image = cardReader!!.getBMPByWlt(idCardInfo.photo)
        if (image != null) {
            photoImage.setImageBitmap(image)
        }
    } else {
        textView.text = "读身份证失败,返回值:" + CardReader.getErrorMessage(result)
    }

}

//读社保卡
//成功返回0,非0失败

fun readsscard(view: View?) {
    val textView = findViewById<TextView>(R.id.msgText)
    val sciCardInfo = SciCardInfo()
    //nType 1:有SAM卡返回全部信息,2:无SAM卡返回卡号
    val errInfor = ByteArray(256)
    result = cardReader!!.Donsee_ReadSSCard(0x11, 1, sciCardInfo, errInfor)
    if (result.toInt() == 0) {
        try { //这个try是给性能没那么好的cpu预留的  没啥特殊需求就留着
            Thread.sleep(100)
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
        cardReader!!.Donsee_Beep(50)
        textView.text = "读社保卡成功:\n$sciCardInfo"
    } else {
        textView.text = "读社保卡错误,code:" + result + " errInfor: " + String(errInfor)
    }
}


http://www.niftyadmin.cn/n/5558102.html

相关文章

django-vue-admin项目运行

文本主要对django-vue-admin项目进行了简要介绍&#xff0c;并且对前后端进行了源码安装和运行。在此基础上可作为管理系统二次开发的基础框架。 一.django-vue-admin简介和安装 1.简介 django-vue-admin项目是基于RBAC模型权限控制的中小型应用的基础开发平台&#xff0c;采…

Accelerated PyTorch training on Mac

本文翻译整理自&#xff1a;Accelerated PyTorch training on Mac https://developer.apple.com/metal/pytorch/ 文章目录 Metal 加速要求开始1.Set upAnacondapip 2.安装Anacondapip从源代码构建 3.验证 反馈意见资源 Metal 加速 PyTorch使用新的 Metal Performance Shaders&…

2024华为数通HCIP-datacom最新题库(变题更新⑥)

请注意&#xff0c;华为HCIP-Datacom考试831已变题 请注意&#xff0c;华为HCIP-Datacom考试831已变题 请注意&#xff0c;华为HCIP-Datacom考试831已变题 近期打算考HCIP的朋友注意了&#xff0c;如果你准备去考试&#xff0c;还是用的之前的题库&#xff0c;切记暂缓。 1、…

JVM常见知识点总结

最近在看关于JVM的一些知识点&#xff0c;之前也有了解过&#xff0c;可是时间一长就总会忘&#xff0c;索性按照自己的记忆方式去回顾一下知识点。 首先&#xff0c;jvm是Java程序运行的环境&#xff0c;全称为Java Virtual Machine 当我们写好的Java文件经过javac编译后就变成…

持续集成04--Jenkins结合Gitee创建项目

前言 在持续集成/持续部署&#xff08;CI/CD&#xff09;的旅途中&#xff0c;Jenkins与版本控制系统的紧密集成是不可或缺的一环。本篇“持续集成03--Jenkins结合Gitee创建项目”将引导如何将Jenkins与Gitee&#xff08;一个流行的Git代码托管平台&#xff09;相结合&#xff…

第一章 Typescript小白快速入门

第一章 Typescript小白快速入门 这里将会介绍到从typescript的简介到环境的部署&#xff0c;从基础类型的入门学习到ts的编译打包。 1、TypeScript简介 TypeScript是JavaScript的超集。它对JS进行了扩展&#xff0c;向JS中引入了类型的概念&#xff0c;并添加了许多新的特性…

【Linux】进程信号 --- 信号预备阶段(入门篇)

&#x1f466;个人主页&#xff1a;Weraphael ✍&#x1f3fb;作者简介&#xff1a;目前正在学习c和算法 ✈️专栏&#xff1a;Linux &#x1f40b; 希望大家多多支持&#xff0c;咱一起进步&#xff01;&#x1f601; 如果文章有啥瑕疵&#xff0c;希望大佬指点一二 如果文章对…

可视化作品集(14)智慧旅游和智慧景区,洞悉一切。

智慧旅游和智慧景区的可视化大屏可以带来以下几个方面的好处&#xff1a; 1. 提升游客体验&#xff1a; 通过可视化大屏&#xff0c;游客可以方便地获取到景区地图、交通信息、景点介绍、活动安排等信息&#xff0c;帮助游客更好地规划行程&#xff0c;提升游览体验。 2. 提供…