在移动应用开发中,图片编辑功能是用户日常使用的高频场景,例如:相册的图片编辑、社交媒体中对图片的滤镜美化等。
PixelMap(参考Interface(PixelMap))是HarmonyOS中用于表示图像像素数据的核心类。它提供了一种无压缩的位图格式,允许开发者直接读取、写入像素数据,并获取图像信息,是进行图像处理、变换和绘制的基础。本文主要介绍如何使用PixelMap对图片进行编辑处理,如获取图片信息、裁剪、缩放、偏移、旋转、翻转、设置透明度、读写像素数据等。
相关功能场景描述及关键技术点如下表所示:
功能描述 | 场景描述 | 关键技术点 |
|---|---|---|
查看图片元数据,包括图片尺寸、像素格式、HDR属性等信息,帮助用户在编辑图片前了解图片基础属性。 | 通过getImageInfo()获取图片元数据。 | |
按比例裁剪图片,适配不同平台要求,例如:头像(1:1)、广告(4:3)、视频封面(16:9)。 | 计算图片裁剪宽高比,通过PixelMap.crop()裁剪。 | |
沿水平/垂直方向调整图片位置,用于构图微调。 | 计算图片横纵方向偏移量,通过PixelMap.translate()执行图片在横纵方向上进行平移。 | |
图片逆时针旋转,修正拍摄角度偏差,适配不同显示方向。 | 计算图片旋转角度,调用PixelMap.rotate()对图片进行旋转。 | |
图片水平翻转,创建对称效果,用于创意设计、制作倒影效果。 | 通过变量控制翻转方向,调用PixelMap.flipSync()翻转图片。 | |
图片按照80%~120%范围进行缩放,适配不同设备屏幕尺寸。 | 等比缩放比例计算,通过PixelMap.scale()执行图片缩放。 |
功能描述 | 场景描述 | 关键技术点 |
|---|---|---|
通过滑块调节图片整体明暗程度,适用于光线不足或过强的图片。 | 像素级RGB数据处理,通过线性缩放算法RGB_new = RGB * factor,结合Worker线程(参考Worker简介)异步处理避免卡顿。 | |
通过滑块调节图片透明度,创建半透明叠加效果,用于UI遮罩或背景虚化。 | 通过PixelMap.opacity()接口,直接修改Alpha通道。 | |
通过滑块调节图片饱和度,增强或降低色彩鲜艳程度,实现图片艺术风格调整。 | 像素级RGB数据处理,通过亮度公式结合饱和度算法,并结合Worker线程异步处理。 | |
图片滤镜添加(参考图片黑白滤镜) | 为图片添加预置滤镜效果,用于图片艺术创作、风格化处理。 | 通过effectKit.createEffect()创建filter图像效果对象,并结合对应接口实现滤镜效果添加。 |
this.imageSource = await createImageSourceFromResource(this.getUIContext());
// ...
this.imageSource.getImageInfo((err, imageInfo) => {
if (err) {
Logger.error(`Failed to get image info: code = ${err?.code}, message = ${err?.message}`);
return;
}
Object.keys(imageInfo).forEach((key) => {
if (key === 'size') {
Object.keys(imageInfo[key]).forEach((chlKey) => {
this.imageInfoArr.push({
label: chlKey,
value: imageInfo[key][chlKey]
});
});
} else {
this.imageInfoArr.push({
label: key,
value: imageInfo[key]
})
}
});
}); List() {
ForEach(this.imageInfoArr, (item: ImageInfoItem) => {
ListItem() {
Row() {
Text(item.label)
// ...
Text(item.value + '')
// ...
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.SpaceBetween)
.padding({ left: 12, right: 12 })
}
.width('100%')
.height(48)
}, (item: ImageInfoItem) => item.label)
}
.divider({
strokeWidth: 0.5,
color: 'rgba(0,0,0,0.2)',
startMargin: 12,
endMargin: 12
})
// ... /**
* Crop image to 1:1 square ratio. The crop region is centered.
*
* @param pixelMap - The image to crop.
* @param width - Original image width.
* @param height - Original image height.
*/
export async function square(pixelMap: PixelMap, width: number, height: number) {
try {
if (width < height) {
await pixelMap.crop({
size: {
width: width,
height: width
},
x: 0,
y: Math.round((height - width) / CommonConstants.AVERAGE_WEIGHT_WIDTH)
});
} else {
await pixelMap.crop({
size: {
width: height,
height: height
},
x: Math.round((width - height) / CommonConstants.AVERAGE_WEIGHT_WIDTH),
y: 0
});
}
} catch (err) {
let error = err as BusinessError;
Logger.error(TAG, `square crop failed: ${error.code}, ${error.message}`);
}
} export async function cropCommon(pixelMap: PixelMap, cropWidth: number, cropHeight: number, cropPosition: RegionItem) {
try {
await pixelMap.crop({
size: {
width: cropWidth,
height: cropHeight
},
x: cropPosition.x,
y: cropPosition.y
});
} catch (err) {
let error = err as BusinessError;
Logger.error(TAG, `cropCommon failed: ${error.code}, ${error.message}`);
}
} /**
* Crop image to 4:3 ratio. The crop region is centered.
*
* @param pixelMap - The image to crop.
* @param width - Original image width.
* @param height - Original image height.
*/
export async function banner(pixelMap: PixelMap, width: number, height: number) {
if (width <= height) {
const cropWidth = width;
const cropHeight = Math.floor(width * CommonConstants.CROP_RATE_4_3);
const cropPosition = new RegionItem(0, Math.floor((height - cropHeight) / CommonConstants.AVERAGE_WEIGHT_WIDTH));
await cropCommon(pixelMap, cropWidth, cropHeight, cropPosition);
return;
}
if (width * CommonConstants.CROP_RATE_4_3 >= height) {
const cropWidth = Math.floor(height / CommonConstants.CROP_RATE_4_3);
const cropHeight = height;
const cropPosition = new RegionItem(Math.floor((width - cropWidth) / CommonConstants.AVERAGE_WEIGHT_WIDTH), 0);
await cropCommon(pixelMap, cropWidth, cropHeight, cropPosition);
return;
}
const cropWidth = width;
const cropHeight = Math.floor(width * CommonConstants.CROP_RATE_4_3);
const cropPosition = new RegionItem(0, Math.floor((height - cropHeight) / CommonConstants.AVERAGE_WEIGHT_WIDTH));
await cropCommon(pixelMap, cropWidth, cropHeight, cropPosition);
} /**
* Crop image to 16:9 ratio. The crop region is centered.
*
* @param pixelMap - The image to crop.
* @param width - Original image width.
* @param height - Original image height.
*/
export async function rectangle(pixelMap: PixelMap, width: number, height: number) {
if (width <= height) {
const cropWidth = width;
const cropHeight = Math.floor(width * (CommonConstants.CROP_RATE_9_16));
const cropPosition = new RegionItem(0, Math.floor((height - cropHeight) / CommonConstants.AVERAGE_WEIGHT_WIDTH));
await cropCommon(pixelMap, cropWidth, cropHeight, cropPosition);
return;
}
if (width * (CommonConstants.CROP_RATE_9_16) >= height) {
const cropWidth = Math.floor(height / (CommonConstants.CROP_RATE_9_16));
const cropHeight = height;
const cropPosition = new RegionItem(Math.floor((width - cropWidth) / CommonConstants.AVERAGE_WEIGHT_WIDTH), 0);
await cropCommon(pixelMap, cropWidth, cropHeight, cropPosition);
return;
}
const cropWidth = width;
const cropHeight = Math.floor(width * (CommonConstants.CROP_RATE_9_16));
const cropPosition = new RegionItem(0, Math.floor((height - cropHeight) / CommonConstants.AVERAGE_WEIGHT_WIDTH));
await cropCommon(pixelMap, cropWidth, cropHeight, cropPosition);
} TabContent() {
TranslateView({
onTranslateChange: (value: number, mode: SliderChangeMode) => {
this.editController?.handleTranslateChange(value, mode);
}
})
} Slider({
value: this.editState.currentTranslateData[this.editState.currentTranslateMode],
step: 1,
min: 0,
max: 1000,
// ...
})
// ...
.onChange((value: number, mode: SliderChangeMode) => {
if (this.onTranslateChange !== undefined) {
this.onTranslateChange(value > 1000 ? 1000 : value, mode);
}
}) async handleTranslateChange(value: number, mode: SliderChangeMode): Promise<void> {
if (mode === SliderChangeMode.End &&
value !== this.editState.currentTranslateData[this.editState.currentTranslateMode]) {
const moveValue: number =
Math.round(value) - this.editState.currentTranslateData[this.editState.currentTranslateMode];
this.editState.updateTranslateData(this.editState.currentTranslateMode, Math.round(value));
if (this.editState.currentTranslateMode === 0) {
// horizontal translation.
await this.pixelMapManager.translateImage({ x: moveValue, y: 0 });
} else {
// vertical translation.
await this.pixelMapManager.translateImage({ x: 0, y: moveValue });
}
}
} getStartEditPixelMap(mode: EditType): PixelMap | undefined {
if (this.editMode !== mode) {
this.editMode = mode;
if (this.finalEditPixelMap !== null && this.finalEditPixelMap !== undefined) {
this.startEditPixelMap = this.replaceWithClone(this.startEditPixelMap, this.finalEditPixelMap);
}
}
let target: PixelMap | undefined;
switch (mode) {
case EditType.CROP:
target = this.startEditPixelMap;
break;
case EditType.MIRROR:
target = this.finalEditPixelMap;
break;
case EditType.ROTATION:
target = this.finalEditPixelMap;
break;
case EditType.FILTER:
target = this.startEditPixelMap;
break;
case EditType.ADJUST:
target = this.startEditPixelMap;
break;
case EditType.TRANSLATION:
target = this.finalEditPixelMap;
break;
case EditType.SCALE:
target = this.startEditPixelMap;
break;
default:
target = this.finalEditPixelMap;
break;
}
if (target !== null && target !== undefined) {
return this.clonePixelMap(target);
}
return undefined;
} // Copy the current Pixelmap object.
clonePixelMap(pixelMap: PixelMap): PixelMap {
try {
return pixelMap.cloneSync();
} catch (err) {
let error = err as BusinessError;
Logger.error(TAG, `${error.code}, ${error.message}`);
return pixelMap;
}
} async translateImage(moveValue: TranslateValue) {
const px = this.getStartEditPixelMap(EditType.TRANSLATION);
if (px === null || px === undefined) {
return;
}
try {
await px.translate(moveValue.x, moveValue.y); // Perform image offset.
} catch (err) {
let error = err as BusinessError;
Logger.error(TAG, `translate failed: ${error.code}, ${error.message}`);
}
this.finalEditPixelMap = this.replaceWith(this.finalEditPixelMap, px);
this.notifyPreviewUpdate(px);
} rotateImage(): void {
this.editState.rotationAngle = (this.editState.rotationAngle - 90 + 360) % 360;
this.pixelMapManager.cropImage(this.editState.currentCropMode, this.editState.rotationAngle,
this.editState.isMirrored);
} async cropImage(proportion: CropType, rotationAngle: number, isMirrored: boolean) {
if (this.originCropPixelMap !== null && this.originCropPixelMap !== undefined) {
// ...
await this.applyTransforms(px, rotationAngle, isMirrored);
this.finalEditPixelMap = this.replaceWith(this.finalEditPixelMap, px);
this.notifyPreviewUpdate(px);
}
} private async applyTransforms(px: PixelMap, rotationAngle: number, isMirrored: boolean): Promise<void> {
if (rotationAngle !== 0) {
try {
await px.rotate(rotationAngle);
} catch (error) {
Logger.error(TAG, `there is a error in rotate process with ${error?.code}`);
}
}
// ...
} 通过flipSync()方法,根据输入的条件对图片进行翻转:
mirrorImage(): void {
this.editState.isMirrored = !this.editState.isMirrored;
this.pixelMapManager.cropImage(this.editState.currentCropMode, this.editState.rotationAngle,
this.editState.isMirrored);
} async cropImage(proportion: CropType, rotationAngle: number, isMirrored: boolean) {
if (this.originCropPixelMap !== null && this.originCropPixelMap !== undefined) {
// ...
await this.applyTransforms(px, rotationAngle, isMirrored);
this.finalEditPixelMap = this.replaceWith(this.finalEditPixelMap, px);
this.notifyPreviewUpdate(px);
}
} private async applyTransforms(px: PixelMap, rotationAngle: number, isMirrored: boolean): Promise<void> {
// ...
if (isMirrored) {
try {
px.flipSync(true, false);
} catch (error) {
Logger.error(TAG, `there is a error in mirror process with ${error?.code}`);
}
}
} async handleZoomChange(value: number, mode: SliderChangeMode): Promise<void> {
if (mode === SliderChangeMode.End && value !== this.editState.currentZoom) {
await this.pixelMapManager.editImageScale(value / 100);
this.editState.currentZoom = value;
}
} async editImageScale(scale: number) {
const px = this.getStartEditPixelMap(EditType.SCALE);
if (px === null || px === undefined) {
return;
}
try {
await px.scale(scale, scale);
} catch (err) {
let error = err as BusinessError;
Logger.error(TAG, `${error.code}, ${error.message}`);
}
this.finalEditPixelMap = this.replaceWith(this.finalEditPixelMap, px);
this.notifyPreviewUpdate(px);
} 通过线性缩放每个像素的RGB分量来改变图像的整体明暗程度。
对应公式:
R_new = R_original * factor
G_new = G_original * factor
B_new = B_original * factor
factor = 当前调节值 / 100
原理说明:
function adjustBrightness(pixelData: Uint8ClampedArray, adjustedData: Uint8ClampedArray, i: number,
factor: number): void {
const r = pixelData[i] * factor;
const g = pixelData[i + 1] * factor;
const b = pixelData[i + 2] * factor;
adjustedData[i] = Math.max(0, Math.min(255, Math.round(r)));
adjustedData[i + 1] = Math.max(0, Math.min(255, Math.round(g)));
adjustedData[i + 2] = Math.max(0, Math.min(255, Math.round(b)));
} export function execColorInfo(bufferArray: ArrayBuffer, last: number, cur: number, hsvIndex: number) {
// ...
try {
const pixelData = new Uint8ClampedArray(bufferArray);
const adjustedData = new Uint8ClampedArray(pixelData.length);
const bytesPerPixel = 4;
const factor = cur / 100;
for (let i = 0; i < pixelData.length; i += bytesPerPixel) {
adjustedData[i + 3] = pixelData[i + 3];
if (pixelData[i + 3] < 1) {
continue;
}
if (hsvIndex === AdjustType.BRIGHTNESS) {
adjustBrightness(pixelData, adjustedData, i, factor);
}
// ...
}
return adjustedData.buffer;
} catch (error) {
Logger.error(TAG, `Failed to set adjustedData: code = ${error?.code}, message = ${error?.message}`);
return null;
}
} workerPort.onmessage = (event: MessageEvents) => {
// ...
let bufferArray: ArrayBuffer = data.buf;
let last: number = data.last;
let cur: number = data.cur;
let adjustType: AdjustType = data.adjustType;
let buffer = execColorInfo(bufferArray, last, cur, adjustType);
if (!buffer) {
Logger.error(TAG, 'execColorInfo returned null');
return;
}
try {
// Return the processed buffer.
workerPort.postMessage(buffer);
} catch (error) {
Logger.error(TAG, `post message error: code = ${error?.code}, message = ${error?.message}`);
}
}; private async processAdjustWorker(value: number, buffer: ArrayBuffer, adjustType: AdjustType): Promise<ArrayBuffer> {
await this.adjustLock;
return new Promise((resolve) => {
this.adjustLock = new Promise<void>((release) => {
const message = new MessageItem(buffer, CommonConstants.SLIDER_MAX, value, adjustType);
this.adjustWorker.onmessage = (event: MessageEvents) => {
resolve(event.data);
release();
};
this.adjustWorker.onerror = () => {
resolve(buffer);
release();
};
try {
this.adjustWorker.postMessage(message);
} catch (error) {
Logger.error(TAG, `postMessage failed, code is ${error.code}, message is ${error.message}`);
resolve(buffer);
release();
}
});
});
} async adjustImage(currentAdjustData: number[]) {
// Obtain the cloned baseline pixel image.
const px = this.getStartEditPixelMap(EditType.ADJUST);
if (px === null || px === undefined) {
return;
}
let buffer = new ArrayBuffer(px.getPixelBytesNumber());
await px.readPixelsToBuffer(buffer).catch((err: BusinessError) => {
Logger.error(TAG, `readPixelsToBuffer failed: ${err.code}, ${err.message}`);
});
if (!buffer) {
this.releasePixelMap(px);
return;
}
if (currentAdjustData[AdjustType.BRIGHTNESS] !== CommonConstants.SLIDER_MAX) {
buffer = await this.processAdjustWorker(
currentAdjustData[AdjustType.BRIGHTNESS],
buffer,
AdjustType.BRIGHTNESS
);
try {
px.writeBufferToPixelsSync(buffer); // Write the processed buffer back to Pixelmap.
} catch (err) {
let error = err as BusinessError;
Logger.error(TAG, `${error.code}, ${error.message}`);
}
}
// ...
this.finalEditPixelMap = this.replaceWith(this.finalEditPixelMap, px); // Save edited pixelMap.
this.notifyPreviewUpdate(px); // Update Preview.
} async sliderChange(value: number, mode: SliderChangeMode) {
if ((mode === SliderChangeMode.End) && (value !== this.editState.currentAdjustData[this.currentAdjustType])) {
const roundedValue = Math.round(value);
this.editState.currentAdjustData[this.currentAdjustType] = roundedValue;
this.paramValues[this.currentAdjustType] = roundedValue;
this.deviceListDialogController.open();
try {
await this.pixelMapManager?.adjustImage(this.paramValues);
} catch (err) {
let error = err as BusinessError;
Logger.error('SliderCustom', `adjustImage failed: code = ${error?.code}, message = ${error?.message}`);
} finally {
this.deviceListDialogController.close();
}
}
} Slider({
value: this.editState.currentAdjustData[this.currentAdjustType],
step: CommonConstants.SLIDER_STEP,
min: this.min,
max: this.max,
// ...
})
// ...
.onChange((value: number, mode: SliderChangeMode) => {
this.sliderChange(value > this.max ? this.max : value, mode);
}) async adjustImage(currentAdjustData: number[]) {
// Obtain the cloned baseline pixel image.
const px = this.getStartEditPixelMap(EditType.ADJUST);
if (px === null || px === undefined) {
return;
}
let buffer = new ArrayBuffer(px.getPixelBytesNumber());
await px.readPixelsToBuffer(buffer).catch((err: BusinessError) => {
Logger.error(TAG, `readPixelsToBuffer failed: ${err.code}, ${err.message}`);
});
if (!buffer) {
this.releasePixelMap(px);
return;
}
// ...
if (currentAdjustData[AdjustType.OPACITY] !== CommonConstants.SLIDER_MAX) {
const opacity = currentAdjustData[AdjustType.OPACITY] / CommonConstants.SLIDER_MAX;
try {
await px.opacity(opacity).catch((err: BusinessError) => {
Logger.error(TAG, `opacity failed: ${err.code}, ${err.message}`);
});
} catch (err) {
let error = err as BusinessError;
Logger.error(TAG, `${error.code}, ${error.message}`);
}
}
this.finalEditPixelMap = this.replaceWith(this.finalEditPixelMap, px); // Save edited pixelMap.
this.notifyPreviewUpdate(px); // Update Preview.
} 通过调整像素与其灰度值的距离,改变色彩鲜艳程度。
对应公式:
luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b
adjustedR = luminance + (r - luminance) * factor
adjustedG = luminance + (g - luminance) * factor
adjustedB = luminance + (b - luminance) * factor
原理说明:
亮度系数:0.2126、0.7152、0.0722为ITU-R BT.709标准亮度转化系数。
function adjustSaturation(pixelData: Uint8ClampedArray, adjustedData: Uint8ClampedArray, i: number,
factor: number): void {
const r = pixelData[i] / 255;
const g = pixelData[i + 1] / 255;
const b = pixelData[i + 2] / 255;
const luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
const adjustedR = luminance + (r - luminance) * factor;
const adjustedG = luminance + (g - luminance) * factor;
const adjustedB = luminance + (b - luminance) * factor;
adjustedData[i] = Math.max(0, Math.min(255, Math.round(adjustedR * 255)));
adjustedData[i + 1] = Math.max(0, Math.min(255, Math.round(adjustedG * 255)));
adjustedData[i + 2] = Math.max(0, Math.min(255, Math.round(adjustedB * 255)));
} export function execColorInfo(bufferArray: ArrayBuffer, last: number, cur: number, hsvIndex: number) {
// ...
try {
const pixelData = new Uint8ClampedArray(bufferArray);
const adjustedData = new Uint8ClampedArray(pixelData.length);
const bytesPerPixel = 4;
const factor = cur / 100;
for (let i = 0; i < pixelData.length; i += bytesPerPixel) {
adjustedData[i + 3] = pixelData[i + 3];
if (pixelData[i + 3] < 1) {
continue;
}
// ...
if (hsvIndex === AdjustType.SATURATION) {
adjustSaturation(pixelData, adjustedData, i, factor);
}
}
return adjustedData.buffer;
} catch (error) {
Logger.error(TAG, `Failed to set adjustedData: code = ${error?.code}, message = ${error?.message}`);
return null;
}
} async adjustImage(currentAdjustData: number[]) {
// Obtain the cloned baseline pixel image.
const px = this.getStartEditPixelMap(EditType.ADJUST);
if (px === null || px === undefined) {
return;
}
let buffer = new ArrayBuffer(px.getPixelBytesNumber());
await px.readPixelsToBuffer(buffer).catch((err: BusinessError) => {
Logger.error(TAG, `readPixelsToBuffer failed: ${err.code}, ${err.message}`);
});
if (!buffer) {
this.releasePixelMap(px);
return;
}
// ...
if (currentAdjustData[AdjustType.SATURATION] !== CommonConstants.SLIDER_MAX) {
try {
buffer = await this.processAdjustWorker(
currentAdjustData[AdjustType.SATURATION],
buffer,
AdjustType.SATURATION
);
// Write the processed buffer back to Pixelmap.
px.writeBufferToPixelsSync(buffer);
} catch (err) {
let error = err as BusinessError;
Logger.error(TAG, `${error.code}, ${error.message}`);
}
}
// ...
this.finalEditPixelMap = this.replaceWith(this.finalEditPixelMap, px); // Save edited pixelMap.
this.notifyPreviewUpdate(px); // Update Preview.
} async handleFilter(type: FilterType) {
// Obtain the cloned baseline pixel image.
let startMap = this.getStartEditPixelMap(EditType.FILTER);
if (startMap === null || startMap === undefined) {
return;
}
let px: PixelMap = startMap;
try {
let filter = effectKit.createEffect(px); // Create filter object.
switch (type) {
case FilterType.GRAYSCALE:
const grayscaleResult = await filter.grayscale().getEffectPixelMap();
this.releasePixelMap(px);
px = grayscaleResult;
break;
// ...
default:
break;
}
this.finalEditPixelMap = this.replaceWith(this.finalEditPixelMap, px);
this.notifyPreviewUpdate(px);
} catch (err) {
let error = err as BusinessError;
Logger.error(TAG, `handleFilter failed: ${error.code}, ${error.message}`);
this.releasePixelMap(px);
}
} ForEach(this.filterData, (item: filterDataType, index: number) => {
Flex({
direction: this.currentBreakpoint === 'lg' ? FlexDirection.Row : FlexDirection.Column,
alignItems: ItemAlign.Center
}) {
Image(this.filterThumbnails[index])
// ...
Text(item.title)
// ...
}
// ...
.onClick(() => {
this.pixelMapManager?.handleFilter(index);
this.editState.currentFilterMode = index;
})
}, (item: filterDataType) => item.key) async handleFilter(type: FilterType) {
// Obtain the cloned baseline pixel image.
let startMap = this.getStartEditPixelMap(EditType.FILTER);
if (startMap === null || startMap === undefined) {
return;
}
let px: PixelMap = startMap;
try {
let filter = effectKit.createEffect(px); // Create filter object.
switch (type) {
// ...
case FilterType.BRIGHTNESS:
const brightnessResult = await filter.brightness(0.7).getEffectPixelMap();
this.releasePixelMap(px);
px = brightnessResult;
break;
// ...
default:
break;
}
this.finalEditPixelMap = this.replaceWith(this.finalEditPixelMap, px);
this.notifyPreviewUpdate(px);
} catch (err) {
let error = err as BusinessError;
Logger.error(TAG, `handleFilter failed: ${error.code}, ${error.message}`);
this.releasePixelMap(px);
}
} async handleFilter(type: FilterType) {
// Obtain the cloned baseline pixel image.
let startMap = this.getStartEditPixelMap(EditType.FILTER);
if (startMap === null || startMap === undefined) {
return;
}
let px: PixelMap = startMap;
try {
let filter = effectKit.createEffect(px); // Create filter object.
switch (type) {
// ...
case FilterType.INVERT:
const invertResult = await filter.invert().getEffectPixelMap();
this.releasePixelMap(px);
px = invertResult;
break;
// ...
default:
break;
}
this.finalEditPixelMap = this.replaceWith(this.finalEditPixelMap, px);
this.notifyPreviewUpdate(px);
} catch (err) {
let error = err as BusinessError;
Logger.error(TAG, `handleFilter failed: ${error.code}, ${error.message}`);
this.releasePixelMap(px);
}
} async handleFilter(type: FilterType) {
// Obtain the cloned baseline pixel image.
let startMap = this.getStartEditPixelMap(EditType.FILTER);
if (startMap === null || startMap === undefined) {
return;
}
let px: PixelMap = startMap;
try {
let filter = effectKit.createEffect(px); // Create filter object.
switch (type) {
// ...
case FilterType.BLUR:
const blurResult = await filter.blur(5).getEffectPixelMap();
this.releasePixelMap(px);
px = blurResult;
break;
default:
break;
}
this.finalEditPixelMap = this.replaceWith(this.finalEditPixelMap, px);
this.notifyPreviewUpdate(px);
} catch (err) {
let error = err as BusinessError;
Logger.error(TAG, `handleFilter failed: ${error.code}, ${error.message}`);
this.releasePixelMap(px);
}
}