如何用PixelMap处理图片

pixelmap图像像素类,用于读取或写入图像数据以及获取图像信息。此demo用于研究将图片转换为pixelmap实例后如何处理图片。

HarmonyOS
2024-05-26 17:39:06
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
鸡翅可乐

使用的核心API

pixelmap

createpixelmap

核心代码解释

访问控制管理:获取访问控制模块对象。首先获取媒体应用的权限。

@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World' 
 
  aboutToAppear() { 
    //申请权限 
    let context = getContext() as common.UIAbilityContext; 
    abilityAccessCtrl.createAtManager().requestPermissionsFromUser(context, ['ohos.permission.READ_MEDIA']).then(() => { 
    }); 
  } 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
        //1 
        Button('图片选择').fancy() 
          .onClick(() => { 
            photoSelect() 
          }) 
        //2 
        Button('相机界面').fancy() 
          .onClick(() => { 
            startImageCapture() 
          }) 
        Button('图片处理').fancy() 
          .onClick(() => { 
            router.pushUrl({ url: 'pages/ImageProcessTest' }) 
          }) 
        // Button('图片压缩和打包').fancy() 
        //   .onClick(() => { 
        //     imagePack() 
        //   }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
 
 
@Extend(Button) 
function fancy() { 
  .fontSize(25) 
  .fontWeight(FontWeight.Bold) 
  .margin({ bottom: 10 }) 
}
  • 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.

图像像素类,用于读取或写入图像数据以及获取图像信息。在调用PixelMap的方法前,需要先通过createPixelMap创建一个PixelMap实例。目前pixelmap序列化大小最大128MB,超过会送显失败。大小计算方式为(宽*高*每像素占用字节数)。通过属性创建PixelMap,默认采用BGRA_8888格式处理数据,通过回调函数返回结果。

@Entry 
@Component 
struct Index { 
  @State imgUrl: PixelMap = undefined; 
  private pixelmap: PixelMap = undefined; 
 
  aboutToAppear() { 
    this.resetPixelMap(); 
  } 
 
  resetPixelMap() { 
    getContext(this).resourceManager.getRawFileContent("httpimage.png").then((uint8Array) => { 
      let imageSourceApi = image.createImageSource(uint8Array.buffer); // 解码接口 
      let opts = { editable: true, pixelFormat: 3, size: { height: 50, width: 50 } } 
      imageSourceApi.createPixelMap(opts, (error, pixelmap) => { 
        if (error) { 
          console.log('Failed to create pixelmap.'); 
        } else { 
          console.log('Succeeded in creating pixelmap.'); 
          this.pixelmap = pixelmap; 
          this.imgUrl = this.pixelmap; 
        } 
      }) 
    }) 
  } 
 
  build() { 
    Column() { 
      Button('缩放:scale').fancy() 
        .onClick(() => { 
          this.pixelmap.scale(0.5,0.5).then(()=>{ 
            console.log('rotate success') 
            this.imgUrl = this.pixelmap; 
          }).catch((err)=>{ 
            console.log('rotate fail with err '+JSON.stringify(err)) 
          }); 
        }) 
 
      Button('旋转:ROTATE').fancy() 
        .onClick(() => { 
          this.pixelmap.rotate(90).then(()=>{ 
            console.log('rotate success') 
            this.imgUrl = this.pixelmap; 
          }).catch((err)=>{ 
            console.log('rotate fail with err '+JSON.stringify(err)) 
          }); 
        }) 
 
      Button('裁剪:CROP').fancy() 
        .onClick(() => { 
          this.pixelmap.crop({ x: 5, y: 5, size: { height: 50, width: 50 } }); 
          this.imgUrl = this.pixelmap; 
        }) 
 
      Button('偏移:translate').fancy() 
        .onClick(() => { 
          this.pixelmap.translate(100,100); 
          this.imgUrl = this.pixelmap; 
        }) 
 
      Button('翻转:flip').fancy() 
        .onClick(() => { 
          this.pixelmap.flip(false, true); 
          this.imgUrl = this.pixelmap; 
        }) 
 
      Button('透明度:opacity').fancy() 
        .onClick(() => { 
          this.pixelmap.opacity(0.5); 
          this.imgUrl = this.pixelmap; 
        }) 
 
      Button('RESET').fancy() 
        .onClick(() => { 
          this.resetPixelMap(); 
        }) 
      Button('Back').fancy() 
        .onClick(() => { 
          router.back({ url:'pages/Index' }) 
        }) 
 
      Row(){ 
        // 将编辑好的pixelMap传递给状态变量imagePixelMap后,通过Image组件进行渲染 
        Image(this.imgUrl).objectFit(ImageFit.None) 
      }.width('100%').height('50%').backgroundColor('#F0F0F0') 
      // Image(this.imgUrl).width(50).height(50).objectFit(ImageFit.Fill) 
 
    } 
    .justifyContent(FlexAlign.Center) 
    .width('100%') 
    .height('100%') 
  } 
} 
 
 
@Extend(Button) function fancy() { 
  .fontSize(25) 
  .fontWeight(FontWeight.Bold) 
  .margin({ bottom: 10 }) 
}
  • 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.
分享
微博
QQ
微信
回复
2024-05-27 22:22:11


相关问题
如何用openGL做解码后处理
2237浏览 • 0回复 待解决
HarmonyOS PixelMap位图处理问题
420浏览 • 1回复 待解决
PixelMap数据处理(Native)
1953浏览 • 1回复 待解决
HarmonyOS PixelMap加载图片
435浏览 • 1回复 待解决
HarmonyOS 网络图片如何转换成PixelMap
388浏览 • 1回复 待解决
HarmonyOS 获取网络图片PixelMap
440浏览 • 1回复 待解决
PixelMap怎么保存成图片文件
1011浏览 • 1回复 待解决
如何图片PixelMap压缩到指定大小
2551浏览 • 1回复 待解决
如何图片进行高斯模糊处理
2670浏览 • 1回复 待解决
HarmonyOS 图片裁切处理
287浏览 • 1回复 待解决