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%')
  }
}
相关推荐
烬头882144 分钟前
React Native鸿蒙跨平台实现二维码联系人APP(QRCodeContactApp)
javascript·react native·react.js·ecmascript·harmonyos
xiaoqi9223 小时前
React Native鸿蒙跨平台如何实现分类页面组件通过searchQuery状态变量管理搜索输入,实现了分类的实时过滤功能
javascript·react native·react.js·ecmascript·harmonyos
听麟3 小时前
HarmonyOS 6.0+ 智慧出行导航APP开发实战:离线地图与多设备位置协同落地
华为·wpf·harmonyos
qq_177767373 小时前
React Native鸿蒙跨平台实现应用介绍页,实现了应用信息卡片展示、特色功能网格布局、权限/联系信息陈列、评分展示、模态框详情交互等通用场景
javascript·react native·react.js·ecmascript·交互·harmonyos
jin1233225 小时前
基于React Native鸿蒙跨平台地址管理是许多电商、外卖、物流等应用的重要功能模块,实现了地址的添加、编辑、删除和设置默认等功能
javascript·react native·react.js·ecmascript·harmonyos
2501_920931705 小时前
React Native鸿蒙跨平台医疗健康类的血压记录,包括收缩压、舒张压、心率、日期、时间、备注和状态
javascript·react native·react.js·ecmascript·harmonyos
2501_920931706 小时前
React Native鸿蒙跨平台使用useState管理健康记录和过滤状态,支持多种健康数据类型(血压、体重等)并实现按类型过滤功能
javascript·react native·react.js·ecmascript·harmonyos
2501_921930836 小时前
高级进阶 React Native 鸿蒙跨平台开发:InteractionManager 交互优化
react native·harmonyos
前端不太难7 小时前
HarmonyOS PC 文档模型完整范式
华为·状态模式·harmonyos
ITUnicorn8 小时前
【HarmonyOS6】从零实现自定义计时器:掌握TextTimer组件与计时控制
华为·harmonyos·arkts·鸿蒙·harmonyos6