中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
证件识别模块,使用cardImageUri可以获取到文件uri但是使用opensync打开该uri无法获取到文件。
微信扫码分享
import { CardRecognition, CallbackParam, CardType, CardSide } from "@kit.VisionKit"; import hilog from '@ohos.hilog'; import { image } from '@kit.ImageKit'; import { BusinessError } from '@kit.BasicServicesKit'; import fs from '@ohos.file.fs'; const TAG: string = 'CardRecognitionPage'; @Entry @Component struct HwSharePage { @State IDCard: ResourceStr | undefined = "2424234" build() { Column() { Text(this.IDCard) Image(this.IDCard) .width('80%') .height('40%') .objectFit(ImageFit.Contain) .backgroundColor(0xF5F5F5) Stack({ alignContent: Alignment.Top }) { CardRecognition({ // 此处选择身份证类型作为示例 supportType: CardType.CARD_ID, cardSide: CardSide.FRONT, callback: ((params: CallbackParam) => { this.IDCard = params.cardInfo?.front.cardImageUri hilog.info(0x0001, TAG, this.IDCard) let imageSource: image.ImageSource = image.createImageSource(fs.openSync(params.cardInfo?.front.cardImageUri).fd) let decodingOptions: image.DecodingOptions = { editable: true, desiredPixelFormat: 3, } if (imageSource == undefined || imageSource == null) { console.log(TAG, "value is null") return } // 创建pixelMap imageSource.createPixelMap(decodingOptions).then((pixelMap: image.PixelMap) => { console.log(TAG, "Succeeded in creating PixelMap") pixelMap.getImageInfo().then((info: image.ImageInfo) => { console.info(TAG, 'info.width = ' + info.size.width); console.info(TAG, 'info.height = ' + info.size.height); }).catch((err: BusinessError) => { console.error(TAG, "Failed to obtain the image pixel map information.And the error is: " + err); }); }).catch((err: BusinessError) => { console.error(TAG, "Failed to create PixelMap") }); }) }) } .height('40%') } } }