如何用PixelMap处理图片

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

HarmonyOS
2024-05-26 17:39:06
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
towerwan

使用的核心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 }) 
}

图像像素类,用于读取或写入图像数据以及获取图像信息。在调用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 }) 
}
分享
微博
QQ
微信
回复
2024-05-27 22:22:11
相关问题
如何用openGL做解码后处理
508浏览 • 0回复 待解决
PixelMap数据处理(Native)
402浏览 • 1回复 待解决
如何图片PixelMap压缩到指定大小
388浏览 • 1回复 待解决
如何图片进行高斯模糊处理
577浏览 • 1回复 待解决
怎么把PixelMap图片转BASE64
7764浏览 • 1回复 待解决
基于ImageKit对图片进行处理
153浏览 • 1回复 待解决
如何用websocket发送音频流?
2250浏览 • 1回复 待解决
如何用WebView创建新窗口。
353浏览 • 1回复 待解决
mybatis 多个参数时,如何用in?
904浏览 • 1回复 待解决
如何用redis/memcache做缓存层?
990浏览 • 1回复 待解决