图片处理
说明: 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 开发前请熟悉鸿蒙开发指导文档: gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md点击或者复制转到。
导入模块
import image from '@ohos.multimedia.image';
image.createPixelMap8+
createPixelMap(colors: ArrayBuffer, options: InitializationOptions): Promise<PixelMap>
通过属性创建PixelMap,通过Promise返回结果。
系统能力: SystemCapability.Multimedia.Image.Core
参数:
名称 | 类型 | 必填 | 说明 |
---|---|---|---|
colors | ArrayBuffer | 是 | BGRA_8888格式的颜色数组。 |
options | [InitializationOptions] | 是 | 创建像素的属性,包括透明度,尺寸,缩略值,像素格式和是否可编辑。 |
返回值:
类型 | 说明 |
---|---|
Promise<[PixelMap]> | 返回Pixelmap。 |
示例:
const color = new ArrayBuffer(96);
let bufferArr = new Unit8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts)
.then((pixelmap) => {
})
image.createPixelMap8+
createPixelMap(colors: ArrayBuffer, options: InitializationOptions, callback: AsyncCallback<PixelMap>): void
通过属性创建PixelMap,通过回调函数返回结果。
系统能力: SystemCapability.Multimedia.Image.Core
参数:
名称 | 类型 | 必填 | 说明 |
---|---|---|---|
colors | ArrayBuffer | 是 | BGRA_8888格式的颜色数组。 |
options | [InitializationOptions] | 是 | 属性。 |
callback | AsyncCallback<[PixelMap]> | 是 | 通过回调返回PixelMap对象。 |
示例:
const color = new ArrayBuffer(96);
let bufferArr = new Unit8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts, (pixelmap) => {
})
PixelMap7+
图像像素类,用于读取或写入图像数据以及获取图像信息。在调用PixelMap的方法前,需要先通过createPixelMap创建一个PixelMap实例。
属性
系统能力: SystemCapability.Multimedia.Image.Core
名称 | 类型 | 可读 | 可写 | 说明 |
---|---|---|---|---|
isEditable7+ | boolean | 是 | 否 | 设定是否图像像素可被编辑。 |
readPixelsToBuffer7+
readPixelsToBuffer(dst: ArrayBuffer): Promise<void>
读取图像像素数据,结果写入ArrayBuffer里,使用Promise形式返回。
系统能力: SystemCapability.Multimedia.Image.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
dst | ArrayBuffer | 是 | 缓冲区,函数执行结束后获取的图像像素数据写入到该内存区域内。 |
返回值:
类型 | 说明 |
---|---|
Promise<void> | Promise实例,用于获取结果,失败时返回错误信息。 |
示例:
const readBuffer = new ArrayBuffer(400);
pixelmap.readPixelsToBuffer(readBuffer).then(() => {
console.log('Succeeded in reading image pixel data.'); //符合条件则进入
}).catch(error => {
console.log('Failed to read image pixel data.'); //不符合条件则进入
})
readPixelsToBuffer7+
readPixelsToBuffer(dst: ArrayBuffer, callback: AsyncCallback<void>): void
读取图像像素数据,结果写入ArrayBuffer里,使用callback形式返回。
系统能力: SystemCapability.Multimedia.Image.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
dst | ArrayBuffer | 是 | 缓冲区,函数执行结束后获取的图像像素数据写入到该内存区域内。 |
callback | AsyncCallback<void> | 是 | 获取回调,失败时返回错误信息。 |
示例:
const readBuffer = new ArrayBuffer(400);
pixelmap.readPixelsToBuffer(readBuffer, (err, res) => {
if(err) {
console.log('Failed to read image pixel data.'); //不符合条件则进入
} else {
console.log('Succeeded in reading image pixel data.'); //符合条件则进入
}
})
readPixels7+
readPixels(area: PositionArea): Promise<void>
读取区域内的图片数据,使用Promise形式返回读取结果。
系统能力: SystemCapability.Multimedia.Image.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
area | [PositionArea] | 是 | 区域大小,根据区域读取。 |
返回值:
类型 | 说明 |
---|---|
Promise<void> | Promise实例,用于获取读取结果,失败时返回错误信息。 |
示例:
const area = new ArrayBuffer(400);
pixelmap.readPixels(area).then(() => {
console.log('Succeeded in reading the image data in the area.'); //符合条件则进入
}).catch(error => {
console.log('Failed to read the image data in the area.'); //不符合条件则进入
})
readPixels7+
readPixels(area: PositionArea, callback: AsyncCallback<void>): void
读取区域内的图片数据,使用callback形式返回读取结果。
系统能力: SystemCapability.Multimedia.Image.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
area | [PositionArea] | 是 | 区域大小,根据区域读取。 |
callback | AsyncCallback<void> | 是 | 获取回调,失败时返回错误信息。 |
示例:
const color = new ArrayBuffer(96);
let bufferArr = new Unit8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts, (err, pixelmap) => {
if(pixelmap == undefined){
console.info('createPixelMap failed.');
} else {
const area = { pixels: new ArrayBuffer(8),
offset: 0,
stride: 8,
region: { size: { height: 1, width: 2 }, x: 0, y: 0 }};
pixelmap.readPixels(area, () => {
console.info('readPixels success');
})
}
})
writePixels7+
writePixels(area: PositionArea): Promise<void>
将PixelMap写入指定区域内,使用Promise形式返回写入结果。
系统能力: SystemCapability.Multimedia.Image.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
area | [PositionArea] | 是 | 区域,根据区域写入。 |
返回值:
类型 | 说明 |
---|---|
Promise<void> | Promise实例,用于获取写入结果,失败时返回错误信息。 |
示例:
const color = new ArrayBuffer(96);
let bufferArr = new Unit8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts)
.then( pixelmap => {
if (pixelmap == undefined) {
console.info('createPixelMap failed.');
}
const area = { pixels: new ArrayBuffer(8),
offset: 0,
stride: 8,
region: { size: { height: 1, width: 2 }, x: 0, y: 0 }
}
let bufferArr = new Uint8Array(area.pixels);
for (var i = 0; i < bufferArr.length; i++) {
bufferArr[i] = i + 1;
}
pixelmap.writePixels(area).then(() => {
const readArea = { pixels: new ArrayBuffer(8),
offset: 0,
stride: 8,
// region.size.width + x < opts.width, region.size.height + y < opts.height
region: { size: { height: 1, width: 2 }, x: 0, y: 0 }
}
})
}).catch(error => {
console.log('error: ' + error);
})
writePixels7+
writePixels(area: PositionArea, callback: AsyncCallback<void>): void
将PixelMap写入指定区域内,使用callback形式返回写入结果。
系统能力: SystemCapability.Multimedia.Image.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
area | [PositionArea] | 是 | 区域,根据区域写入。 |
callback: | AsyncCallback<void> | 是 | 获取回调,失败时返回错误信息。 |
示例:
const area = new ArrayBuffer(400);
pixelmap.writePixels(area, (error) => {
if (error!=undefined) {
console.info('Failed to write pixelmap into the specified area.');
} else {
const readArea = {
pixels: new ArrayBuffer(20),
offset: 0,
stride: 8,
region: { size: { height: 1, width: 2 }, x: 0, y: 0 },
}
}
})
writeBufferToPixels7+
writeBufferToPixels(src: ArrayBuffer): Promise<void>
读取缓冲区中的图片数据,结果写入PixelMap中,使用Promise形式返回。
系统能力: SystemCapability.Multimedia.Image.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
src | ArrayBuffer | 是 | 图像像素数据。 |
返回值:
类型 | 说明 |
---|---|
Promise<void> | Promise实例,用于获取结果,失败时返回错误信息。 |
示例:
const color = new ArrayBuffer(96);
const pixelMap = new ArrayBuffer(400);
let bufferArr = new Unit8Array(color);
pixelMap.writeBufferToPixels(color).then(() => {
console.log("Succeeded in writing data from a buffer to a PixelMap.");
}).catch((err) => {
console.error("Failed to write data from a buffer to a PixelMap.");
})
writeBufferToPixels7+
writeBufferToPixels(src: ArrayBuffer, callback: AsyncCallback<void>): void
读取缓冲区中的图片数据,结果写入PixelMap中,使用callback形式返回。
系统能力: SystemCapability.Multimedia.Image.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
src | ArrayBuffer | 是 | 图像像素数据。 |
callback | AsyncCallback<void> | 是 | 获取回调,失败时返回错误信息。 |
示例:
const color = new ArrayBuffer(96);\
const pixelMap = new ArrayBuffer(400);
let bufferArr = new Unit8Array(color);
pixelMap.writeBufferToPixels(color, function(err) {
if (err) {
console.error("Failed to write data from a buffer to a PixelMap.");
return;
} else {
console.log("Succeeded in writing data from a buffer to a PixelMap.");
}
});
getImageInfo7+
getImageInfo(): Promise<ImageInfo>
获取图像像素信息,使用Promise形式返回获取的图像像素信息。
系统能力: SystemCapability.Multimedia.Image.Core
返回值:
类型 | 说明 |
---|---|
Promise<[ImageInfo]> | Promise实例,用于异步获取图像像素信息,失败时返回错误信息。 |
示例:
const pixelMap = new ArrayBuffer(400);
pixelMap.getImageInfo().then(function(info) {
console.log("Succeeded