Q:如何实现PixelMap和base64的相互转换?
A:PixelMap是无压缩的位图对象,包含像素数据以及基本的图片信息(如图片尺寸、像素密度、像素格式等)。
Base64编码是一种将二进制数据转换为ASCII字符串的编码方式,用于存储、传输。一般不会对PixelMap做Base64编码,而是将PixelMap编码成Uint8Array后,再对文件的二进制数据进行Base64编码。
参考链接:图片编码、图片解码、Base64Helper。
Q:如何对相册图片进行编辑裁剪?
A:可以通过图片处理模块的pixelMap方法对图片进行编辑裁剪。
其中包括但不限于:
以下示例代码为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
});
}
// 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);
}