鸿蒙开发接口媒体:【@ohos.multimedia.image (图片处理)】

 图片处理

说明:  本模块首批接口从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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值