HarmonyOS image的pixelmap如何进行深拷贝

有什么方法能实现pixelmap的深拷贝?在拿到一份pixelMap的时候,想要对pixelmap做操作–裁剪,但又不想这操作影响原数据,请问有什么方法能实现到?

HarmonyOS
2025-01-10 08:35:17
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

参考demo:

import { AttributeUpdater } from '@kit.ArkUI';
import { image } from '@kit.ImageKit';

@Entry
@Component
struct CacheTest {
  build() {
    Column() {
      ReusableImageComponent({
        resource: $r('app.media.background')
      })
        .width('100%')
        .height(100)
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}

class MyImageAttributeModifier extends AttributeUpdater<ImageAttribute, ImageInterface> {
  initializeModifier(instance: ImageAttribute): void {
  }
}

@Reusable
@Component
struct ReusableImageComponent {
  @Prop resource: Resource
  @State imagePixelMap: image.PixelMap | undefined = undefined;
  @State imagePixelMap2: image.PixelMap | undefined = undefined
  private modifier: MyImageAttributeModifier = new MyImageAttributeModifier();

  private async getPixmapFromMedia(resource: Resource) {
    let unit8Array = await getContext(this)?.resourceManager?.getMediaContent({
      bundleName: resource.bundleName,
      moduleName: resource.moduleName,
      id: resource.id
    })
    let imageSource = image.createImageSource(unit8Array.buffer.slice(0, unit8Array.buffer.byteLength))
    let createPixelMap: image.PixelMap = await imageSource.createPixelMap({
      desiredPixelFormat: image.PixelMapFormat.RGB_565
    })
    await imageSource.release()
    return createPixelMap
  }

  async aboutToAppear() {
    this.imagePixelMap = await this.getPixmapFromMedia($r('app.media.poster1'))
    if (this.imagePixelMap) {
      let imageinfo = this.imagePixelMap.getImageInfoSync();
      let Format = 0;
      switch (imageinfo.pixelFormat) {
        case image.PixelMapFormat.RGB_565: {
          Format = 2;
          break;
        }
          ;
        case image.PixelMapFormat.RGBA_8888: {
          Format = 4;
          break;
        }
          ;
        case image.PixelMapFormat.BGRA_8888: {
          Format = 4;
          break;
        }
          ;
        case image.PixelMapFormat.RGB_888: {
          Format = 3;
          break;
        }
          ;
        case image.PixelMapFormat.ALPHA_8: {
          Format = 1;
          break;
        }
          ;
        case image.PixelMapFormat.RGBA_F16: {
          Format = 8;
          break;
        }
          ;
        case image.PixelMapFormat.NV21: {
          Format = 1.5;
          break;
        }
          ;
        case image.PixelMapFormat.NV12: {
          Format = 1.5;
          break;
        }
          ;
        default: {
          Format = 1;
          break;
        }
      }
      let array: ArrayBuffer = new ArrayBuffer(imageinfo.size.height * imageinfo.size.width * Format);
      this.imagePixelMap.readPixelsToBuffer(array);
      this.imagePixelMap2 = image.createPixelMapSync(array, {
        srcPixelFormat: imageinfo.pixelFormat,
        pixelFormat: imageinfo.pixelFormat,
        size: { width: imageinfo.size.width, height: imageinfo.size.height }
      })
      this.imagePixelMap2.flip(true, false);
    }
  }

  build() {
    Row() {
      Column() {
        Image($r('app.media.poster1'))
          .objectFit(ImageFit.Cover)
        Text('Raw picture')
      }.width('50%').height('100%')
      .margin({right: 5})

      Column() {
        Image(this.imagePixelMap2)
          .attributeModifier(this.modifier)
          .objectFit(ImageFit.Cover)
        Text('Copy picture')
      }.width('50%').height('100%')
      .margin({left: 5})
    }
    .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.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
分享
微博
QQ
微信
回复
2025-01-10 10:14:00
相关问题
HarmonyOS pixelmap拷贝问题
1398浏览 • 1回复 待解决
HarmonyOS 如何对数组进行拷贝
1008浏览 • 1回复 待解决
HarmonyOS拷贝拷贝
1062浏览 • 1回复 待解决
HarmonyOS 对象实现拷贝
999浏览 • 1回复 待解决
pixelMap格式图如何进行页面流转
95浏览 • 1回复 待解决
ArkTS中如何实现对象拷贝
1505浏览 • 1回复 待解决
HarmonyOS ArkWeb组件是否支持拷贝
1348浏览 • 2回复 待解决
ArkWeb组件是否支持拷贝
1390浏览 • 1回复 待解决
HarmonyOS image.PixelMap拉伸变形
527浏览 • 1回复 待解决
HarmonyOS 如何进行图片裁剪
636浏览 • 1回复 待解决
HarmonyOS 请问如何进行UrlEncode
590浏览 • 1回复 待解决
HarmonyOS 如何进行音频合成
969浏览 • 1回复 待解决
HarmonyOS 如何进行邀请测试
690浏览 • 1回复 待解决
HarmonyOS 如何进行sdk升级
589浏览 • 1回复 待解决
HarmonyOS 如何进行代码检查
685浏览 • 1回复 待解决