如何对相册图片进行编辑裁剪
=============

可以通过[图片处理](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-image)模块的[pixelMap](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-image-pixelmap)方法对图片进行编辑裁剪。

其中包括但不限于：

*   [pixelMap.crop](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-image-pixelmap#crop9)方法，可以根据输入的尺寸对图片进行裁剪。
*   [pixelMap.opacity](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-image-pixelmap#opacity9)方法，可以通过设置透明比率对图片设置透明效果。
*   [pixelMap.scale](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-image-pixelmap#scale9)方法，可以根据输入的宽高对图片进行缩放。
*   [pixelMap.rotate](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-image-pixelmap#rotate9)方法，可以根据输入的角度对图片进行旋转。
*   [pixelMap.flip](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-image-pixelmap#flip9)方法，可以根据输入的条件对图片进行翻转。

以下示例代码为[pixelMap.crop](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-image-pixelmap#crop9)图片裁剪方法的使用：

_// Crop 4:3_
class RegionItem {
  _/\*\*_
   _\* width coordinate._
   _\*/_
  x: number;

  _/\*\*_
   _\* height coordinate._
   _\*/_
  y: number;

  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }
}

export async function cropCommon(pixelMap: PixelMap, cropWidth: number, cropHeight: number, cropPosition: RegionItem) {
  pixelMap.crop({
    size: {
      width: cropWidth,
      height: cropHeight
    },
    x: cropPosition.x,
    y: cropPosition.y
  });
}

_// Pass in three parameters: image. PixelMap, image width, and image height. After obtaining the cropped image width and height,_
_// pass the parameters into the cropCommon method_
export async function banner(pixelMap: PixelMap, width: number, height: number) {
  if (width <= height) {
    const cropWidth = width;
    const cropHeight = Math.floor(width \* 0.75);
    const cropPosition = new RegionItem(0, Math.floor((height - cropHeight) / 2));
    cropCommon(pixelMap, cropWidth, cropHeight, cropPosition);
    return;
  }
  if (width \* 0.75 >= height) {
    const cropWidth = Math.floor(height / 0.75);
    const cropHeight = height;
    const cropPosition = new RegionItem(Math.floor((width - cropWidth) / 2), 0);
    cropCommon(pixelMap, cropWidth, cropHeight, cropPosition);
    return;
  }
  const cropWidth = width;
  const cropHeight = Math.floor(width \* 0.75);
  const cropPosition = new RegionItem(0, Math.floor((height - cropHeight) / 2));
  cropCommon(pixelMap, cropWidth, cropHeight, cropPosition);
}