安卓使用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)
    }
}
相关推荐
hedalei几秒前
android13修改系统Launcher不跟随重力感应旋转
android·launcher
许苑向上41 分钟前
Java八股文(下)
java·开发语言
菜鸟一枚在这1 小时前
深入解析设计模式之单例模式
开发语言·javascript·单例模式
独孤求败Ace1 小时前
第44天:Web开发-JavaEE应用&反射机制&类加载器&利用链&成员变量&构造方法&抽象方法
java·开发语言
计算机-秋大田1 小时前
基于Spring Boot的农产品智慧物流系统设计与实现(LW+源码+讲解)
java·开发语言·spring boot·后端·spring·课程设计
matlabgoodboy1 小时前
Matlab代编电气仿真电力电子电机控制自动化新能源微电网储能能量
开发语言·matlab·自动化
镰圈量化1 小时前
当电脑上有几个python版本Vscode选择特定版本python
开发语言·vscode·python
Indoraptor1 小时前
Android Fence 同步框架
android
峥嵘life2 小时前
DeepSeek本地搭建 和 Android
android
叶羽西2 小时前
Android14 Camera框架中Jpeg流buffer大小的计算
android·安卓