HarmonyOS 无法上传图片

做身份证强验证业务,通过VisionKit分别捕捉到身份后上传至服务器做ocr,之后再通过VisionKit获取到活体检测的图片,手动保存至本地后三张图片一起上传,但之前上传过的身份证图片无法打开,卡在fs.openSync方法,如图所示uri顺序分别为正面、反面、活体,更换uri顺序后活体图片文件可以打开,身份证正面依然无法打开。

有以下几个问题:

1、VisionKit捕捉到的身份图片的路径是怎么生成的,活体检测的图片路径需要设置成类似的吗?

2、上传图片时做了copy操作必须做copy么,是否因为上传过一次,做了copy操作导致图片地址变更造成的吗?

HarmonyOS
2025-01-09 17:11:48
763浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

参考示例:

import { CardRecognition, CallbackParam, CardType, CardSide } from "@kit.VisionKit";
import { hilog } from '@kit.PerformanceAnalysisKit';
import fs from '@ohos.file.fs';
import { image } from '@kit.ImageKit';

const TAG: string = 'CardRecognitionPage'

@Entry
@Component
struct CardRecognitionPage {
  private myFile: fs.File | undefined = undefined;
  private myFile1: fs.File | undefined = undefined;
  @State pixelMap: PixelMap | undefined = undefined;
  @State pixelMap1: PixelMap | undefined = undefined;

  build() {
    Stack({ alignContent: Alignment.Top }) {
      Column() {
        //显示人像面
        Image(this.pixelMap)
          .height(200)
          .width('100%')
        Image(this.pixelMap1)
          .height(200)
          .width('100%')
      }

      CardRecognition({
        supportType: CardType.CARD_ID,
        cardSide: CardSide.DEFAULT,
        callback: (async (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)}`)

          try { // front 是人像,back 是国徽
            this.myFile = await fs.openSync(params.cardInfo?.front?.cardImageUri, fs.OpenMode.READ_ONLY)
            this.myFile1 = await fs.openSync(params.cardInfo?.back?.cardImageUri, fs.OpenMode.READ_ONLY)
            console.log(`${params.cardInfo?.front?.cardImageUri}`)
            console.log(`${params.cardInfo?.back?.cardImageUri}`)
          } catch (e) {
            hilog.info(0x0001, TAG, `params params cardInfo back error: ${JSON.stringify(e)}`)
          }
          let context = getContext(this);
          let pathDir = context.filesDir;
          hilog.info(0x0001, TAG,
            `params params cardInfo pathDir: ${JSON.stringify(pathDir)}`)
          // 正反面两张照片都拷贝进应用沙箱
          fs.copyFileSync(this.myFile?.fd, pathDir + '/wy.jpeg');
          fs.copyFileSync(this.myFile1?.fd, pathDir + '/wy1.jpeg');
          // 判断照片拷贝是否成功
          console.log('params params copy result: ' + fs.accessSync(pathDir + '/wy.jpeg'))
          fs.close(this.myFile)
          // 取人像面转换成可显示的类型
          let imageSourceApi = image.createImageSource(pathDir + '/wy.jpeg');
          this.pixelMap = imageSourceApi.createPixelMapSync();
          let imageSourceApi1 = image.createImageSource(pathDir + '/wy1.jpeg');
          this.pixelMap1 = imageSourceApi1.createPixelMapSync();
        })
      })
    }.width('100%').height('100%')
  }
}
  • 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.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
分享
微博
QQ
微信
回复
2025-01-09 20:49:13
相关问题
HarmonyOS h5客服无法上传相册的图片
811浏览 • 1回复 待解决
HarmonyOS 图片上传
594浏览 • 1回复 待解决
HarmonyOS 图片上传失败
831浏览 • 1回复 待解决
HarmonyOS 上传图片失败
1220浏览 • 1回复 待解决
HarmonyOS 上传图片异常
721浏览 • 1回复 待解决
HarmonyOS 上传图片问题
639浏览 • 1回复 待解决
HarmonyOS web如何上传图片
538浏览 • 1回复 待解决
HarmonyOS axios如何上传图片
689浏览 • 1回复 待解决
HarmonyOS图片上传相关问题
1138浏览 • 1回复 待解决
HarmonyOS 图片上传问题咨询
719浏览 • 1回复 待解决
HarmonyOS 图片上传httpRequest格式问题
598浏览 • 1回复 待解决
HarmonyOS axios如何上传多张图片
821浏览 • 1回复 待解决
HarmonyOS如何实现图片上传
986浏览 • 1回复 待解决
HarmonyOS web上传文件和图片
903浏览 • 1回复 待解决
HarmonyOS 文件上传无法解析文件地址
919浏览 • 1回复 待解决
HarmonyOS 上传图片到服务器
725浏览 • 1回复 待解决
HarmonyOS Web 图片上传功能失效
876浏览 • 1回复 待解决
HarmonyOS 如何上传本地图片
836浏览 • 1回复 待解决
HarmonyOS 怎么对图片进行压缩上传
946浏览 • 1回复 待解决
怎么实现后台上传图片
3079浏览 • 1回复 待解决