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

时间:2024-07-17 10:01:05

步骤一:在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)
    }
}