HarmonyOS 使用cardImageUri可以获取到文件uri但是使用opensync打开该uri无法获取到文件

证件识别模块,使用cardImageUri可以获取到文件uri但是使用opensync打开该uri无法获取到文件。

HarmonyOS
2024-11-15 09:26:18
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa
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%') 
    } 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
分享
微博
QQ
微信
回复
2024-11-15 17:05:35
相关问题
HarmonyOS rawfile中的文件如何获取URI
844浏览 • 1回复 待解决
HarmonyOS db文件无法取到数据
1320浏览 • 1回复 待解决
HarmonyOS 文件路径转uri
1086浏览 • 1回复 待解决
如何根据uri读取文件?
4138浏览 • 1回复 待解决
HarmonyOS uri获取query失败
611浏览 • 2回复 待解决