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

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

Kotlin 复制代码
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里面引用下面包和类名

Kotlin 复制代码
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调用身份证读卡库

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

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

Kotlin 复制代码
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
    }
}
Kotlin 复制代码
//身份证
//成功返回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)
    }

}
Kotlin 复制代码
//读社保卡
//成功返回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)
    }
}
相关推荐
练习时长一年6 小时前
Jdk反射优化
java·开发语言
Turnsole_y6 小时前
pytest与Selenium结合使用指南
开发语言·python
介一安全6 小时前
【Frida Android】基础篇15(完):Frida-Trace 基础应用——JNI 函数 Hook
android·网络安全·ida·逆向·frida
吞掉星星的鲸鱼6 小时前
android studio创建使用开发打包教程
android·ide·android studio
陈老师还在写代码6 小时前
android studio 签名打包教程
android·ide·android studio
csj506 小时前
android studio设置
android
hifhf6 小时前
Android Studio gradle下载失败报错
android·ide·android studio
陈老师还在写代码6 小时前
android studio,java 语言。新建了项目,在哪儿设置 app 的名字和 logo。
android·java·android studio
消失的旧时光-19436 小时前
Kotlin reified泛型 和 Java 泛型 区别
java·kotlin·数据
郝学胜-神的一滴6 小时前
深入解析C++命令模式:设计原理与实际应用
开发语言·c++·程序人生·软件工程·命令模式