HarmonyOS handleTakePicture不执行

通过相机拍照回调函数的savePicture拿到照片的url想通过离屏绘制在照片内添加需要的图片或者文字内容,但是在handleTakePicture中包含UI,在函数savePicture中调用handleTakePicture会不执行,这个如何修改来实现拍照照片添加自定义的文字或者图片集合

HarmonyOS
9天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

请参考demo:

SaveButton(this.saveButtonOptions)
              .onClick(async (event, result: SaveButtonOnClickResult) => {
                if (result == SaveButtonOnClickResult.SUCCESS) {
                  try {
                    let context = getContext();
                    let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
                    let uri = await phAccessHelper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg'); // 创建媒体文件
                    if (this.pixelMap) {
                      let imageInfo = await this.pixelMap.getImageInfo()
                      let offCanvas = new OffscreenCanvas(px2vp(imageInfo.size.width), px2vp(imageInfo.size.height))
                      let offContext = offCanvas.getContext("2d")
                      let contextPixelMap = this.context.getPixelMap(0, 0, this.context.width, this.context.height)
                      offContext.drawImage(contextPixelMap, 0, 0, offCanvas.width, offCanvas.height)
                      saveToFile(offContext.getPixelMap(0, 0, offCanvas.width, offCanvas.height), uri);
                    }
                  } catch (err) {
                    console.error('createAsset failed, message = ', err);
                  }
                } else {
                  console.error('SaveButtonOnClickResult createAsset failed');
                }
              })

// 保存图片
export async function saveToFile(pixelMap: image.PixelMap, imagePath: string): Promise<void> {
  try {
    const filePath = imagePath;
    const imagePacker = image.createImagePacker();
    const imageBuffer = await imagePacker.packing(pixelMap, {
      format: 'image/jpeg',
      quality: 100
    });
    const mode = fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE;
    fd = (await fs.open(filePath, mode)).fd;
    await fs.truncate(fd);
    await fs.write(fd, imageBuffer);
  } catch (err) {
  } finally {
    if (fd) {
      fs.close(fd);
    }
  }
}

可以通过下述方法,将buffer转成pixelMap,demo:

// 加载图片
onComplete(imageInfo: ArrayBuffer) {
  let imageSource: image.ImageSource = image.createImageSource(imageInfo);
  imageSource.getImageInfo((err, value) => {
    if (err) {
      return;
    }
    this.hValue = Math.round(value.size.height * 1);
    this.wValue = Math.round(value.size.width * 1);
    let defaultSize: image.Size = {
      height: this.hValue,
      width: this.wValue
    };

    let opts: image.DecodingOptions = {
      editable: true,
      desiredSize: defaultSize
    };
    imageSource.createPixelMap(opts, (err, pixelMap) => {
      if (err) {
      } else {
        let rect = Utils.getComponentRect("imageContainer")
        this.imageScale = (rect.right - rect.left) / this.wValue
        this.imageHeight = this.hValue * this.imageScale
        this.context.transform(this.imageScale, 0, 0, this.imageScale, 0, 0)
        this.pixelMap = pixelMap
        this.context.drawImage(this.pixelMap, 0, 0)
      }
    })
  })
}
分享
微博
QQ
微信
回复
9天前
相关问题
HarmonyOS Promise 重复执行执行
195浏览 • 1回复 待解决
HarmonyOS zlib.compressFile执行回调
142浏览 • 1回复 待解决
HarmonyOS onBackPress执行异常问题
692浏览 • 1回复 待解决
HarmonyOS onBackPress执行异常问题 ?
77浏览 • 0回复 待解决
HarmonyOS margin生效
740浏览 • 1回复 待解决
HarmonyOS 地图展示
95浏览 • 1回复 待解决
HarmonyOS 执行PixelMap进行保存
127浏览 • 1回复 待解决
HarmonyOS runJavaScript执行时序
354浏览 • 1回复 待解决