一、工具
DevEco Studio
二、項目介紹
開發(fā)步驟
將卡證識別控件相關(guān)的類添加至工程。
//其中CardRecognitionConfig,CardContentConfig,BankCardConfig從API12開始支持
import { CardRecognition, CallbackParam, CardType, CardSide, CardRecognitionConfig, ShootingMode, CardContentConfig, BankCardConfig } from "@kit.VisionKit";
配置頁面的布局,選擇需要識別的卡證類型和需要識別的卡證頁面,配置對應(yīng)設(shè)置項,在回調(diào)中獲取結(jié)果返回值。
以下分別為身份證、銀行卡、護照、駕駛證和行駛證的示例代碼。
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%')
? }
}
開發(fā)實例
// 卡證識別開發(fā)實例分兩頁實現(xiàn),一頁為卡證識別入口頁,一頁為卡證識別實現(xiàn)頁
// 卡證識別入口頁,需引入卡證識別實現(xiàn)頁,以下文實例為例,實現(xiàn)頁文件名為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)
? }
}
//卡證識別實現(xiàn)頁,文件名為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%')
? }
}