Bootstrap

中软高科鸿蒙Next身份证读卡SDK集成说明

导入har

项目中导入提供的开发包 readcardlibrary.har

鸿蒙next的NFC使用配置,请参考官方文档。

需要在EntryAbility 中添加NFC tag相关的监听。

在项目的 module.json5 文件中需要配置NFC的权限

"requestPermissions": [
      {
        "name": "ohos.permission.NFC_TAG",
        "reason": "$string:app_name"
      },
      {
        "name": "ohos.permission.NFC",
        "reason": "$string:app_name"
      },
      {
        "name": "ohos.permission.INTERNET",
        "reason": "$string:app_name"
      },
      {
        "name": "ohos.permission.STORE_PERSISTENT_DATA",
        "reason": "$string:app_name"
      }
    ]

还需要配置NFC的tag类型

"skills": [
          {
            "entities": [
              "entity.system.home"
            ],
            "actions": [
              "action.system.home",
              // add the nfc tag action
              "ohos.nfc.tag.action.TAG_FOUND"
            ],
            "uris": [
              {
                "type":"tag-tech/NfcA"
              },
              {
                "type":"tag-tech/IsoDep"
              },
              {
                "type":"tag-tech/NfcB"
              }
              // Add other technology if neccessary,
              // such as: NfcB/NfcF/NfcV/Ndef/MifareClassic/MifareUL/NdefFormatable
            ]
          }
        ]

在需要NFC读卡的界面使用SDK

// 读卡库
import { ReadCardManager, IdCardData } from 'readcardlibrary'
// NFC相关
import { tag, nfcController } from '@kit.ConnectivityKit';
// 状态码
let StatusCode = ReadCardManager.getInstance().StatusCodeEnum()

设置读卡参数

// 设置参数
ReadCardManager.getInstance().readSetting({
            decodeImageType: "dn1", // dn0: 无照片   dn1: 平台解码照片
            readCardType: 2, // 2:NFC
            saveLog: false, // 是否保存日志文件
            openLocalCache: true, // 是否开启本地缓存
            ipPortArray: [{
              // 云解码服务器配置。
              address: 'yfs4.sfzydq.com',
              port: 9999,
              canUse: true
            }]
          })

// 设置APPID
// 配置读卡 appid (正式商业APPID请联系我司商务分配)
ReadCardManager.getInstance().setAppId("");

接收到NFC TAG 开始读卡

/**
   * 接收到NFC TAG,开始读卡
   * @param tagInfo
   */
  tagNfc(tagInfo: tag.TagInfo): void {
    try {
      console.log("canIUse NFC.Tag : " + canIUse("SystemCapability.Communication.NFC.Tag"));
      if (tagInfo == null || tagInfo == undefined) {
        console.log("no TagInfo to be created, ignore it.");
        return;
      }
      if (tagInfo.technology == null || tagInfo.technology == undefined) {
        console.log("technology is null");
        return;
      }
      let that = this;
      ReadCardManager.getInstance()
        .startReadCard(tagInfo, (code: number, msg: string, value: Object, cardType: number) => {
          let code_msg = "code:" + code + "\n" + "msg:" + msg;
          console.log("\n" + code_msg);
          switch (code) {
            case StatusCode.ININ_ING.code: // 初始化中
              break;
            case StatusCode.ININ_OK.code: // 初始化成功
              that.showToast('初始化成功,请刷身份证');
              break;
            case StatusCode.ININ_FAILE.code: // 初始化失败
              that.showToast('初始化失败');
              break;
            case StatusCode.FIND_CARD_START.code: // 开始寻卡
              break;
            case StatusCode.FIND_CARD_SUCCESS.code: // 寻卡成功
              break;
            case StatusCode.READCARD_START.code: // 开始解码 请勿移动卡片
              that.loadingController.open();
              break;
            case StatusCode.READCARD_SUCCESS.code: // 解码成功
              that.loadingController.close();
              switch (cardType) {
                case StatusCode.CARD_IC.code: // IC卡
                  console.log("IC卡:", value);
                  that.message = "IC卡:" + value as string;
                  break;
                case StatusCode.CARD_IDCARD.code: // 身份证
                  // 读取到的身份证信息
                  // 详细字段说明,详见文档下发的附录
                  let data = value as IdCardData;
                  let cardInfoStr = "";
                  switch (data.type) {
                    case StatusCode.CARD_IDCARD_CHINA.code:
                      cardInfoStr = '\n姓名: ' + data.name +
                        '\n性别: ' + data.sex +
                        '\n民族: ' + data.nation +
                        '\n生日: ' + data.birthday +
                        '\n住址: ' + data.address +
                        '\n证件号码: ' + data.id +
                        '\n签发机关: ' + data.issue +
                        '\n有效期限: ' + data.startDate + '-' + data.endDate;
                      that.message = "中国居民身份证:" + cardInfoStr;
                      break;
                    case StatusCode.CARD_IDCARD_GAT.code:
                      cardInfoStr = '\n姓名: ' + data.name +
                        '\n性别: ' + data.sex +
                        '\n生日: ' + data.birthday +
                        '\n住址: ' + data.address +
                        '\n证件号码: ' + data.id +
                        '\n签发机关: ' + data.issue +
                        '\n有效期限: ' + data.startDate + '-' + data.endDate;
                      that.message = "港澳台居住证:" + cardInfoStr;
                      break;
                    case StatusCode.CARD_IDCARD_FOREIGN.code:
                    case StatusCode.CARD_IDCARD_FOREIGN_OLD.code:
                      cardInfoStr = '\n中文姓名: ' + data.nameChinese +
                        '\n英文姓名: ' + data.nameEnglish +
                        '\n性别: ' + data.sex +
                        '\n生日: ' + data.birthday +
                        '\n国籍: ' + data.nationlity +
                        '\n证件号码: ' + data.id +
                        '\n有效期限: ' + data.startDate + '-' + data.endDate;
                      that.message = "外国永久居住证:" + cardInfoStr;
                      break;
                  }
                  // 当设置平台解码身份证照片时,
                  // idCardInfo.imageBase64字段,为base64编码的照片字符串,可直接用于显示
                  that.headImage = data.imageBase64;
                  break;
              }
              break;
            case StatusCode.READCARD_FAILE.code: // 解码失败
              that.headImage = "";
              that.loadingController.close();
              that.showToast('解码失败');
              that.message = "解码失败:" + msg;
              break;
          }
        })
    } catch (error) {
      console.log("tag.getTagInfo catched error: " + error);
    }
  }
}
;