HarmonyOS 在Pad上,PixelMap的writePixels接口失败,返回62980115,在手机端正常

操作步骤:

1、读取本地一张图片,缩小至40*40的

2、从自定义绘制了一些文本的OffscreenCanvas的context中,通过getPixelMap接口得到一张大图

3、调用大图的writePixels接口把小图合并至大图上

4、相同的代码,在手机上,接口正常返回并图片合并成功,但在Pad上返回错误码62980115

HarmonyOS
17h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

writepixels会做area里的参数校验。

resultImage又是从offscreencanvas创建出来的,猜测手机和平板上由于屏幕尺寸不同,导致get出来的resultImage大小不同。

而write的函数在两个设备上都是一套参数,可能是由于这个因素导致的错误。

请参考示例如下:

import { image } from '@kit.ImageKit';
import { BusinessError, settings } from '@kit.BasicServicesKit';
import picker from '@ohos.file.picker';
import fs from '@ohos.file.fs';
import fileIo from '@ohos.file.fs';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import multimedia_image from '@ohos.multimedia.image';

const TAG = "pixel";

@Component
export struct Pixel {
  @State a: number = 1
  private settings: RenderingContextSettings = new RenderingContextSettings(true);
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(200, 300)
  @State _pixelMap2: multimedia_image.PixelMap | undefined = undefined
  build() {

    Row() {
      Button('pixe').onClick(() => {
        this.fn2()
      })

      Column() {
        Image(this._pixelMap2)
          .border({ width: 1, color: Color.Blue })
          .width(500).height(500)
          .objectFit(ImageFit.None)
      }
    }
  }

  aboutToAppear(): void {
  }

  fn2() {
    let uris: Array<string> = [];
    let PhotoSelectOptions = new picker.PhotoSelectOptions();
    PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
    PhotoSelectOptions.maxSelectNumber = 1;
    let photoPicker = new picker.PhotoViewPicker();
    photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {
      uris = PhotoSelectResult.photoUris;
      let file = fileIo.openSync(uris[0], fileIo.OpenMode.READ_ONLY);
      console.info('file fd: ' + file.fd);
      let buffer = new ArrayBuffer(4096);
      let readLen = fileIo.readSync(file.fd, buffer);
      console.info('readSync data to file succeed and buffer size is:' + readLen);
      const imageSource: image.ImageSource = image.createImageSource(file.fd);
      let decodingOptions: image.DecodingOptions = {
        editable: true,
        desiredPixelFormat:image.PixelMapFormat.BGRA_8888 ,
      }
      imageSource.createPixelMap(decodingOptions, (err: BusinessError, pixelMap: image.PixelMap) => {
        if (err !== undefined) {
          console.error(`Failed to create pixelMap.code is ${err.code},message is ${err.message}`);
        } else {
          console.info('Succeeded in creating pixelMap object.');

          let pixel = pixelMap
          console.log('_29')
          pixel.scale(0.3636, 0.3636)
          console.log('_31')
          if (pixel) {
            console.log('_33')
            let pixelBytesNumber: number = pixel.getPixelBytesNumber();
            const readBuffer = new ArrayBuffer(pixelBytesNumber);
            pixel.readPixelsToBufferSync(readBuffer)
            let stride = pixel.getImageInfoSync().stride
            const area: image.PositionArea = {
              pixels: readBuffer,
              offset: 0,
              stride: stride,
              region: { size: { width: 100, height: 100 }, x: 0, y: 0 }
            }
            let totalHeight = px2vp(100)
            let totalWidth = px2vp(100)
            let offCanvas: OffscreenCanvas =new OffscreenCanvas(totalWidth, totalHeight)
            let settings = new RenderingContextSettings(true)
            let offContext = offCanvas.getContext('2d', settings)
            let resultImage = offContext.getPixelMap(0, 0, totalWidth, totalHeight) as PixelMap
            try {
              resultImage.writePixels(area).then(() => {
                this._pixelMap2 = resultImage
                console.info('Gift', `write pixels success`)
              }).catch((e: object) => {
                console.info('Gift', `write callback err ${JSON.stringify(e)}`)
              })
            } catch (e) {
              console.info('Gift', `write catch err ${JSON.stringify(e)}`)
            }
          }
        }
      })
    }).catch((err: BusinessError) => {
      console.error(`Invoke photoPicker.select failed, code is ${err.code}, message is ${err.message}`);
    })
  }
}

按照区域读写。写入的区域宽度加X坐标不能大于原图的宽度,写入的区域高度加Y坐标不能大于原图的高度。

参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-image-V5#positionarea7

分享
微博
QQ
微信
回复
16h前
相关问题
HarmonyOS PAD从剪切板获取数据失败
51浏览 • 1回复 待解决
HarmonyOS 区分手机pad方法
43浏览 • 1回复 待解决
判断模型能否在手机上运行
1876浏览 • 1回复 待解决
HarmonyOS 识别手机录制语音失败
0浏览 • 1回复 待解决