HarmonyOS Next应用开发——图像PixelMap变换

【高心星出品】

图像变换

图片处理指对PixelMap进行相关的操作,如获取图片信息、裁剪、缩放、偏移、旋转、翻转、设置透明度、读写像素数据等。图片处理主要包括图像变换、位图操作,本文介绍图像变换。

图形裁剪
// 裁剪图片 x,y为裁剪的起始坐标,size为裁剪的图片宽和高
temp.cropSync({ x: 20, y: 20, size: { width: this.imagewidth - 20, height: this.imageheight - 20 } })
图形缩放
// 缩放图片
temp.scaleSync(0.5, 0.5)
图形偏移
// 偏移图片
temp.translateSync(30, 20)
图形旋转
// 旋转角度
temp.rotateSync(90)
图形反转
// 水平反转图片
temp.flipSync(true, false)
图形透明度
// 图片半透明
temp.opacitySync(0.5)
完整代码
typescript 复制代码
import { image } from '@kit.ImageKit';

@Entry
@Component
struct PmChange {
  @State message: string = 'Hello World';
  @State pm: PixelMap | undefined = undefined
  @State crop: PixelMap | undefined = undefined
  private imagewidth: number = 0
  private imageheight: number = 0

  genpm(index: number) {
    // 获取资源图片的字节
    let buffer = getContext(this).resourceManager.getMediaContentSync($r('app.media.jingse')).buffer.slice(0)
    // 生成imagesource
    let source = image.createImageSource(buffer)
    // 转化为pixelmap
    let temp = source.createPixelMapSync({ editable: true })
    switch (index) {
      case 0:
        // 裁剪图片 x,y为裁剪的起始坐标,size为裁剪的图片宽和高
        temp.cropSync({ x: 20, y: 20, size: { width: this.imagewidth - 20, height: this.imageheight - 20 } })
        return temp
      case 1:
        // 缩放图片
        temp.scaleSync(0.5, 0.5)
        return temp
      case 2:
        // 偏移图片
        temp.translateSync(30, 20)
        return temp
      case 3:
        // 旋转角度
        temp.rotateSync(90)
        return temp
      case 4:
        // 水平反转图片
        temp.flipSync(true, false)
        return temp
      case 5:
        // 图片半透明
        temp.opacitySync(0.5)
        return temp
      default :
        return temp


    }
  }

  aboutToAppear(): void {
    this.pm=this.genpm(-1)
    let imginfo = this.pm.getImageInfoSync()
    // 获取图片的宽和高
    this.imagewidth = imginfo.size.width
    this.imageheight = imginfo.size.height
    
  }

  build() {
    Column() {
      Image(this.pm).width(300).height(300).margin({ top: 20 })
      Flex({
        direction: FlexDirection.Row,
        wrap: FlexWrap.Wrap,
        justifyContent: FlexAlign.SpaceAround,

      }) {
        Stack() {
          Image(this.genpm(0)).width(100).height(100).border({width:2,color:Color.Red})
          Text('裁剪掉20vp')
        }.margin({top:20})

        Stack() {
          Image(this.genpm(1)).width(100).height(100).objectFit(ImageFit.ScaleDown).border({width:2,color:Color.Red})
          Text('缩小一半')
        }.margin({top:20})

        Stack() {
          Image(this.genpm(2)).width(100).height(100).objectFit(ImageFit.Fill).border({width:2,color:Color.Red})
          Text('偏移图片')
        }.margin({top:20})

        Stack() {
          Image(this.genpm(3)).width(100).height(100).border({width:2,color:Color.Red})
          Text('旋转90°')
        }.margin({top:20})

        Stack() {
          Image(this.genpm(4)).width(100).height(100).border({width:2,color:Color.Red})
          Text('水平反转')
        }.margin({top:20})

        Stack() {
          Image(this.genpm(5)).width(100).height(100).border({width:2,color:Color.Red})
          Text('图片半透明')
        }.margin({top:20})
      }.width('100%')
      .padding(20)
    }.width('100%')
    .height('100%')
  }
}
Color.Red})
          Text('图片半透明')
        }.margin({top:20})
      }.width('100%')
      .padding(20)
    }.width('100%')
    .height('100%')
  }
}
相关推荐
行十万里人生25 分钟前
Qt 对象树详解:从原理到运用
开发语言·数据库·qt·华为od·华为·华为云·harmonyos
ax一号街阿楠42 分钟前
华为AP 4050DN-HD的FIT AP模式改为FAT AP,家用FAT基本配置
网络·华为·信息与通信
电子小子洋酱2 小时前
ESP32移植Openharmony外设篇(9)NB-IOT
单片机·物联网·华为·harmonyos·鸿蒙
北京迅为2 小时前
【北京迅为】itop-3568 开发板openharmony鸿蒙烧写及测试-第1章 体验OpenHarmony—烧写镜像
人工智能·单片机·嵌入式硬件·harmonyos·鸿蒙
北京迅为4 小时前
【北京迅为】itop-3568 开发板openharmony鸿蒙烧写及测试-第2章OpenHarmony v3.2-Beta4版本测试
嵌入式硬件·鸿蒙·rk3568
以山河作礼。4 小时前
极速探索 HarmonyOS NEXT:开启国产操作系统开发的新篇章
华为·harmonyos
微软MVP Eleven5 小时前
【朝夕教育】《鸿蒙原生应用开发从零基础到多实战》003-TypeScript 中的类
华为·typescript·harmonyos
敢嗣先锋18 小时前
鸿蒙5.0实战案例:基于WaterFlow的页面滑动加载
c++·移动开发·harmonyos·arkui·组件化·鸿蒙开发·页面布局
Huang兄18 小时前
鸿蒙-状态管理V2其他方法
harmonyos
seabirdssss21 小时前
华为机试牛客刷题之HJ75 公共子串计算
java·算法·华为