HarmonyOS 需要实现拼长图九宫格图片裁剪功能?

需要实现选择图片后拼接功能,图片可编辑边框及边框颜色,图片可按模板样式拼接,可实现九宫格、拼台词、智能拼接网页长截图方案这样的功能有没有已实现的实例代码呀

阅读 501
1 个回答

拼接图片可以参考:https://developer.huawei.com/consumer/cn/forum/topic/0209140872409552061?fid=0102683795438680754

图片裁剪可以参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-image-7-V5

// 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
  });
}

// 传入image.PixelMap、图片width、图片height三个参数,获取到裁剪后的图片宽度和高度后将参数传入cropCommon方法
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);
}