文章目录
卡证识别控件提供身份证(目前仅支持中国大陆二代身份证,且不包含民汉双文身份证)、行驶证、驾驶证、护照、银行卡等证件的结构化识别服务,满足卡证的自动分类功能,系统可自动判断所属卡证类型并返回结构化信息和卡证图片信息。
对于需要填充卡证信息的场景,如身份证、银行卡信息等,可使用卡证识别控件读取OCR(Optical Character Recognition)信息,将结果信息返回后进行填充。支持单独识别正面、反面,或同时进行双面识别。
注意
使用该控件会创建弹窗,并以全模态形式展示。因此,该控件被拉起或退出时均会触发接入页面的生命周期变化,拉起时会触发页面的onPageHide,退出时则触发页面的onPageShow。
图1 银行卡识别示意图
约束与限制
该能力当前不支持模拟器。
开发步骤
将卡证识别控件相关的类添加至工程。
//其中CardRecognitionConfig,CardContentConfig,BankCardConfig从API12开始支持
import { CardRecognition, CallbackParam, CardType, CardSide, CardRecognitionConfig, ShootingMode, CardContentConfig, BankCardConfig } from "@kit.VisionKit";
- 配置页面的布局,选择需要识别的卡证类型和需要识别的卡证页面,配置对应设置项,在回调中获取结果返回值。
以下分别为身份证、银行卡、护照、驾驶证和行驶证的示例代码。
import { hilog } from '@kit.PerformanceAnalysisKit';
const TAG = 'CardRecognition'
@Entry
@Component
struct Index {
build() {
Column() {
//身份证
CardRecognition({
supportType: CardType.CARD_ID,
// 身份证可双面识别
cardSide: CardSide.DEFAULT,
cardRecognitionConfig: {
defaultShootingMode: ShootingMode.MANUAL,
isPhotoSelectionSupported: true
},
callback: ((params: CallbackParam) => {
hilog.info(0x0001, TAG, `params code: ${params.code}`)
hilog.info(0x0001, TAG, `params cardType: ${params.cardType}`)
hilog.info(0x0001, TAG, `params cardInfo front: ${JSON.stringify(params.cardInfo?.front)}`)
hilog.info(0x0001, TAG, `params cardInfo back: ${JSON.stringify(params.cardInfo?.back)}`)
})
})
}
.height('100%')
.width('100%')
}
}
import { hilog } from '@kit.PerformanceAnalysisKit';
const TAG = 'CardRecognition'
@Entry
@Component
struct Index {
build() {
Column() {
// 银行卡
CardRecognition({
supportType: CardType.CARD_BANK,
// 银行卡为单面识别
cardSide: CardSide.FRONT,
cardRecognitionConfig: {
defaultShootingMode: ShootingMode.MANUAL,
isPhotoSelectionSupported: true,
cardContentConfig: { bankCard: { isBankNumberDialogShown: true} }
},
callback: ((params: CallbackParam) => {
hilog.info(0x0001, TAG, `params code: ${params.code}`)
hilog.info(0x0001, TAG, `params cardType: ${params.cardType}`)
hilog.info(0x0001, TAG, `params cardInfo: ${JSON.stringify(params.cardInfo?.main)}`)
})})
}
.height('100%')
.width('100%')
}
}
import { hilog } from '@kit.PerformanceAnalysisKit';
const TAG = 'CardRecognition'
@Entry
@Component
struct Index {
build() {
Column() {
// 护照
CardRecognition({
supportType: CardType.CARD_PASSPORT,
// 护照为单面识别
cardSide: CardSide.FRONT,
cardRecognitionConfig: {
defaultShootingMode: ShootingMode.MANUAL,
isPhotoSelectionSupported: true
},
callback: ((params: CallbackParam) => {
hilog.info(0x0001, TAG, `params code: ${params.code}`)
hilog.info(0x0001, TAG, `params cardType: ${params.cardType}`)
hilog.info(0x0001, TAG, `params cardInfo: ${JSON.stringify(params.cardInfo?.main)}`)
})})
}
.height('100%')
.width('100%')
}
}
import { hilog } from '@kit.PerformanceAnalysisKit';
const TAG = 'CardRecognition'
@Entry
@Component
struct Index {
build() {
Column() {
// 驾驶证
CardRecognition({
supportType: CardType.CARD_DRIVER_LICENSE,
// 驾驶证可双面识别
cardSide: CardSide.DEFAULT,
cardRecognitionConfig: {
defaultShootingMode: ShootingMode.MANUAL,
isPhotoSelectionSupported: true
},
callback: ((params: CallbackParam) => {
hilog.info(0x0001, TAG, `params code: ${params.code}`)
hilog.info(0x0001, TAG, `params cardType: ${params.cardType}`)
hilog.info(0x0001, TAG, `params cardInfo front: ${JSON.stringify(params.cardInfo?.front)}`)
hilog.info(0x0001, TAG, `params cardInfo back: ${JSON.stringify(params.cardInfo?.back)}`)
})
})
}
.height('100%')
.width('100%')
}
}
import { hilog } from '@kit.PerformanceAnalysisKit';
const TAG = 'CardRecognition'
@Entry
@Component
struct Index {
build() {
Column() {
// 行驶证
CardRecognition({
supportType: CardType.CARD_VEHICLE_LICENSE,
// 行驶证可双面识别
cardSide: CardSide.DEFAULT,
cardRecognitionConfig: {
defaultShootingMode: ShootingMode.MANUAL,
isPhotoSelectionSupported: true
},
callback: ((params: CallbackParam) => {
hilog.info(0x0001, TAG, `params code: ${params.code}`)
hilog.info(0x0001, TAG, `params cardType: ${params.cardType}`)
hilog.info(0x0001, TAG, `params cardInfo front: ${JSON.stringify(params.cardInfo?.front)}`)
hilog.info(0x0001, TAG, `params cardInfo back: ${JSON.stringify(params.cardInfo?.back)}`)
})
})
}
.height('100%')
.width('100%')
}
}
开发实例
// 卡证识别开发实例分两页实现,一页为卡证识别入口页,一页为卡证识别实现页
// 卡证识别入口页,需引入卡证识别实现页,以下文实例为例,实现页文件名为CardDemoPage
import { CardDemoPage } from './CardDemoPage'
@Entry
@Component
struct MainPage {
@Provide('pathStack') pathStack: NavPathStack = new NavPathStack()
@Builder
PageMap(name: string) {
if (name === 'cardRecognition') {
CardDemoPage()
}
}
//卡证识别入口按钮
build() {
Navigation(this.pathStack) {
Button('CardRecognition', { stateEffect: true, type: ButtonType.Capsule })
.width('50%')
.height(40)
.onClick(() => {
this.pathStack.pushPath({ name: 'cardRecognition' })
})
}.title('卡证识别控件demo').navDestination(this.PageMap)
.mode(NavigationMode.Stack)
}
}
//卡证识别实现页,文件名为CardDemoPage,需被引入至入口页
import { CardRecognition, CallbackParam, CardType, CardSide, ShootingMode } from "@kit.VisionKit"
import { hilog } from '@kit.PerformanceAnalysisKit';
const TAG: string = 'CardRecognitionPage'
//卡证识别页,用于加载uiExtensionAbility
@Entry
@Component
export struct CardDemoPage {
@State cardDataSource: Record<string, string>[] = []
@Consume('pathStack') pathStack: NavPathStack
build() {
NavDestination() {
Stack({ alignContent: Alignment.Top }) {
Stack() {
this.cardDataShowBuilder()
}
.width('80%')
.height('80%')
CardRecognition({
// 此处选择身份证类型作为示例
supportType: CardType.CARD_ID,
cardSide: CardSide.DEFAULT,
cardRecognitionConfig: {
defaultShootingMode: ShootingMode.MANUAL,
isPhotoSelectionSupported: true
},
callback: ((params: CallbackParam) => {
hilog.info(0x0001, TAG, `params code: ${params.code}`)
if (params.code === -1) {
this.pathStack.pop()
}
hilog.info(0x0001, TAG, `params cardType: ${params.cardType}`)
if (params.cardInfo?.front !== undefined) {
this.cardDataSource.push(params.cardInfo?.front)
}
if (params.cardInfo?.back !== undefined) {
this.cardDataSource.push(params.cardInfo?.back)
}
if (params.cardInfo?.main !== undefined) {
this.cardDataSource.push(params.cardInfo?.main)
}
hilog.info(0x0001, TAG, `params cardInfo front: ${JSON.stringify(params.cardInfo?.front)}`)
hilog.info(0x0001, TAG, `params cardInfo back: ${JSON.stringify(params.cardInfo?.back)}`)
})
})
}
.width('100%')
.height('100%')
}
.width('100%')
.height('100%')
.hideTitleBar(true)
}
@Builder
cardDataShowBuilder() {
List() {
ForEach(this.cardDataSource, (cardData: Record<string, string>) => {
ListItem() {
Column() {
Image(cardData.cardImageUri)
.objectFit(ImageFit.Contain)
.width(100)
.height(100)
Text(JSON.stringify(cardData))
.width('100%')
.fontSize(12)
}
}
})
}
.listDirection(Axis.Vertical)
.alignListItem(ListItemAlign.Center)
.margin({
top: 50
})
.width('100%')
.height('100%')
}
}