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%')
  }
}
相关推荐
Swift社区3 小时前
AI 原生鸿蒙应用开发实战
人工智能·华为·harmonyos
盐焗西兰花3 小时前
鸿蒙学习实战之路-Share Kit系列(12/17)-判断应用是否被系统分享拉起
前端·学习·harmonyos
互联网散修3 小时前
鸿蒙应用开发UI基础第二十四节:构造Preferences用户首选项数据存储开源工具
开源·harmonyos·鸿蒙应用开发教程
国医中兴5 小时前
Flutter 三方库 ngrouter 鸿蒙适配指南 - 实现高性能扁平化路由导航管理实战
flutter·harmonyos·鸿蒙·openharmony
大雷神6 小时前
HarmonyOS APP<玩转React>开源教程十:组件化开发概述
前端·react.js·开源·harmonyos
国医中兴6 小时前
Flutter 三方库 inject_generator 的鸿蒙化适配指南 - 自动化依赖注入注入生成器、驱动鸿蒙大型工程解耦实战
flutter·harmonyos·鸿蒙·openharmony·inject_generator
Easonmax6 小时前
ReactNative for OpenHarmony项目鸿蒙化三方库:react-native-pager-view — 流畅的页面滑动体验
react native·react.js·harmonyos
讯方洋哥7 小时前
HarmonyOS App开发——鸿蒙ArkTS在手机和PC多终端适配机制应用
harmonyos
以太浮标7 小时前
华为eNSP模拟器综合实验之- 虚拟路由冗余协议VRRP(Virtual Router Redundancy Protocol)解析
网络·网络协议·华为·智能路由器·信息与通信
国医中兴8 小时前
Flutter 三方库 themed_color_palette 的鸿蒙化适配指南 - 定义语义化调色板、在鸿蒙端实现像素级的主题切换实战
flutter·harmonyos·鸿蒙·openharmony·themed_color_palette