You can edit and crop an image by using the APIs of the pixelMap class of the image module.
The APIs include:
The following sample code uses pixelMap.crop to crop an image:
// 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);
}