HarmonyOS PixelMap保存到文件时报错PackedSize outside size

代码如下,最后一步packToFile会报错 ,请问如何解决

async saveBase64Pixel(){
  let name = 'test.jpg'
  let base64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANwAAAAoCAIAAAAaOwPZAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAQuSURBVHhe7ZptmoMgDIR7rh6o5+lpvEwP01XUGshAokgX+8z+7PKRTF6SoN7e/KMCnSlw68wemkMF3oSSEHSnAKHsLiQ0iFCSge4UIJTdhYQGEUoy0J0ChLK7kNAgQkkGulOAUHYXEhpEKMlAdwpcG8rhcRv/HkN3stIgW4F88DYoX89nObjmANuOc0eMXpHHcyX9+mowhgHKmdlChM0BZzvzet6DSSW7xjEWk8Hu+/O1x7zF1237/Uu4t/O46V6sZuARoZb9KqbO7On4rJlykqcYYnNAjSbx3Gmrj6WTzxirVlA+90F82G+nm4fX3zOxgqyKqRaUU7b8FpRDOeyjJa7k5oByT1yWse4mxfDC3NrrprnQtQeUMuUXoURmCGHdKfl/oTS8MElxu2mudO0BXUCZL8efVGU0EmsQjkGpM2H8y/CwGtW1C3el8ywxhHKWxgOlaPNj0VcRRW+OoiKvCXF0o6YeXWLQDaNQyMf1Clhsi22D9HUNXOBCVZamaBmiO5BxRdRQOt3M3oFUAD4/HDolSChx7AvXzRIJQtgsUfMu6HB+HglNLc5d5KiwpcAqTH7Idk/lvLD9Z0rUx4vYWL2UJ4WY6XbdL91ML57+EjsRNEMnw/LCrKklN9NNkbuLvKsdabjM/ZMByh+PDWuuw6kDEYXPzeSfzGARlNG1M1ENRCfGLlUuJ5MVTg+UyxGzC+1+KN/DkDyuTSVbqo7vNnagfKPTrH9b8pQtgQ/PRCifDTaUJaIWw8adUycklLrcppkyCZfkJ5cYlSZnQTkmsYf58OYAlMpg6JnlhYlC9uxhIdWvbr1NS8Ahc9pgQlkkai3fOorVUK4JGeYTJIgVTm+mnCqrmSfOgDJ0mOlOlhcmClk3M0KmPzeF0mnDGVB6LjqbmKB8p5GRQ34DStRCdpEpp5MRNWRNocwsjk9i7nyqugzPYTWUSZuqe0qVucAT5tgH9ITmxEdCdihjpcCVAgfI8uJ4pgx3K3UhgBeRQ9dtbJmjp1TnYmsKoSH1UGqKE23mxlrsri4yKsuAFnZ5BrAugypw0/IdSvHmxHJbEI6lREzj0asuOc7TR8BONdd9pNKCo4LRNY9CdgCEXjqObDhQvsFpy7z7DsqHP9khxp9DzNeKbSR+Iy3/n31tqVFYe17xFUZkTu507+4px4USFwBRm32lbzFyXphgRMtn3cwqqaef8a0UrMHlaJYM8RC1Iq2DeOXvKUdVjALmzromST8+4N+Egm9rrwzl/DpAVlddnE9su36Jyx6ECtkUxufaUMJOzfwQsxldUbnTLyO/ckCcNsS112yDmkkGF/4xKL8rHndrowChbKMrV61QgFBWiMepbRQglG105aoVChDKCvE4tY0ChLKNrly1QgFCWSEep7ZRgFC20ZWrVihAKCvE49Q2ChDKNrpy1QoF/gDXIhmWmc+CSAAAAABJRU5ErkJggg=='
  let sourceTmpPath = getContext().cacheDir+'/'+name;
  let file = await fs.open(sourceTmpPath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  const reg = new RegExp('data:image/\\w+;base64,');
  const base64Str = base64.replace(reg, '');
  let base64Helper = new util.Base64Helper();
  let result = base64Helper.decodeSync(base64Str);
  let arrayBuffer = result.buffer as ArrayBuffer;
  let imageSource = image.createImageSource(arrayBuffer);
  let opts: image.DecodingOptions = { editable: false }
  let pixelMap = await imageSource.createPixelMap(opts);
  const imagePacker: image.ImagePacker = image.createImagePacker();
  await imagePacker.packToFile(pixelMap, file.fd,{ format: 'image/jpg', quality: 100 }).finally(() => {
    imagePacker.release(); //释放
  });
}
HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

62980115 输入的参数无效

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/errorcode-image-V5#section62980115-图片无效参数

请参考示例如下:

import { BusinessError } from '@ohos.base'
import { image } from '@kit.ImageKit';
import { picker } from '@kit.CoreFileKit';
import fs from '@ohos.file.fs';
import { util } from '@kit.ArkTS';

@Entry
@Component
struct Demo {
  @State imagePackerApi: image.ImagePacker = image.createImagePacker();
  @State imageSourceApi: image.ImageSource = image.createImageSource(0);

  async saveBase64Pixel() {
    let name = 'test.jpg'
    let base64 ='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr'
    let sourceTmpPath = getContext().cacheDir + '/' + name;
    console.log(`sourceTmpPath+${sourceTmpPath}`);
    let file = await fs.open(sourceTmpPath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
    const reg = new RegExp('data:image/\\w+;base64,');
    const base64Str = base64.replace(reg, '');
    let base64Helper = new util.Base64Helper();
    let result = base64Helper.decodeSync(base64Str);
    let arrayBuffer = result.buffer as ArrayBuffer;
    let imageSource = image.createImageSource(arrayBuffer);
    let opts: image.DecodingOptions = { editable: false }
    let pixelMap = await imageSource.createPixelMap(opts);
    const imagePacker: image.ImagePacker = image.createImagePacker();
    await imagePacker.packToFile(pixelMap, file.fd, { format: 'image/jpg', quality: 100 }, (err: BusinessError) => {
      if (err) {
        console.error(`Failed to pack the image to file.code ${err.code},message is ${err.message}`);
      } else {
        imagePacker.release(); //释放
        console.info('Succeeded in packing the image to file.');
      }
    })
  }


  build() {
    Column() {
      Button('保存').onClick(() => {
        this.saveBase64Pixel()
      })
    }
  }
}


图片格式换成jpeg,demo如下:
import { BusinessError } from '@ohos.base'
import { image } from '@kit.ImageKit';
import { picker } from '@kit.CoreFileKit';
import fs from '@ohos.file.fs';
import { util } from '@kit.ArkTS';

@Entry
@Component
struct Demo {
  @State imagePackerApi: image.ImagePacker = image.createImagePacker();
  @State imageSourceApi: image.ImageSource = image.createImageSource(0);

  async saveBase64Pixel() {
    let name = 'test.jpg'
    let base64 =      'data:image/png;base64,iVBORw0KGgoAAAANSUhE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AgfI8uJ4pgx3K3UhgBeRQ9dtbJmjp1TnYmsKoSH1UGqKE23mxlrsri4yKsuAFnZ5BrAugypw0/IdSvHmxHJbEI6lREzj0asuOc7TR8BONdd9pNKCo4LRNY9CdgCEXjqObDhQvsFpy7z7DsqHP9khxp9DzNeKbSR+Iy3/n31tqVFYe17xFUZkTu507+4px4USFwBRm32lbzFyXphgRMtn3cwqqaef8a0UrMHlaJYM8RC1Iq2DeOXvKUdVjALmzromST8+4N+Egm9rrwzl/DpAVlddnE9su36Jyx6ECtkUxufaUMJOzfwQsxldUbnTLyO/ckCcNsS112yDmkkGF/4xKL8rHndrowChbKMrV61QgFBWiMepbRQglG105aoVChDKCvE4tY0ChLKNrly1QgFCWSEep7ZRgFC20ZWrVihAKCvE49Q2ChDKNrpy1QoF/gDXIhmWmc+CSAAAAABJRU5ErkJggg=='
    let sourceTmpPath = getContext().cacheDir + '/' + name;
    console.log(`sourceTmpPath+${sourceTmpPath}`);
    let file = await fs.open(sourceTmpPath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
    const reg = new RegExp('data:image/\\w+;base64,');
    const base64Str = base64.replace(reg, '');
    let base64Helper = new util.Base64Helper();
    let result = base64Helper.decodeSync(base64Str);
    let arrayBuffer = result.buffer as ArrayBuffer;
    let imageSource = image.createImageSource(arrayBuffer);
    let opts: image.DecodingOptions = { editable: false }
    let pixelMap = await imageSource.createPixelMap(opts);
    const imagePacker: image.ImagePacker = image.createImagePacker();
    imagePacker.packToFile(pixelMap, file.fd, { format: 'image/jpeg', quality: 100 }, (err: BusinessError) => {
      if (err) {
        console.error(`Failed to pack the image to file.code ${err.code},message is ${err.message}`);
      } else {
        imagePacker.release(); //释放
        console.info('Succeeded in packing the image to file.');
      }
    })
  }

  build() {
    Column() {
      Button('保存').onClick(() => {
        this.saveBase64Pixel()
      })
    }
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS如何将PixelMap保存到相册?
613浏览 • 1回复 待解决
HarmonyOS 图片保存到相册报错
62浏览 • 1回复 待解决
HarmonyOS 保存到相册报错13900012
60浏览 • 1回复 待解决
PixelMap怎么保存成图片文件
446浏览 • 1回复 待解决
HarmonyOS 下载文件保存到指定目录
96浏览 • 1回复 待解决
如何将Pixmap保存到本地文件
509浏览 • 1回复 待解决
使用ohpm发布tgz文件时报错
824浏览 • 1回复 待解决
HarmonyOS java.util.List.size() 报错
226浏览 • 1回复 待解决
应用内组件截图并保存到用户文件
1146浏览 • 1回复 待解决
HarmonyOS PhotoViewPicker 保存到图库
687浏览 • 1回复 待解决
如何将像素点保存到图片文件
2345浏览 • 1回复 待解决
HarmonyOS 运行HmosWorld时报错
242浏览 • 1回复 待解决
HarmonyOS 应用发布时报错
33浏览 • 1回复 待解决
在配置文件中配置权限时报错如下
1381浏览 • 1回复 待解决
HarmonyOS 加载激励视频时报错
328浏览 • 1回复 待解决