1# 图像变换(ArkTS) 2 3图片处理指对PixelMap进行相关的操作,如获取图片信息、裁剪、缩放、偏移、旋转、翻转、设置透明度、读写像素数据等。图片处理主要包括图像变换、[位图操作](image-pixelmap-operation.md),本文介绍图像变换。 4 5## 开发步骤 6 7图像变换相关API的详细介绍请参见[API参考](../reference/apis/js-apis-image.md#pixelmap7)。 8 91. 完成[图片解码](image-decoding.md#开发步骤),获取Pixelmap对象。 10 112. 获取图片信息。 12 13 ``` 14 import {BusinessError} from '@ohos.base' 15 // 获取图片大小 16 pixelMap.getImageInfo().then( (info : image.ImageInfo) => { 17 console.info('info.width = ' + info.size.width); 18 console.info('info.height = ' + info.size.height); 19 }).catch((err : BusinessError) => { 20 console.error("Failed to obtain the image pixel map information.And the error is: " + err); 21 }); 22 ``` 23 243. 进行图像变换操作。 25 26 原图: 27 28  29 - 裁剪 30 31 ``` 32 // x:裁剪起始点横坐标0 33 // y:裁剪起始点纵坐标0 34 // height:裁剪高度400,方向为从上往下(裁剪后的图片高度为400) 35 // width:裁剪宽度400,方向为从左到右(裁剪后的图片宽度为400) 36 pixelMap.crop({x: 0, y: 0, size: { height: 400, width: 400 } }); 37 ``` 38 39  40 41 - 缩放 42 43 ``` 44 // 宽为原来的0.5 45 // 高为原来的0.5 46 pixelMap.scale(0.5, 0.5); 47 ``` 48 49  50 51 - 偏移 52 53 ``` 54 // 向下偏移100 55 // 向右偏移100 56 pixelMap.translate(100, 100); 57 ``` 58 59  60 61 - 旋转 62 63 ``` 64 // 顺时针旋转90° 65 pixelMap.rotate(90); 66 ``` 67 68  69 70 - 翻转 71 72 ``` 73 // 垂直翻转 74 pixelMap.flip(false, true); 75 ``` 76 77  78 79 80 ``` 81 // 水平翻转 82 pixelMap.flip(true, false); 83 ``` 84 85  86 87 - 透明度 88 89 ``` 90 // 透明度0.5 91 pixelMap.opacity(0.5); 92 ``` 93 94  95