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%')
  }
}
相关推荐
xmdy586619 分钟前
Flutter + 开源鸿蒙跨端实战|基于空间地理信息的**城市全域智慧泊车调度与多维运维管理平台** Day1 项目架构基座与工程化环境搭建
flutter·开源·harmonyos
KillerNoBlood27 分钟前
2026移动端跨平台开发面经总结
android·算法·flutter·ios·移动开发·鸿蒙·kmp
枫叶丹42 小时前
【HarmonyOS 6.0】状态栏扩展新特性:点击状态栏图标展开二级菜单的场景动效详解
开发语言·华为·harmonyos
想你依然心痛3 小时前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与Face AR & Body AR的“灵犀筑境“——PC端沉浸式AR建筑空间评审系统
华为·ar·harmonyos·悬浮导航·沉浸光感
xmdy58663 小时前
Flutter+开源鸿蒙全域智慧泊车调度管理平台 Day4 订单全流程闭环+支付核验+会员权益+个人中心开发
flutter·开源·harmonyos
前端不太难3 小时前
鸿蒙 App 多端 UI 不一致的原因
ui·状态模式·harmonyos
说再见再也见不到3 小时前
华为AC+AP旁挂二层组网+直接转发,配置实战
网络·华为·交换机·无线组网·无线ac
unique_williams3 小时前
开源 | 我用 HarmonyOS + Spring Boot 写了一个全栈背单词 App,已上架 GitHub
springboot·鸿蒙
key_3_feng4 小时前
鸿蒙6.0电子手表高山攀登指标监测功能开发实战
华为·harmonyos
nashane4 小时前
HarmonyOS 6学习:超大分辨率图片压缩与长截图生成优化实践
学习·华为·harmonyos