安卓使用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)
    }
}
相关推荐
手心里的白日梦24 分钟前
STL中queue、stack的实现与容器适配器的讲解
开发语言·c++
Freesial_28 分钟前
C# foreach集合遍历循环语句
开发语言·c#
Jasonial42 分钟前
0.0 C语言被我遗忘的知识点
c语言·开发语言
仙陨1 小时前
C++和QT
开发语言·c++
Python大数据分析@1 小时前
Python是工程,不是艺术
开发语言·python
孺子牛 for world1 小时前
python实现循环神经网络
开发语言·python·rnn
胡净1 小时前
Java-I/O
java·开发语言
姝孟1 小时前
C语言——字符函数、字符串函数和内存函数
c语言·开发语言
oscar9992 小时前
Python测试框架之—— pytest介绍与示例
开发语言·python·pytest
北京最后的深情2 小时前
Java框架Spring(一)
java·开发语言·spring