当前位置:   article > 正文

身份证阅读器和社保卡读卡器Harmony鸿蒙系统ArkTS语言SDK开发包_鸿蒙社保卡读卡器

鸿蒙社保卡读卡器

项目需求,用ArkTS新一代开发语言实现了在Harmony鸿蒙系统上面兼容身份证阅读器和社保卡读卡器,调用了DonseeDeviceLib.har这个读卡库。

需要注意的是,鸿蒙系统的app扩展名为.hap,本项目编译输出的应用为:entry-default-signed.hap

下面是调用身份证阅读器读取身份证信息的接口,支持居民身份证、GAT居民居住证以及外国人永久居留身份证三种证件读取。

ArkTS语言,CSDN这里没有这个选项,选择的TypeScript。

  1. import CommonContants from '../common/CommonContants';
  2. import DonseeDevice from '@ohos/DonseeDevice/src/main/ets/model/DonseeDevice';
  3. import { IDCardInfor } from '@ohos/DonseeDevice/src/main/ets/model/IDCardInfor';
  4. /**
  5. * 广东东信智能科技有限公司
  6. * EST-100多功能智能卡读写器
  7. */
  8. @Component
  9. export struct IDCardComponent {
  10. @State tvResult: string = '';
  11. @State imgBase64: string = '';
  12. @State imageVisible: Visibility = Visibility.None;
  13. @State nType : number = 1;//0,文本信息;1,文本+照片;2,文本+照片+指纹
  14. @Provide showSelector: boolean = false // 是否展开下拉菜单
  15. @Provide modesData: any = [{id: 1,name: '文本信息'},{id: 2,name: '文本照片'},{id: 3,name: '文本照片指纹'}]
  16. @Provide modeId: number = 0 // 当前选中项id
  17. build() {
  18. Column() {
  19. Row() {
  20. Column() {
  21. Image(this.imgBase64)
  22. .visibility(this.imageVisible)
  23. .width(51)
  24. .height(63)
  25. .objectFit(ImageFit.Contain)
  26. Text(this.tvResult)
  27. .fontSize(10)
  28. .margin({ top: 2 })
  29. }
  30. .layoutWeight(1)
  31. .margin({left:10})
  32. .alignItems(HorizontalAlign.Start)
  33. Column() {
  34. Column() {
  35. Row() {
  36. Radio({ value: "文本", group: "1234" })
  37. .checked(this.nType === 0 ? true : false)
  38. .height(20)
  39. .width(20)
  40. .onClick(() => {
  41. this.nType = 0;
  42. })
  43. Text('文本')
  44. }.margin({ left: 10 })
  45. Row() {
  46. Radio({ value: "文本照片", group: "1234" })
  47. .checked(this.nType === 1 ? true : false)
  48. .height(20)
  49. .width(20)
  50. .onClick(() => {
  51. this.nType = 1;
  52. console.info("Radio onClick")
  53. })
  54. Text('文本照片')
  55. }.margin({ left: 10 })
  56. Row() {
  57. Radio({ value: "文本照片指纹", group: "1234" })
  58. .checked(this.nType === 2 ? true : false)
  59. .height(20)
  60. .width(20)
  61. .onClick(() => {
  62. this.nType = 2;
  63. console.info("Radio onClick")
  64. })
  65. Text('文本照片指纹')
  66. }.margin({ left: 10 })
  67. }.justifyContent(FlexAlign.Start)
  68. .alignItems(HorizontalAlign.Start)
  69. Button("读身份证")
  70. .fontSize($r('app.float.submit_button_font_size'))
  71. .fontWeight(CommonContants.FONT_WEIGHT)
  72. .height(30)
  73. .width(120)
  74. .onClick(() => {
  75. let idInfo:IDCardInfor = DonseeDevice.Donsee_ReadIDCard(this.nType)
  76. // HexUtil.bytesToHex()
  77. // if(obj.result>0){
  78. // console.info("version: " + obj.version)
  79. // }
  80. // let[result,base64] = this.donseeDevice.Donsee_ReadIDCard()
  81. console.info("result: " + idInfo.result)
  82. if(idInfo.result==0){
  83. // console.info("obj.base64: " + obj.base64)
  84. // console.info("imgBase64.length1: " + this.imgBase64.length)
  85. // console.log(("中文姓名:"), idInfo.name);
  86. // console.log(("英文姓名:"), idInfo.ENfullname);
  87. // console.log(("性 别:"), idInfo.sex);
  88. // console.log(("民 族:"), idInfo.people);
  89. // console.log(("出身日期:"), idInfo.birthday);
  90. // console.log(("家庭住址:"), idInfo.address);
  91. // console.log(("身份证号:"), idInfo.number);
  92. // console.log(("签发单位:"), idInfo.organs);
  93. // console.log(("开始有效期限:"), idInfo.signdate);
  94. // console.log(("结束有效期限:"), idInfo.validterm);
  95. // console.log(("证件类别:"), idInfo.certType);
  96. // console.log(("证件版本:"), idInfo.certVersion);
  97. // console.log(("通行证号:"), idInfo.passNu);
  98. // console.log(("签发数次:"), idInfo.signCount);
  99. if(idInfo.ENfullnameOther.length>0){
  100. idInfo.ENfullname += idInfo.ENfullnameOther
  101. }
  102. this.tvResult =
  103. "中文姓名:"+ idInfo.name+" "
  104. +"英文姓名:"+ idInfo.ENfullname+"\n"
  105. +"性 别:"+ idInfo.sex+" "
  106. +"民 族:"+ idInfo.people+" "
  107. +"出生日期:"+ idInfo.birthday+"\n"
  108. +"家庭住址:"+ idInfo.address+"\n"
  109. +"身份证号:"+ idInfo.number+"\n"
  110. +"签发单位:"+ idInfo.organs+" "
  111. +"国籍代码:"+ idInfo.nationality+"\n"
  112. +"有效期限:"+ idInfo.signdate+" - "+ idInfo.validterm+"\n"
  113. +"证件类别:"+ idInfo.certType+" "
  114. +"证件版本:"+ idInfo.certVersion+"\n"
  115. +"通行证号:"+ idInfo.passNu+" "
  116. +"换证次数:"+ idInfo.changCount+"\n"
  117. +"签发数次:"+ idInfo.signCount+" "
  118. +"既往版本:"+ idInfo.oldNumber+"\n"
  119. +"指纹:"+ idInfo.figData+"\n"
  120. if(idInfo.imgBase64.length>0){
  121. this.imgBase64 = 'data:image/png;base64,'+idInfo.imgBase64
  122. this.imageVisible = Visibility.Visible
  123. }else{
  124. this.imageVisible = Visibility.None
  125. }
  126. }else{
  127. this.imgBase64 = ''
  128. this.tvResult = "读取失败:"+ idInfo.result
  129. }
  130. }).margin({top:10})
  131. Button("身份证ID")
  132. .fontSize($r('app.float.submit_button_font_size'))
  133. .fontWeight(CommonContants.FONT_WEIGHT)
  134. .height(30)
  135. .width(120)
  136. .onClick(() => {
  137. let[result,data] = DonseeDevice.Donsee_ReadIDCardUid()
  138. if(result == 0){
  139. this.tvResult = "Uid:"+ data
  140. }else{
  141. this.tvResult = "Uid读取失败:"+ result
  142. }
  143. }).margin({top:10})
  144. }.justifyContent(FlexAlign.End)
  145. .margin({right:10})
  146. }.justifyContent(FlexAlign.Start)
  147. .width(CommonContants.FULL_PARENT)
  148. }.justifyContent(FlexAlign.Start)
  149. .backgroundColor($r('app.color.card_background'))
  150. .width(CommonContants.FULL_PARENT)
  151. .height(CommonContants.FULL_PARENT)
  152. }
  153. // 获取选中项的内容
  154. getSelectedText() {
  155. const selectedItem = this.modesData.find(item => {
  156. console.info('this.modeId==='+this.modeId)
  157. console.info('item.id==='+item.id)
  158. return item.id == this.modeId
  159. })
  160. if (selectedItem) {
  161. console.info('selectedItem.name==='+selectedItem.name)
  162. return selectedItem.name
  163. }
  164. return ''
  165. }
  166. }

以下是社保卡读卡器读取社保卡的代码,支持二代社保卡和三代社保卡读卡。

 

ArkTS语言,CSDN这里没有这个选项,选择的TypeScript。

  1. import CommonContants from '../common/CommonContants';
  2. import DonseeDevice from '@ohos/DonseeDevice/src/main/ets/model/DonseeDevice';
  3. import { IDCardInfor } from '@ohos/DonseeDevice/src/main/ets/model/IDCardInfor';
  4. import { SSCardInfor } from '@ohos/DonseeDevice/src/main/ets/model/SSCardInfor';
  5. import util from '@ohos.util';
  6. /**
  7. * 广东东信智能科技有限公司
  8. * EST-100多功能智能卡读写器
  9. */
  10. @Component
  11. export struct SSCardComponent {
  12. @State tvResult: string = '';
  13. @State pKkey: string = '01020304050607080102030405060708||';
  14. @State slot : number = 0;//卡座
  15. private textInputController: TextInputController = new TextInputController();
  16. build() {
  17. Column() {
  18. Row() {
  19. Column() {
  20. Row() {
  21. Text("pkey:")
  22. .fontSize(18)
  23. TextInput({ controller: this.textInputController,text:'01020304050607080102030405060708||' })
  24. .type(InputType.Normal)
  25. .height(32)
  26. .margin({ left: $r('app.float.text_input_margin_left') })
  27. .layoutWeight(CommonContants.TEXTINPUT_LAYOUT_WEIGHT)
  28. .onChange(value => {
  29. this.pKkey = value
  30. })
  31. }
  32. Text(this.tvResult)
  33. .fontSize(18)
  34. Text("").layoutWeight(1)
  35. }
  36. .layoutWeight(1)
  37. .margin({left:10})
  38. .alignItems(HorizontalAlign.Start)
  39. Column() {
  40. Column() {
  41. Row() {
  42. Text('SAM卡座选择:')
  43. }.margin({ left: 10 })
  44. Row() {
  45. Radio({ value: "SCard", group: "3" })
  46. .checked(this.slot === 1 ? true : false)
  47. .height(20)
  48. .width(20)
  49. .onClick(() => {
  50. this.slot = 1;
  51. console.info("Radio onClick")
  52. })
  53. Text('SAM1')
  54. Radio({ value: "SCard", group: "3" })
  55. .checked(this.slot === 2 ? true : false)
  56. .height(20)
  57. .width(20)
  58. .onClick(() => {
  59. this.slot = 2;
  60. console.info("Radio onClick")
  61. }).margin({ left: 10 })
  62. Text('SAM2')
  63. }.margin({ left: 10 })
  64. Row() {
  65. Radio({ value: "SCard", group: "3" })
  66. .checked(this.slot === 3 ? true : false)
  67. .height(20)
  68. .width(20)
  69. .onClick(() => {
  70. this.slot = 3;
  71. console.info("Radio onClick")
  72. })
  73. Text('SAM3')
  74. Radio({ value: "SCard", group: "3" })
  75. .checked(this.slot === 4 ? true : false)
  76. .height(20)
  77. .width(20)
  78. .onClick(() => {
  79. this.slot = 4;
  80. console.info("Radio onClick")
  81. }).margin({ left: 10 })
  82. Text('SAM4')
  83. }.margin({ left: 10 })
  84. Row() {
  85. Radio({ value: "SCard", group: "3" })
  86. .checked(this.slot === 0 ? true : false)
  87. .height(20)
  88. .width(20)
  89. .onClick(() => {
  90. this.slot = 0;
  91. console.info("Radio onClick")
  92. }).margin({ left: 15 })
  93. Text('无').margin({ left: 10 })
  94. }
  95. }.justifyContent(FlexAlign.Start)
  96. .alignItems(HorizontalAlign.Start)
  97. Row() {
  98. Button("读取社保卡")
  99. .fontSize($r('app.float.submit_button_font_size'))
  100. .fontWeight(CommonContants.FONT_WEIGHT)
  101. .height(20)
  102. .width(95)
  103. .onClick(() => {
  104. //nType 1, 有SAM卡返回全部信息 2, 无SAM卡返回卡号
  105. var nType = 1
  106. if (this.slot == 0) {
  107. nType = 2
  108. }
  109. console.info("nType = " + nType)
  110. console.info("this.slot = " + this.slot)
  111. let ssCardInfor: SSCardInfor = DonseeDevice.Donsee_ReadSSCardPre(this.slot, nType)
  112. console.info("ssCardInfor.result = " + ssCardInfor.result)
  113. if (ssCardInfor.result == 0) {
  114. // const str = new util.TextDecoder('gbk').decode(ssCardInfor.name)
  115. // let gbkDecoder = new util.TextDecoder("gbk",{ignoreBOM: true});
  116. console.info("ssCardInfor.name = " + ssCardInfor.nation)
  117. console.info("ssCardInfor.name = " + ssCardInfor.name)
  118. // this.tvResult = "姓 名:"+ ssCardInfor.name
  119. this.tvResult = "姓 名:" + ssCardInfor.name + "\n"
  120. + "性 别:" + ssCardInfor.sex + "\n"
  121. + "民 族:" + ssCardInfor.nation + "\n"
  122. + "出身日期:" + ssCardInfor.birthday + "\n"
  123. + "城市代码:" + ssCardInfor.city + "\n"
  124. + "身份证号:" + ssCardInfor.idnumber + "\n"
  125. + "社保卡号:" + ssCardInfor.cardnumber + "\n"
  126. + "开始有效期限:" + ssCardInfor.signdate + "\n"
  127. + "结束有效期限:" + ssCardInfor.validterm + "\n"
  128. + "社保版本:" + ssCardInfor.cardveVrsion + "\n"
  129. } else {
  130. this.tvResult = "读卡失败:" + ssCardInfor.result
  131. }
  132. })
  133. Button("读基本信息")
  134. .fontSize($r('app.float.submit_button_font_size'))
  135. .fontWeight(CommonContants.FONT_WEIGHT)
  136. .height(20)
  137. .width(95)
  138. .onClick(() => {
  139. let [result, data] = DonseeDevice.Donsee_iReadCardBas(3)
  140. if (result == 0) {
  141. this.tvResult = "读基本信息成功:" + data
  142. } else {
  143. this.tvResult = "读基本信息失败:" + result
  144. }
  145. })
  146. }.margin({ top: 10 })
  147. Row() {
  148. Button("加密步骤1")
  149. .fontSize($r('app.float.submit_button_font_size'))
  150. .fontWeight(CommonContants.FONT_WEIGHT)
  151. .height(20)
  152. .width(95)
  153. .onClick(() => {
  154. let [result, data] = DonseeDevice.Donsee_iReadCardBas_HSM_Step1(3)
  155. if (result == 0) {
  156. this.tvResult = "读卡成功:" + data
  157. } else {
  158. this.tvResult = "读卡失败:" + result
  159. }
  160. })
  161. Button("加密步骤2")
  162. .fontSize($r('app.float.submit_button_font_size'))
  163. .fontWeight(CommonContants.FONT_WEIGHT)
  164. .height(20)
  165. .width(95)
  166. .onClick(() => {
  167. let [result, data] = DonseeDevice.Donsee_iReadCardBas_HSM_Step2(this.pKkey)
  168. if (result == 0) {
  169. this.tvResult = "读卡成功:" + data
  170. } else {
  171. this.tvResult = "读卡失败:" + result
  172. }
  173. })
  174. } .margin({ top: 10 })
  175. }.justifyContent(FlexAlign.End)
  176. .margin({right:10})
  177. }.justifyContent(FlexAlign.Start)
  178. .width(CommonContants.FULL_PARENT)
  179. }.justifyContent(FlexAlign.Start)
  180. .backgroundColor($r('app.color.card_background'))
  181. .width(CommonContants.FULL_PARENT)
  182. .height(CommonContants.FULL_PARENT)
  183. }
  184. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/134879
推荐阅读
相关标签
  

闽ICP备14008679号