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

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

HarmonyOS
2024-03-18 22:33:09
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
pfuchenlu

可以通过图片处理模块的pixelMap方法对图片进行编辑裁剪。

其中包括但不限于:

pixelMap.crop方法,可以根据输入的尺寸对图片进行裁剪。

pixelMap.opacity方法,可以通过设置透明比率对图片设置透明效果。

pixelMap.scale方法,可以根据输入的宽高对图片进行缩放。

pixelMap.rotate方法,可以根据输入的角度对图片进行旋转。

pixelMap.flip方法,可以根据输入的条件对图片进行翻转。

以下示例代码为pixelMap.crop图片裁剪方法的使用:

// 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); 
} 
...
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
分享
微博
QQ
微信
回复
2024-03-19 22:26:40
相关问题
如何编辑裁剪相册中的图片
1682浏览 • 1回复 待解决
HarmonyOS 如何进行图片裁剪
948浏览 • 1回复 待解决
如何图片进行高斯模糊处理
3053浏览 • 1回复 待解决
基于ImageKit图片进行处理
1550浏览 • 1回复 待解决
基于PhotoViewPicker图片进行操作
1811浏览 • 1回复 待解决
如何实现图片裁剪、旋转
1438浏览 • 1回复 待解决
HarmonyOS 相册,相机拍照并裁剪
891浏览 • 1回复 待解决
HarmonyOS 怎么图片进行压缩上传
923浏览 • 1回复 待解决
HarmonyOS 图片裁剪问题
708浏览 • 1回复 待解决
指定url的图片进行下载保存
2104浏览 • 1回复 待解决
HarmonyOS 如何实现图片编辑功能
953浏览 • 1回复 待解决