HarmonyOS实战开发:@ohos.multimedia.image (图片处理)

本模块提供图片处理效果,包括通过属性创建PixelMap、读取图像像素数据、读取区域内的图片数据等。

说明: 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import image from '@ohos.multimedia.image';复制到剪贴板错误复制

image.createPixelMap8+

createPixelMap(colors: ArrayBuffer, options: InitializationOptions): Promise<PixelMap>

通过属性创建PixelMap,默认采用BGRA_8888格式处理数据,通过Promise返回结果。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
colorsArrayBufferBGRA_8888格式的颜色数组。
选项InitializationOptions创建像素的属性,包括透明度,尺寸,缩略值,像素格式和是否可编辑。

返回值:

类型说明
Promise<PixelMap>返回Pixelmap。
当创建的pixelmap大小超过原图大小时,返回原图pixelmap大小。

示例:

import {BusinessError} from '@ohos.base';
const color : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
let opts : image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts).then((pixelMap : image.PixelMap) => {
  console.log('Succeeded in creating pixelmap.');
}).catch((error : BusinessError) => {
  console.error('Failed to create pixelmap.');
})复制到剪贴板错误复制

image.createPixelMap8+

createPixelMap(colors: ArrayBuffer, options: InitializationOptions, callback: AsyncCallback<PixelMap>): void

通过属性创建PixelMap,默认采用BGRA_8888格式处理数据,通过回调函数返回结果。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
colorsArrayBufferBGRA_8888格式的颜色数组。
选项InitializationOptions属性。
回调AsyncCallback<PixelMap>通过回调返回PixelMap对象。

示例:

import {BusinessError} from '@ohos.base';
const color : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
let opts : image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts, (error : BusinessError, pixelMap : image.PixelMap) => {
    if(error) {
        console.error('Failed to create pixelmap.');
    } else {
        console.log('Succeeded in creating pixelmap.');
    }
})复制到剪贴板错误复制

PixelMap7+

图像像素类,用于读取或写入图像数据以及获取图像信息。在调用PixelMap的方法前,需要先通过createPixelMap创建一个PixelMap实例。目前pixelmap序列化大小最大128MB,超过会送显失败。大小计算方式为(宽*高*每像素占用字节数)。 从API version 11开始,PixelMap支持通过worker跨线程调用。当PixelMap通过Worker跨线程后,原线程的PixelMap的所有接口均不能调用,否则将报错501 服务器不具备完成请求的功能。

属性

系统能力: SystemCapability.Multimedia.Image.Core

名称类型可读可写说明
isEditable布尔设定是否图像像素可被编辑。

readPixelsToBuffer7+

readPixelsToBuffer(dst: ArrayBuffer): Promise<void>

读取图像像素数据,结果写入ArrayBuffer里,使用Promise形式返回。指定BGRA_8888格式创建pixelmap,读取的像素数据与原数据保持一致。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
dstArrayBuffer缓冲区,函数执行结束后获取的图像像素数据写入到该内存区域内。缓冲区大小由getPixelBytesNumber接口获取。

返回值:

类型说明
Promise<void>Promise实例,用于获取结果,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
const readBuffer : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
pixelMap.readPixelsToBuffer(readBuffer).then(() => {
    console.log('Succeeded in reading image pixel data.');  //符合条件则进入 
}).catch((error : BusinessError) => {
    console.error('Failed to read image pixel data.');  //不符合条件则进入
})Copy to clipboardErrorCopied

readPixelsToBuffer7+

readPixelsToBuffer(dst: ArrayBuffer, callback: AsyncCallback<void>): void

读取图像像素数据,结果写入ArrayBuffer里,使用callback形式返回。指定BGRA_8888格式创建pixelmap,读取的像素数据与原数据保持一致。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
dstArrayBuffer缓冲区,函数执行结束后获取的图像像素数据写入到该内存区域内。缓冲区大小由getPixelBytesNumber接口获取。
回调AsyncCallback<void>获取回调,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
const readBuffer : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
pixelMap.readPixelsToBuffer(readBuffer, (err : BusinessError, res : void) => {
    if(err) {
        console.error('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

参数:

参数名类型必填说明
areaPositionArea区域大小,根据区域读取。

返回值:

类型说明
承诺<无效>Promise实例,用于获取读取结果,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
const area : image.PositionArea = {
    pixels: new ArrayBuffer(8),
    offset: 0,
    stride: 8,
    region: { size: { height: 1, width: 2 }, x: 0, y: 0 }
}
pixelMap.readPixels(area).then(() => {
    console.log('Succeeded in reading the image data in the area.'); //符合条件则进入
}).catch((error : BusinessError) => {
    console.error('Failed to read the image data in the area.'); //不符合条件则进入
})复制到剪贴板错误复制

readPixels7+

readPixels(area: PositionArea, callback: AsyncCallback<void>): void

读取区域内的图片数据,使用callback形式返回读取结果。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
areaPositionArea区域大小,根据区域读取。
回调AsyncCallback<void>获取回调,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
const color : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
let opts : image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts, (err : BusinessError, pixelMap : image.PixelMap) => {
    if(pixelMap == undefined){
        console.error('createPixelMap failed.');
    } else {
        const area : image.PositionArea = { pixels: new ArrayBuffer(8),
            offset: 0,
            stride: 8,
            region: { size: { height: 1, width: 2 }, x: 0, y: 0 }};
        pixelMap.readPixels(area, (err : BusinessError) => {
            if (err != undefined) {
            console.error('Failed to read pixelmap from the specified area.');
        } else {
            console.info('Succeeded to read pixelmap from the specified area.');
        }
        })
    }
})复制到剪贴板错误复制

writePixels7+

writePixels(area: PositionArea): Promise<void>

将PixelMap写入指定区域内,使用Promise形式返回写入结果。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
areaPositionArea区域,根据区域写入。

返回值:

类型说明
Promise<void>Promise实例,用于获取写入结果,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
const color : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
let opts : image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts)
    .then( (pixelMap : image.PixelMap)  => {
        if (pixelMap == undefined) {
            console.error('createPixelMap failed.');
            return;
        }
        const area : image.PositionArea = { pixels: new ArrayBuffer(8),
            offset: 0,
            stride: 8,
            region: { size: { height: 1, width: 2 }, x: 0, y: 0 }
        }
        let bufferArr : Uint8Array = new Uint8Array(area.pixels);
        for (let i = 0; i < bufferArr.length; i++) {
            bufferArr[i] = i + 1;
        }
        pixelMap.writePixels(area).then(() => {
            console.info('Succeeded to write pixelmap into the specified area.');
        }).catch((error : BusinessError) => {
            console.error(`Failed to write pixelmap into the specified area. code is ${error.code}, message is ${error.message}`);
        })
    }).catch((error : BusinessError) => {
        console.error('error: ' + error);
    })Copy to clipboardErrorCopied

writePixels7+

writePixels(area: PositionArea, callback: AsyncCallback<void>): void

将PixelMap写入指定区域内,使用callback形式返回写入结果。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
areaPositionArea区域,根据区域写入。
callbackAsyncCallback<void>获取回调,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
const area : image.PositionArea = { pixels: new ArrayBuffer(8),
    offset: 0,
    stride: 8,
    region: { size: { height: 1, width: 2 }, x: 0, y: 0 }
}
let bufferArr : Uint8Array = new Uint8Array(area.pixels);
for (let i = 0; i < bufferArr.length; i++) {
    bufferArr[i] = i + 1;
}
pixelMap.writePixels(area, (error : BusinessError) => {
    if (error != undefined) {
        console.error('Failed to write pixelmap into the specified area.');
    } else {
        console.info('Succeeded to write pixelmap into the specified area.');
    }
})Copy to clipboardErrorCopied

writeBufferToPixels7+

writeBufferToPixels(src: ArrayBuffer): Promise<void>

读取缓冲区中的图片数据,结果写入PixelMap中,使用Promise形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
srcArrayBuffer图像像素数据。

返回值:

类型说明
Promise<void>Promise实例,用于获取结果,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
const color : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
let bufferArr : Uint8Array = new Uint8Array(color);
for (let i = 0; i < bufferArr.length; i++) {
    bufferArr[i] = i + 1;
}
pixelMap.writeBufferToPixels(color).then(() => {
    console.log("Succeeded in writing data from a buffer to a PixelMap.");
}).catch((error : BusinessError) => {
    console.error("Failed to write data from a buffer to a PixelMap.");
})Copy to clipboardErrorCopied

writeBufferToPixels7+

writeBufferToPixels(src: ArrayBuffer, callback: AsyncCallback<void>): void

读取缓冲区中的图片数据,结果写入PixelMap中,使用callback形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
srcArrayBuffer图像像素数据。
callbackAsyncCallback<void>获取回调,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
const color : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
let bufferArr : Uint8Array = new Uint8Array(color);
for (let i = 0; i < bufferArr.length; i++) {
    bufferArr[i] = i + 1;
}
pixelMap.writeBufferToPixels(color, (err : BusinessError) => {
    if (err != undefined) {
        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.");
    }
});Copy to clipboardErrorCopied

getImageInfo7+

getImageInfo(): Promise<ImageInfo>

获取图像像素信息,使用Promise形式返回获取的图像像素信息。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型说明
Promise<ImageInfo>Promise实例,用于异步获取图像像素信息,失败时返回错误信息。

示例:

const color : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
let opts : image.InitializationOptions = { editable: true, pixelFormat: 2, size: { height: 6, width: 8 } }
image.createPixelMap(color, opts).then((pixelMap : image.PixelMap) => {
    if (pixelMap == undefined) {
        console.error("Failed to obtain the image pixel map information.");
    }
    pixelMap.getImageInfo().then((imageInfo : image.ImageInfo) => {
        if (imageInfo == undefined) {
            console.error("Failed to obtain the image pixel map information.");
        }
        if (imageInfo.size.height == 4 && imageInfo.size.width == 6) {
            console.log("Succeeded in obtaining the image pixel map information.");
        }
    })
})复制到剪贴板错误复制

getImageInfo7+

getImageInfo(callback: AsyncCallback<ImageInfo>): void

获取图像像素信息,使用callback形式返回获取的图像像素信息。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
回调AsyncCallback<ImageInfo>获取图像像素信息回调,异步返回图像像素信息,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
const color : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
let opts : image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts, (err : BusinessError, pixelMap : image.PixelMap) => {
    if (pixelMap == undefined) {
        console.error("Failed to obtain the image pixel map information.");
    }
    pixelMap.getImageInfo((err : BusinessError, imageInfo : image.ImageInfo) => {
        if (imageInfo == undefined) {
            console.error("Failed to obtain the image pixel map information.");
        }
        if (imageInfo.size.height == 4 && imageInfo.size.width == 6) {
            console.log("Succeeded in obtaining the image pixel map information.");
        }
    })
})复制到剪贴板错误复制

getBytesNumberPerRow7+

getBytesNumberPerRow(): number

获取图像像素每行字节数。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型说明
图像像素的行字节数。

示例:

import {BusinessError} from '@ohos.base';
const color : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
let opts : image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts, (err : BusinessError, pixelMap : image.PixelMap) => {
    let rowCount : number = pixelMap.getBytesNumberPerRow();
})复制到剪贴板错误复制

getPixelBytesNumber7+

getPixelBytesNumber(): number

获取图像像素的总字节数。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型说明
number图像像素的总字节数。

示例:

let pixelBytesNumber : number = pixelMap.getPixelBytesNumber();Copy to clipboardErrorCopied

getDensity9+

getDensity():number

获取当前图像像素的密度。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型说明
number图像像素的密度。

示例:

let getDensity : number = pixelMap.getDensity();Copy to clipboardErrorCopied

opacity9+

opacity(rate: number, callback: AsyncCallback<void>): void

通过设置透明比率来让PixelMap达到对应的透明效果,使用callback形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
ratenumber透明比率的值。
callbackAsyncCallback<void>获取回调,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
let rate = 0.5;
pixelMap.opacity(rate, (err : BusinessError) => {
    if (err) {
        console.error("Failed to set opacity.");
        return;
    } else {
        console.log("Succeeded in setting opacity.");
    }
})Copy to clipboardErrorCopied

opacity9+

opacity(rate: number): Promise<void>

通过设置透明比率来让PixelMap达到对应的透明效果,使用Promise形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
ratenumber透明比率的值。

返回值:

类型说明
Promise<void>Promise实例,用于获取结果,失败时返回错误信息。

示例:

async function Demo() {
    await pixelMap.opacity(0.5);
}Copy to clipboardErrorCopied

createAlphaPixelmap9+

createAlphaPixelmap(): Promise<PixelMap>

根据Alpha通道的信息,来生成一个仅包含Alpha通道信息的pixelmap,可用于阴影效果,使用Promise形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型说明
Promise<PixelMap>Promise实例,返回pixelmap。

示例:

async function Demo() {
    await pixelMap.createAlphaPixelmap();
}   Copy to clipboardErrorCopied

createAlphaPixelmap9+

createAlphaPixelmap(callback: AsyncCallback<PixelMap>): void

根据Alpha通道的信息,来生成一个仅包含Alpha通道信息的pixelmap,可用于阴影效果,使用callback形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
callbackAsyncCallback<PixelMap>获取回调,异步返回结果。

示例:

import {BusinessError} from '@ohos.base';
pixelMap.createAlphaPixelmap((err : BusinessError, alphaPixelMap : image.PixelMap) => {
    if (alphaPixelMap == undefined) {
        console.error('Failed to obtain new pixel map.');
    } else {
        console.info('Succeed in obtaining new pixel map.');
    }
})Copy to clipboardErrorCopied

scale9+

scale(x: number, y: number, callback: AsyncCallback<void>): void

根据输入的宽高对图片进行缩放,使用callback形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
xnumber宽度的缩放值,其值为输入的倍数。
ynumber高度的缩放值,其值为输入的倍数。
callbackAsyncCallback<void>获取回调,失败时返回错误信息。

示例:

async function Demo() {
    await pixelMap.scale(2.0, 1.0);
}Copy to clipboardErrorCopied

scale9+

scale(x: number, y: number): Promise<void>

根据输入的宽高对图片进行缩放,使用Promise形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
xnumber宽度的缩放值,其值为输入的倍数。
ynumber高度的缩放值,其值为输入的倍数。

返回值:

类型说明
Promise<void>Promise实例,异步返回结果。

示例:

async function Demo() {
    await pixelMap.scale(2.0, 1.0);
}Copy to clipboardErrorCopied

translate9+

translate(x: number, y: number, callback: AsyncCallback<void>): void

根据输入的坐标对图片进行位置变换,使用callback形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
xnumber区域横坐标。
ynumber区域纵坐标。
callbackAsyncCallback<void>获取回调,失败时返回错误信息。

示例:

async function Demo() {
    await pixelMap.translate(3.0, 1.0);
}Copy to clipboardErrorCopied

translate9+

translate(x: number, y: number): Promise<void>

根据输入的坐标对图片进行位置变换,使用Promise形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
xnumber区域横坐标。
ynumber区域纵坐标。

返回值:

类型说明
Promise<void>Promise实例,异步返回结果。

示例:

async function Demo() {
    await pixelMap.translate(3.0, 1.0);
}Copy to clipboardErrorCopied

rotate9+

rotate(angle: number, callback: AsyncCallback<void>): void

根据输入的角度对图片进行旋转,使用callback形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
anglenumber图片旋转的角度。
callbackAsyncCallback<void>获取回调,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
let angle = 90.0;
pixelMap.rotate(angle, (err : BusinessError) => {
    if (err != undefined) {
        console.error("Failed to set rotation.");
        return;
    } else {
        console.log("Succeeded in setting rotation.");
    }
})Copy to clipboardErrorCopied

rotate9+

rotate(angle: number): Promise<void>

根据输入的角度对图片进行旋转,使用Promise形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
anglenumber图片旋转的角度。

返回值:

类型说明
Promise<void>Promise实例,异步返回结果。

示例:

async function Demo() {
    await pixelMap.rotate(90.0);
}Copy to clipboardErrorCopied

flip9+

flip(horizontal: boolean, vertical: boolean, callback: AsyncCallback<void>): void

根据输入的条件对图片进行翻转,使用callback形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
horizontalboolean水平翻转。
verticalboolean垂直翻转。
callbackAsyncCallback<void>获取回调,失败时返回错误信息。

示例:

async function Demo() {
    await pixelMap.flip(false, true);
}Copy to clipboardErrorCopied

flip9+

flip(horizontal: boolean, vertical: boolean): Promise<void>

根据输入的条件对图片进行翻转,使用Promise形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
horizontalboolean水平翻转。
verticalboolean垂直翻转。

返回值:

类型说明
Promise<void>Promise实例,异步返回结果。

示例:

async function Demo() {
    await pixelMap.flip(false, true);
}复制到剪贴板错误复制

crop9+

crop(region: Region, callback: AsyncCallback<void>): void

根据输入的尺寸对图片进行裁剪,使用callback形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
regionRegion裁剪的尺寸。
回调AsyncCallback<void>获取回调,失败时返回错误信息。

示例:

async function Demo() {
    await pixelMap.crop({ x: 0, y: 0, size: { height: 100, width: 100 } } as image.Region);
}复制到剪贴板错误复制

crop9+

crop(region: Region): Promise<void>

根据输入的尺寸对图片进行裁剪,使用Promise形式返回。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
regionRegion裁剪的尺寸。

返回值:

类型说明
承诺<无效>Promise实例,异步返回结果。

示例:

async function Demo() {
    await pixelMap.crop({ x: 0, y: 0, size: { height: 100, width: 100 } } as image.Region);
}复制到剪贴板错误复制

getColorSpace10+

getColorSpace(): colorSpaceManager.ColorSpaceManager

获取图像广色域信息。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型说明
colorSpaceManager.ColorSpaceManager图像广色域信息。

错误码:

以下错误码的详细介绍请参见Image错误码

错误码ID错误信息
62980101If the image data abnormal
62980103If the image data unsupport
62980115If the image parameter invalid

示例:

import colorSpaceManager from '@ohos.graphics.colorSpaceManager';
async function Demo() {
    let csm : Object = pixelMap.getColorSpace();
}Copy to clipboardErrorCopied

setColorSpace10+

setColorSpace(colorSpace: colorSpaceManager.ColorSpaceManager): void

设置图像广色域信息。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
colorSpacecolorSpaceManager.ColorSpaceManager图像广色域信息。

错误码:

以下错误码的详细介绍请参见Image错误码

错误码ID错误信息
62980111If the image source data incomplete
62980115If the image parameter invalid

示例:

import colorSpaceManager from '@ohos.graphics.colorSpaceManager';
async function Demo() {
    let colorSpaceName = colorSpaceManager.ColorSpace.SRGB;
    let csm : colorSpaceManager.ColorSpaceManager = colorSpaceManager.create(colorSpaceName);
    pixelMap.setColorSpace(csm);
}Copy to clipboardErrorCopied

marshalling10+

marshalling(sequence: rpc.MessageSequence): void

将PixelMap序列化后写入MessageSequence。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
sequencerpc.MessageSequence新创建的MessageSequence。

错误码:

以下错误码的详细介绍请参见Image错误码

错误码ID错误信息
62980115If the input parameter invalid
62980097If the ipc error

示例:

import image from '@ohos.multimedia.image';
import rpc from '@ohos.rpc';
class MySequence implements rpc.Parcelable {
    pixel_map : image.PixelMap;
    constructor(conPixelMap : image.PixelMap) {
        this.pixel_map = conPixelMap;
    }
    marshalling(messageSequence : rpc.MessageSequence) {
        this.pixel_map.marshalling(messageSequence);
        console.log('marshalling');
        return true;
    }
    unmarshalling(messageSequence : rpc.MessageSequence) {
      image.createPixelMap(new ArrayBuffer(96), {size: { height:4, width: 6}}).then((pixelParcel : image.PixelMap) => {
        pixelParcel.unmarshalling(messageSequence).then(async (pixelMap : image.PixelMap) => {
          this.pixel_map = pixelMap;
          await pixelMap.getImageInfo().then((imageInfo : image.ImageInfo) => {
            console.log("unmarshalling information h:" + imageInfo.size.height + "w:" + imageInfo.size.width);
          })
        })
      });
      return true;
    }
}
async function Demo() {
   const color : ArrayBuffer = new ArrayBuffer(96);
   let bufferArr : Uint8Array = new Uint8Array(color);
   for (let i = 0; i < bufferArr.length; i++) {
      bufferArr[i] = 0x80;
   }
   let opts : image.InitializationOptions = {
      editable: true,
      pixelFormat: 4,
      size: { height: 4, width: 6 },
      alphaType: 3
   }
   let pixelMap : image.PixelMap | undefined = undefined;
   await image.createPixelMap(color, opts).then((srcPixelMap : image.PixelMap) => {
      pixelMap = srcPixelMap;
   })
   if (pixelMap != undefined) {
     // 序列化
     let parcelable : MySequence = new MySequence(pixelMap);
     let data : rpc.MessageSequence = rpc.MessageSequence.create();
     data.writeParcelable(parcelable);


     // 反序列化 rpc获取到data
     let ret : MySequence = new MySequence(pixelMap);
     data.readParcelable(ret);
   }
}Copy to clipboardErrorCopied

unmarshalling10+

unmarshalling(sequence: rpc.MessageSequence): Promise<PixelMap>

从MessageSequence中获取PixelMap。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
sequencerpc.MessageSequence保存有PixelMap信息的MessageSequence。

返回值:

类型说明
Promise<PixelMap>异步返回Promise对象。

错误码:

以下错误码的详细介绍请参见Image错误码

错误码ID错误信息
62980115If the input parameter invalid
62980097If the ipc error
62980096If the operation failed

示例:

import image from '@ohos.multimedia.image';
import rpc from '@ohos.rpc';
class MySequence implements rpc.Parcelable {
    pixel_map : image.PixelMap;
    constructor(conPixelMap : image.PixelMap) {
        this.pixel_map = conPixelMap;
    }
    marshalling(messageSequence : rpc.MessageSequence) {
        this.pixel_map.marshalling(messageSequence);
        console.log('marshalling');
        return true;
    }
    unmarshalling(messageSequence : rpc.MessageSequence) {
      image.createPixelMap(new ArrayBuffer(96), {size: { height:4, width: 6}}).then((pixelParcel : image.PixelMap) => {
        pixelParcel.unmarshalling(messageSequence).then(async (pixelMap : image.PixelMap) => {
          this.pixel_map = pixelMap;
          await pixelMap.getImageInfo().then((imageInfo : image.ImageInfo) => {
            console.log("unmarshalling information h:" + imageInfo.size.height + "w:" + imageInfo.size.width);
          })
        })
      });
      return true;
    }
}
async function Demo() {
   const color : ArrayBuffer = new ArrayBuffer(96);
   let bufferArr : Uint8Array = new Uint8Array(color);
   for (let i = 0; i < bufferArr.length; i++) {
      bufferArr[i] = 0x80;
   }
   let opts : image.InitializationOptions = {
      editable: true,
      pixelFormat: 4,
      size: { height: 4, width: 6 },
      alphaType: 3
   }
   let pixelMap : image.PixelMap | undefined = undefined;
   await image.createPixelMap(color, opts).then((srcPixelMap : image.PixelMap) => {
      pixelMap = srcPixelMap;
   })
   if (pixelMap != undefined) {
     // 序列化
     let parcelable : MySequence = new MySequence(pixelMap);
     let data : rpc.MessageSequence = rpc.MessageSequence.create();
     data.writeParcelable(parcelable);


     // 反序列化 rpc获取到data
     let ret : MySequence = new MySequence(pixelMap);
     data.readParcelable(ret);
   }
}Copy to clipboardErrorCopied

release7+

release():Promise<void>

释放PixelMap对象,使用Promise形式返回释放结果。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型说明
Promise<void>Promise实例,异步返回释放结果。

示例:

import {BusinessError} from '@ohos.base';
pixelMap.release().then(() => {
    console.log('Succeeded in releasing pixelmap object.');
}).catch((error : BusinessError) => {
    console.error('Failed to release pixelmap object.');
})Copy to clipboardErrorCopied

release7+

release(callback: AsyncCallback<void>): void

释放PixelMap对象,使用callback形式返回释放结果。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
回调AsyncCallback<void>异步返回释放结果。

示例:

import {BusinessError} from '@ohos.base';
pixelMap.release((err : BusinessError) => {
    if (err != undefined) {
        console.error('Failed to release pixelmap object.');
    } else {
        console.log('Succeeded in releasing pixelmap object.');
    }
})复制到剪贴板错误复制

image.createImageSource

createImageSource(uri: string): ImageSource

通过传入的uri创建图片源实例。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
uri字符串图片路径,当前仅支持应用沙箱路径。
当前支持格式有:.jpg .png .gif .bmp .webp RAW SVG10+

返回值:

类型说明
ImageSource返回ImageSource类实例,失败时返回undefined。

示例:

//Stage模型
const context : Context = getContext(this);
const path : string = context.cacheDir + "/test.jpg";
const imageSourceApi : image.ImageSource = image.createImageSource(path);复制到剪贴板错误复制
//FA模型
import featureAbility from '@ohos.ability.featureAbility';

const context : featureAbility.Context = featureAbility.getContext();
const path : string = context.getCacheDir() + "/test.jpg";
const imageSourceApi : image.ImageSource = image.createImageSource(path);复制到剪贴板错误复制

image.createImageSource9+

createImageSource(uri: string, options: SourceOptions): ImageSource

通过传入的uri创建图片源实例。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
uri字符串图片路径,当前仅支持应用沙箱路径。
当前支持格式有:.jpg .png .gif .bmp .webp RAW SVG10+
选项SourceOptions图片属性,包括图片序号与默认属性值。

返回值:

类型说明
ImageSource返回ImageSource类实例,失败时返回undefined。

示例:

let sourceOptions : image.SourceOptions = { sourceDensity: 120 };
let imageSource : image.ImageSource = image.createImageSource('test.png', sourceOptions);Copy to clipboardErrorCopied

image.createImageSource7+

createImageSource(fd: number): ImageSource

通过传入文件描述符来创建图片源实例。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
fdnumber文件描述符fd。

返回值:

类型说明
ImageSource返回ImageSource类实例,失败时返回undefined。

示例:

const imageSourceApi : image.ImageSource = image.createImageSource(0);Copy to clipboardErrorCopied

image.createImageSource9+

createImageSource(fd: number, options: SourceOptions): ImageSource

通过传入文件描述符来创建图片源实例。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
fdnumber文件描述符fd。
optionsSourceOptions图片属性,包括图片序号与默认属性值。

返回值:

类型说明
ImageSource返回ImageSource类实例,失败时返回undefined。

示例:

let sourceOptions : image.SourceOptions = { sourceDensity: 120 };
const imageSourceApi : image.ImageSource = image.createImageSource(0, sourceOptions);Copy to clipboardErrorCopied

image.createImageSource9+

createImageSource(buf: ArrayBuffer): ImageSource

通过缓冲区创建图片源实例。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
bufArrayBuffer图像缓冲区数组。

返回值:

类型说明
ImageSource返回ImageSource类实例,失败时返回undefined。

示例:

const buf : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
const imageSourceApi : image.ImageSource = image.createImageSource(buf);Copy to clipboardErrorCopied

image.createImageSource9+

createImageSource(buf: ArrayBuffer, options: SourceOptions): ImageSource

通过缓冲区创建图片源实例。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
bufArrayBuffer图像缓冲区数组。
optionsSourceOptions图片属性,包括图片序号与默认属性值。

返回值:

类型说明
ImageSource返回ImageSource类实例,失败时返回undefined。

示例:

const data : ArrayBuffer= new ArrayBuffer(112);
let sourceOptions : image.SourceOptions = { sourceDensity: 120 };
const imageSourceApi : image.ImageSource = image.createImageSource(data, sourceOptions);Copy to clipboardErrorCopied

image.CreateIncrementalSource9+

CreateIncrementalSource(buf: ArrayBuffer): ImageSource

通过缓冲区以增量的方式创建图片源实例。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
bufArrayBuffer增量数据。

返回值:

类型说明
ImageSource返回图片源,失败时返回undefined。

示例:

const buf : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
const imageSourceIncrementalSApi : image.ImageSource = image.CreateIncrementalSource(buf);Copy to clipboardErrorCopied

image.CreateIncrementalSource9+

CreateIncrementalSource(buf: ArrayBuffer, options?: SourceOptions): ImageSource

通过缓冲区以增量的方式创建图片源实例。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
bufArrayBuffer增量数据。
optionsSourceOptions图片属性,包括图片序号与默认属性值。

返回值:

类型说明
ImageSource返回图片源,失败时返回undefined。

示例:

const buf : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
const imageSourceIncrementalSApi : image.ImageSource = image.CreateIncrementalSource(buf);Copy to clipboardErrorCopied

ImageSource

图片源类,用于获取图片相关信息。在调用ImageSource的方法前,需要先通过createImageSource构建一个ImageSource实例。

属性

系统能力: SystemCapability.Multimedia.Image.ImageSource

名称类型可读可写说明
supportedFormatsArray<string>支持的图片格式,包括:png,jpeg,bmp,gif,webp,RAW。

getImageInfo

getImageInfo(index: number, callback: AsyncCallback<ImageInfo>): void

获取指定序号的图片信息,使用callback形式返回图片信息。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
indexnumber创建图片源时的序号。
callbackAsyncCallback<ImageInfo>获取图片信息回调,异步返回图片信息对象。

示例:

import {BusinessError} from '@ohos.base';
imageSourceApi.getImageInfo(0,(error : BusinessError, imageInfo : image.ImageInfo) => { 
    if(error) {
        console.error('getImageInfo failed.');
    } else {
        console.log('getImageInfo succeeded.');
    }
})Copy to clipboardErrorCopied

getImageInfo

getImageInfo(callback: AsyncCallback<ImageInfo>): void

获取图片信息,使用callback形式返回图片信息。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
callbackAsyncCallback<ImageInfo>获取图片信息回调,异步返回图片信息对象。

示例:

import {BusinessError} from '@ohos.base';
imageSourceApi.getImageInfo((err : BusinessError, imageInfo : image.ImageInfo) => { 
    if (err != undefined) {
        console.error(`Failed to obtaining the image information.code is ${err.code}, message is ${err.message}`);
    } else {
        console.log('Succeeded in obtaining the image information.');
    }
})Copy to clipboardErrorCopied

getImageInfo

getImageInfo(index?: number): Promise<ImageInfo>

获取图片信息,使用Promise形式返回图片信息。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
indexnumber创建图片源时的序号,不选择时默认为0。

返回值:

类型说明
Promise<ImageInfo>返回获取到的图片信息。

示例:

import {BusinessError} from '@ohos.base';
imageSourceApi.getImageInfo(0)
    .then((imageInfo : image.ImageInfo) => {
        console.log('Succeeded in obtaining the image information.');
    }).catch((error : BusinessError) => {
        console.error('Failed to obtain the image information.');
    })Copy to clipboardErrorCopied

getImageProperty7+

getImageProperty(key:string, options?: GetImagePropertyOptions): Promise<string>

获取图片中给定索引处图像的指定属性键的值,用Promise形式返回结果,仅支持JPEG文件,且需要包含exif信息。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
key字符串图片属性名。
选项GetImagePropertyOptions图片属性,包括图片序号与默认属性值。

返回值:

类型说明
Promise<string>Promise实例,用于异步获取图片属性值,如获取失败则返回属性默认值。

示例:

imageSourceApi.getImageProperty("BitsPerSample")
    .then((data : string) => {
        console.log('Succeeded in getting the value of the specified attribute key of the image.');
    })复制到剪贴板错误复制

getImageProperty7+

getImageProperty(key:string, callback: AsyncCallback<string>): void

获取图片中给定索引处图像的指定属性键的值,用callback形式返回结果,仅支持JPEG文件,且需要包含exif信息。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
key字符串图片属性名。
回调AsyncCallback<string>获取图片属性回调,返回图片属性值,如获取失败则返回属性默认值。

示例:

import {BusinessError} from '@ohos.base';
imageSourceApi.getImageProperty("BitsPerSample",(error : BusinessError, data : string) => { 
    if(error) {
        console.error('Failed to get the value of the specified attribute key of the image.');
    } else {
        console.log('Succeeded in getting the value of the specified attribute key of the image.');
    }
})复制到剪贴板错误复制

getImageProperty7+

getImageProperty(key:string, options: GetImagePropertyOptions, callback: AsyncCallback<string>): void

获取图片指定属性键的值,callback形式返回结果,仅支持JPEG文件,且需要包含exif信息。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
key字符串图片属性名。
选项GetImagePropertyOptions图片属性,包括图片序号与默认属性值。
回调AsyncCallback<string>获取图片属性回调,返回图片属性值,如获取失败则返回属性默认值。

示例:

import {BusinessError} from '@ohos.base';
let property : image.GetImagePropertyOptions = { index: 0, defaultValue: '9999' }
imageSourceApi.getImageProperty("BitsPerSample",property,(error : BusinessError, data : string) => { 
    if(error) {
        console.error('Failed to get the value of the specified attribute key of the image.');
    } else {
        console.log('Succeeded in getting the value of the specified attribute key of the image.');
    }
})复制到剪贴板错误复制

modifyImageProperty9+

modifyImageProperty(key: string, value: string): Promise<void>

通过指定的键修改图片属性的值,使用Promise形式返回结果,仅支持JPEG文件,且需要包含exif信息。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
keystring图片属性名。
valuestring属性值。

返回值:

类型说明
Promise<void>Promise实例,异步返回结果。

示例:

imageSourceApi.modifyImageProperty("ImageWidth", "120").then(() => {
    imageSourceApi.getImageProperty("ImageWidth").then((width : string) => {
        console.info(`ImageWidth is :${width}`);
    })
})Copy to clipboardErrorCopied

modifyImageProperty9+

modifyImageProperty(key: string, value: string, callback: AsyncCallback<void>): void

通过指定的键修改图片属性的值,callback形式返回结果,仅支持JPEG文件,且需要包含exif信息。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
keystring图片属性名。
valuestring属性值。
callbackAsyncCallback<void>修改属性值,callback返回结果。

示例:

import {BusinessError} from '@ohos.base';
imageSourceApi.modifyImageProperty("ImageWidth", "120",(err : BusinessError) => {
    if (err != undefined) {
        console.error('modifyImageProperty Failed');
    } else {
        console.info('modifyImageProperty Succeeded');
    }
})Copy to clipboardErrorCopied

updateData9+

updateData(buf: ArrayBuffer, isFinished: boolean, value: number, length: number): Promise<void>

更新增量数据,使用Promise形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
bufArrayBuffer增量数据。
isFinishedboolean是否更新完。
valuenumber偏移量。
lengthnumber数组长。

返回值:

类型说明
Promise<void>Promise实例,异步返回结果。

示例:

import {BusinessError} from '@ohos.base';
const array : ArrayBuffer = new ArrayBuffer(100);
imageSourceApi.updateData(array, false, 0, 10).then(() => {
    console.info('Succeeded in updating data.');
}).catch((err: BusinessError) => {
    console.error(`Failed to update data.code is ${err.code},message is ${err.message}`);
})Copy to clipboardErrorCopied

updateData9+

updateData(buf: ArrayBuffer, isFinished: boolean, value: number, length: number, callback: AsyncCallback<void>): void

更新增量数据,callback形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
bufArrayBuffer增量数据。
isFinishedboolean是否更新完。
valuenumber偏移量。
lengthnumber数组长。
callbackAsyncCallback<void>回调表示成功或失败。

示例:

import {BusinessError} from '@ohos.base';
const array : ArrayBuffer = new ArrayBuffer(100);
imageSourceApi.updateData(array, false, 0, 10, (err: BusinessError) => {
    if (err != undefined) {
        console.error(`Failed to update data.code is ${err.code},message is ${err.message}`);
    } else {
        console.info('Succeeded in updating data.');
    }
})Copy to clipboardErrorCopied

createPixelMap7+

createPixelMap(options?: DecodingOptions): Promise<PixelMap>

通过图片解码参数创建PixelMap对象。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
optionsDecodingOptions解码参数。

返回值:

类型说明
Promise<PixelMap>异步返回Promise对象。

示例:

import {BusinessError} from '@ohos.base';
imageSourceApi.createPixelMap().then((pixelMap : image.PixelMap) => {
    console.log('Succeeded in creating pixelMap object through image decoding parameters.');
}).catch((error : BusinessError) => {
    console.error('Failed to create pixelMap object through image decoding parameters.');
})Copy to clipboardErrorCopied

createPixelMap7+

createPixelMap(callback: AsyncCallback<PixelMap>): void

通过默认参数创建PixelMap对象,使用callback形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
callbackAsyncCallback<PixelMap>通过回调返回PixelMap对象。

示例:

import {BusinessError} from '@ohos.base';
imageSourceApi.createPixelMap((err : BusinessError, pixelMap : image.PixelMap) => {
    console.info('Succeeded in creating pixelMap object.');
})Copy to clipboardErrorCopied

createPixelMap7+

createPixelMap(options: DecodingOptions, callback: AsyncCallback<PixelMap>): void

通过图片解码参数创建PixelMap对象。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
optionsDecodingOptions解码参数。
callbackAsyncCallback<PixelMap>通过回调返回PixelMap对象。

示例:

import {BusinessError} from '@ohos.base';
let decodingOptions : image.DecodingOptions = {
    sampleSize: 1,
    editable: true,
    desiredSize: { width: 1, height: 2 },
    rotate: 10,
    desiredPixelFormat: 3,
    desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 0 },
    index: 0
};
imageSourceApi.createPixelMap(decodingOptions, (err : BusinessError, pixelMap : image.PixelMap) => { 
    console.log('Succeeded in creating pixelMap object.');
})Copy to clipboardErrorCopied

createPixelMapList10+

createPixelMapList(options?: DecodingOptions): Promise<Array<PixelMap>>

通过图片解码参数创建PixelMap数组。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
optionsDecodingOptions解码参数。

返回值:

类型说明
Promise<Array<PixelMap>>异步返回PixeMap数组。

错误码:

以下错误码的详细介绍请参见Image错误码

错误码ID错误信息
62980096If the operation failed
62980103If the image data unsupport
62980110If the image source data error
62980111If the image source data incomplete
62980118If the image plugin create failed

示例:

import {BusinessError} from '@ohos.base';
let decodeOpts: image.DecodingOptions = {
    sampleSize: 1,
    editable: true,
    desiredSize: { width: 198, height: 202 },
    rotate: 0,
    desiredPixelFormat: 3,
    index: 0,
};
imageSourceApi.createPixelMapList(decodeOpts).then((pixelMapList: Array<image.PixelMap>) => {
    console.log('Succeeded in creating pixelMapList object.');
}).catch((err: BusinessError) => {
    console.error(`Failed to create pixelMapList object.code is ${err.code},message is ${err.message}`);
})复制到剪贴板错误复制

createPixelMapList10+

createPixelMapList(callback: AsyncCallback<Array<PixelMap>>): void

通过默认参数创建PixelMap数组,使用callback形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
回调AsyncCallback<Array<PixelMap>>通过回调返回PixelMap数组。

错误码:

以下错误码的详细介绍请参见Image错误码

错误码ID错误信息
62980096If the operation failed
62980103If the image data unsupport
62980110If the image source data error
62980111If the image source data incomplete
62980118If the image plugin create failed

示例:

import {BusinessError} from '@ohos.base';
imageSourceApi.createPixelMapList((err: BusinessError, pixelMapList: Array<image.PixelMap>) => {
    if (err != undefined) {
        console.error(`Failed to create pixelMapList object.code is ${err.code},message is ${err.message}`);
    } else {
        console.info('Succeeded in creating pixelMapList object.');
    }
})复制到剪贴板错误复制

createPixelMapList10+

createPixelMapList(options: DecodingOptions, callback: AsyncCallback<Array<PixelMap>>): void

通过图片解码参数创建PixelMap数组,使用callback形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
选项DecodingOptions解码参数。
回调AsyncCallback<Array<PixelMap>>通过回调返回PixelMap数组。

错误码:

以下错误码的详细介绍请参见Image错误码

错误码ID错误信息
62980096If the operation failed
62980103If the image data unsupport
62980110If the image source data error
62980111If the image source data incomplete
62980118If the image plugin create failed

示例:

import {BusinessError} from '@ohos.base';
let decodeOpts : image.DecodingOptions = {
    sampleSize: 1,
    editable: true,
    desiredSize: { width: 198, height: 202 },
    rotate: 0,
    desiredPixelFormat: 3,
    index: 0,
};
imageSourceApi.createPixelMapList(decodeOpts, (err: BusinessError, pixelMapList: Array<image.PixelMap>) => {
    if (err != undefined) {
        console.error(`Failed to create pixelMapList object.code is ${err.code},message is ${err.message}`);
    } else {
        console.log('Succeeded in creating pixelMapList object.');
    }
})Copy to clipboardErrorCopied

getDelayTimeList10+

getDelayTimeList(callback: AsyncCallback<Array<number>>): void

获取图像延迟时间数组,使用callback形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
callbackAsyncCallback<Array<number>>通过回调返回延迟时间数组。

错误码:

以下错误码的详细介绍请参见Image错误码

错误码ID错误信息
62980096If the operation failed
62980110If the image source data error
62980111If the image source data incomplete
62980113If the image format unknown
62980116If the image decode failed
62980118If the image plugin create failed
62980122If the image decode head abnormal

示例:

import {BusinessError} from '@ohos.base';
imageSourceApi.getDelayTimeList((err: BusinessError, delayTimes: Array<number>) => {
    if (err != undefined) {
        console.error(`Failed to get delayTimes object.code is ${err.code},message is ${err.message}`);
    } else {
        console.log('Succeeded in delayTimes object.');
    }
})Copy to clipboardErrorCopied

getDelayTimeList10+

getDelayTimeList(): Promise<Array<number>>

获取图像延迟时间数组,使用Promise形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageSource

返回值:

类型说明
Promise<Array<number>>Promise实例,异步返回延迟时间数组。

错误码:

以下错误码的详细介绍请参见Image错误码

错误码ID错误信息
62980096If the operation failed
62980110If the image source data error
62980111If the image source data incomplete
62980113If the image format unknown
62980116If the image decode failed
62980118If the image plugin create failed
62980122If the image decode head abnormal

示例:

imageSourceApi.getDelayTimeList().then((delayTimes : Array<number>) => {
    console.log('Succeeded in delayTimes object.');
}).catch((err: BusinessError) => {
    console.error(`Failed to get delayTimes object.code is ${err.code},message is ${err.message}`);
})Copy to clipboardErrorCopied

getFrameCount10+

getFrameCount(callback: AsyncCallback<number>): void

获取图像帧数,使用callback形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
callbackAsyncCallback<number>通过回调返回图像帧数。

错误码:

以下错误码的详细介绍请参见Image错误码

错误码ID错误信息
62980096If the operation failed
62980110If the image source data error
62980111If the image source data incomplete
62980113If the image format unknown
62980116If the image decode failed
62980118If the image plugin create failed
62980122If the image decode head abnormal

示例:

import {BusinessError} from '@ohos.base';
imageSourceApi.getFrameCount((err: BusinessError, frameCount: number) => {
    if (err != undefined) {
        console.error(`Failed to get frame count.code is ${err.code},message is ${err.message}`);
    } else {
        console.log('Succeeded in getting frame count.');
    }
})Copy to clipboardErrorCopied

getFrameCount10+

getFrameCount(): Promise<number>

获取图像帧数,使用Promise形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageSource

返回值:

类型说明
Promise<number>Promise实例,异步返回图像帧数。

错误码:

以下错误码的详细介绍请参见Image错误码

错误码ID错误信息
62980096If the operation failed
62980110If the image source data error
62980111If the image source data incomplete
62980113If the image format unknown
62980116If the image decode failed
62980118If the image plugin create failed
62980122If the image decode head abnormal

示例:

imageSourceApi.getFrameCount().then((frameCount: number) => {
    console.log('Succeeded in getting frame count.');
}).catch((err : BusinessError) => {
    console.error(`Failed to get frame count.code is ${err.code},message is ${err.message}`);
})复制到剪贴板错误复制

release

release(callback: AsyncCallback<void>): void

释放图片源实例,使用callback形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageSource

参数:

参数名类型必填说明
回调AsyncCallback<void>资源释放回调,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
imageSourceApi.release((err : BusinessError) => { 
    if (err != undefined) {
        console.error('Failed to release the image source instance.');
    } else {
        console.log('Succeeded in releasing the image source instance.');
    }
})复制到剪贴板错误复制

release

release(): Promise<void>

释放图片源实例,使用Promise形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageSource

返回值:

类型说明
承诺<无效>Promise实例,异步返回结果。

示例:

import {BusinessError} from '@ohos.base';
imageSourceApi.release().then(()=>{
    console.log('Succeeded in releasing the image source instance.');
}).catch((error : BusinessError) => {
    console.error('Failed to release the image source instance.');
})复制到剪贴板错误复制

image.createImagePacker

createImagePacker(): ImagePacker

创建ImagePacker实例。

系统能力: SystemCapability.Multimedia.Image.ImagePacker

返回值:

类型说明
ImagePacker返回ImagePacker实例。

示例:

const imagePackerApi : image.ImagePacker = image.createImagePacker();Copy to clipboardErrorCopied

ImagePacker

图片打包器类,用于图片压缩和打包。在调用ImagePacker的方法前,需要先通过createImagePacker构建一个ImagePacker实例,当前支持格式有:jpeg webp png。

属性

系统能力: SystemCapability.Multimedia.Image.ImagePacker

名称类型可读可写说明
supportedFormatsArray<string>图片打包支持的格式 jpeg webp png。

packing

packing(source: ImageSource, option: PackingOption, callback: AsyncCallback<ArrayBuffer>): void

图片压缩或重新打包,使用callback形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImagePacker

参数:

参数名类型必填说明
sourceImageSource打包的图片源。
optionPackingOption设置打包参数。
callbackAsyncCallback<ArrayBuffer>获取图片打包回调,返回打包后数据。

示例:

import {BusinessError} from '@ohos.base';
const imageSourceApi : image.ImageSource = image.createImageSource(0);
let packOpts : image.PackingOption = { format:"image/jpeg", quality:98 };
imagePackerApi.packing(imageSourceApi, packOpts, (err : BusinessError, data : ArrayBuffer) => {})Copy to clipboardErrorCopied

packing

packing(source: ImageSource, option: PackingOption): Promise<ArrayBuffer>

图片压缩或重新打包,使用Promise形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImagePacker

参数:

参数名类型必填说明
sourceImageSource打包的图片源。
optionPackingOption设置打包参数。

返回值:

类型说明
Promise<ArrayBuffer>Promise实例,用于异步获取压缩或打包后的数据。

示例:

import {BusinessError} from '@ohos.base';
const imageSourceApi : image.ImageSource = image.createImageSource(0);
let packOpts : image.PackingOption = { format:"image/jpeg", quality:98 }
imagePackerApi.packing(imageSourceApi, packOpts)
    .then( (data : ArrayBuffer) => {
        console.log('packing succeeded.');
    }).catch((error : BusinessError) => {
        console.error('packing failed.');
    })Copy to clipboardErrorCopied

packing8+

packing(source: PixelMap, option: PackingOption, callback: AsyncCallback<ArrayBuffer>): void

图片压缩或重新打包,使用callback形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImagePacker

参数:

参数名类型必填说明
sourcePixelMap打包的PixelMap资源。
optionPackingOption设置打包参数。
callbackAsyncCallback<ArrayBuffer>获取图片打包回调,返回打包后数据。

示例:

import {BusinessError} from '@ohos.base';
const color : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
let opts : image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts).then((pixelMap : image.PixelMap) => {
    let packOpts : image.PackingOption = { format:"image/jpeg", quality:98 }
    imagePackerApi.packing(pixelMap, packOpts, (err : BusinessError, data : ArrayBuffer) => { 
        console.log('Succeeded in packing the image.');
    })
}).catch((error : BusinessError) => {
    console.error('createPixelMap failed.');
})Copy to clipboardErrorCopied

packing8+

packing(source: PixelMap, option: PackingOption): Promise<ArrayBuffer>

图片压缩或重新打包,使用Promise形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImagePacker

参数:

参数名类型必填说明
sourcePixelMap打包的PixelMap源。
optionPackingOption设置打包参数。

返回值:

类型说明
Promise<ArrayBuffer>Promise实例,用于异步获取压缩或打包后的数据。

示例:

import {BusinessError} from '@ohos.base';
const color : ArrayBuffer = new ArrayBuffer(96);  //96为需要创建的像素buffer大小,取值为:height * width *4
let opts : image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts).then((pixelMap : image.PixelMap) => {
    let packOpts : image.PackingOption = { format:"image/jpeg", quality:98 }
    imagePackerApi.packing(pixelMap, packOpts)
        .then( (data : ArrayBuffer) => {
            console.log('Succeeded in packing the image.');
        }).catch((error : BusinessError) => {
            console.error('Failed to pack the image..');
        })
}).catch((error : BusinessError) => {
    console.error('createPixelMap failed.');
})Copy to clipboardErrorCopied

release

release(callback: AsyncCallback<void>): void

释放图片打包实例,使用callback形式返回结果。

系统能力: SystemCapability.Multimedia.Image.ImagePacker

参数:

参数名类型必填说明
回调AsyncCallback<void>释放回调,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
imagePackerApi.release((err : BusinessError)=>{ 
    if (err != undefined) {
        console.error('Failed to release image packaging.'); 
    } else {
        console.log('Succeeded in releasing image packaging.');
    }
})复制到剪贴板错误复制

release

release(): Promise<void>

释放图片打包实例,使用Promise形式返回释放结果。

系统能力: SystemCapability.Multimedia.Image.ImagePacker

返回值:

类型说明
承诺<无效>Promise实例,用于异步获取释放结果,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
imagePackerApi.release().then(()=>{
    console.log('Succeeded in releasing image packaging.');
}).catch((error : BusinessError)=>{ 
    console.error('Failed to release image packaging.'); 
}) 复制到剪贴板错误复制

image.createImageReceiver9+

createImageReceiver(width: number, height: number, format: number, capacity: number): ImageReceiver

通过宽、高、图片格式、容量创建ImageReceiver实例。

系统能力: SystemCapability.Multimedia.Image.ImageReceiver

参数:

参数名类型必填说明
宽度图像的默认宽度。
height图像的默认高度。
format图像格式,取值为ImageFormat常量(目前仅支持 ImageFormat:JPEG)。
capacity同时访问的最大图像数。

返回值:

类型说明
ImageReceiver如果操作成功,则返回ImageReceiver实例。

示例:

let receiver : image.ImageReceiver = image.createImageReceiver(8192, 8, image.ImageFormat.JPEG, 8);复制到剪贴板错误复制

ImageReceiver9+

图像接收类,用于获取组件surface id,接收最新的图片和读取下一张图片,以及释放ImageReceiver实例。

在调用以下方法前需要先创建ImageReceiver实例。

属性

系统能力: SystemCapability.Multimedia.Image.ImageReceiver

名称类型可读可写说明
sizeSize图片大小。
capacitynumber同时访问的图像数。
formatImageFormat图像格式。

getReceivingSurfaceId9+

getReceivingSurfaceId(callback: AsyncCallback<string>): void

用于获取一个surface id供Camera或其他组件使用。使用callback返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageReceiver

参数:

参数名类型必填说明
callbackAsyncCallback<string>回调函数,返回surface id。

示例:

import {BusinessError} from '@ohos.base';
receiver.getReceivingSurfaceId((err : BusinessError, id : string) => { 
    if(err) {
        console.error('getReceivingSurfaceId failed.');
    } else {
        console.log('getReceivingSurfaceId succeeded.');
    }
});Copy to clipboardErrorCopied

getReceivingSurfaceId9+

getReceivingSurfaceId(): Promise<string>

用于获取一个surface id供Camera或其他组件使用。使用promise返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageReceiver

返回值:

类型说明
Promise<string>异步返回surface id。

示例:

import {BusinessError} from '@ohos.base';
receiver.getReceivingSurfaceId().then( (id : string) => { 
    console.log('getReceivingSurfaceId succeeded.');
}).catch((error : BusinessError) => {
    console.error('getReceivingSurfaceId failed.');
})复制到剪贴板错误复制

readLatestImage9+

readLatestImage(callback: AsyncCallback<Image>): void

从ImageReceiver读取最新的图片,并使用callback返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageReceiver

参数:

参数名类型必填说明
回调AsyncCallback<Image>回调函数,返回最新图像。

示例:

import {BusinessError} from '@ohos.base';
receiver.readLatestImage((err : BusinessError, img : image.Image) => { 
    if(err) {
        console.error('readLatestImage failed.');
    } else {
        console.log('readLatestImage succeeded.');
    }
});复制到剪贴板错误复制

readLatestImage9+

readLatestImage(): Promise<Image>

从ImageReceiver读取最新的图片,并使用promise返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageReceiver

返回值:

类型说明
Promise<Image>异步返回最新图片。

示例:

import {BusinessError} from '@ohos.base';
receiver.readLatestImage().then((img : image.Image) => {
    console.log('readLatestImage succeeded.');
}).catch((error : BusinessError) => {
    console.error('readLatestImage failed.');
})复制到剪贴板错误复制

readNextImage9+

readNextImage(callback: AsyncCallback<Image>): void

从ImageReceiver读取下一张图片,并使用callback返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageReceiver

参数:

参数名类型必填说明
callbackAsyncCallback<Image>回调函数,返回下一张图片。

示例:

import {BusinessError} from '@ohos.base';
receiver.readNextImage((err : BusinessError, img : image.Image) => { 
    if(err) {
        console.error('readNextImage failed.');
    } else {
        console.log('readNextImage succeeded.');
    }
});Copy to clipboardErrorCopied

readNextImage9+

readNextImage(): Promise<Image>

从ImageReceiver读取下一张图片,并使用promise返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageReceiver

返回值:

类型说明
Promise<Image>异步返回下一张图片。

示例:

import {BusinessError} from '@ohos.base';
receiver.readNextImage().then((img : image.Image) => {
    console.log('readNextImage succeeded.');
}).catch((error : BusinessError) => {
    console.error('readNextImage failed.');
})Copy to clipboardErrorCopied

on9+

on(type: 'imageArrival', callback: AsyncCallback<void>): void

接收图片时注册回调。

系统能力: SystemCapability.Multimedia.Image.ImageReceiver

参数:

参数名类型必填说明
typestring注册事件的类型,固定为'imageArrival',接收图片时触发。
callbackAsyncCallback<void>注册的事件回调。

示例:

receiver.on('imageArrival', () => {})Copy to clipboardErrorCopied

release9+

release(callback: AsyncCallback<void>): void

释放ImageReceiver实例并使用回调返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageReceiver

参数:

参数名类型必填说明
callbackAsyncCallback<void>回调函数,返回操作结果。

示例:

import {BusinessError} from '@ohos.base'
receiver.release((err : BusinessError) => {})Copy to clipboardErrorCopied

release9+

release(): Promise<void>

释放ImageReceiver实例并使用promise返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageReceiver

返回值:

类型说明
Promise<void>异步返回操作结果。

示例:

import {BusinessError} from '@ohos.base';
receiver.release().then(() => {
    console.log('release succeeded.');
}).catch((error : BusinessError) => {
    console.error('release failed.');
})Copy to clipboardErrorCopied

image.createImageCreator9+

createImageCreator(width: number, height: number, format: number, capacity: number): ImageCreator

通过宽、高、图片格式、容量创建ImageCreator实例。

系统能力: SystemCapability.Multimedia.Image.ImageCreator

参数:

参数名类型必填说明
widthnumber图像的默认宽度。
heightnumber图像的默认高度。
formatnumber图像格式,如YCBCR_422_SP,JPEG。
capacitynumber同时访问的最大图像数。

返回值:

类型说明
ImageCreator如果操作成功,则返回ImageCreator实例。

示例:

let creator : image.ImageCreator = image.createImageCreator(8192, 8, image.ImageFormat.JPEG, 8);Copy to clipboardErrorCopied

ImageCreator9+

图像创建模块,用于请求图像原生数据区域,并开放给应用编译原生图像数据的能力。 在调用以下方法前需要先创建ImageCreator实例,ImageCreator不支持多线程。

属性

系统能力: SystemCapability.Multimedia.Image.ImageCreator

名称类型可读可写说明
capacitynumber同时访问的图像数。
formatImageFormat图像格式。

dequeueImage9+

dequeueImage(callback: AsyncCallback<Image>): void

从空闲队列中获取buffer图片,用于绘制UI内容,并使用callback返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageCreator

参数:

参数名类型必填说明
callbackAsyncCallback<Image>回调函数,返回最新图片。

示例:

import {BusinessError} from '@ohos.base';
creator.dequeueImage((err : BusinessError, img : image.Image) => {
    if (err) {
        console.error('dequeueImage failed.');
    } else {
        console.info('dequeueImage succeeded.');
    }
});Copy to clipboardErrorCopied

dequeueImage9+

dequeueImage(): Promise<Image>

从空闲队列中获取buffer图片,用于绘制UI内容,并使用promise返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageCreator

返回值:

类型说明
Promise<Image>返回绘制的图像。

示例:

import {BusinessError} from '@ohos.base';
creator.dequeueImage().then((img : image.Image) => {
    console.info('dequeueImage succeeded.');
}).catch((error : BusinessError) => {
    console.error('dequeueImage failed: ' + error);
})Copy to clipboardErrorCopied

queueImage9+

queueImage(interface: Image, callback: AsyncCallback<void>): void

将绘制好的图片放入Dirty队列,并使用callback返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageCreator

参数:

参数名类型必填说明
interfaceImage绘制好的buffer图像。
callbackAsyncCallback<void>获取回调,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
creator.dequeueImage().then((img : image.Image) => {
    //绘制图片
    img.getComponent(4).then( (component : image.Component) => {
        let bufferArr : Uint8Array = new Uint8Array(component.byteBuffer);
        for (let i = 0; i < bufferArr.length; i += 4) {
            bufferArr[i] = 0; //B
            bufferArr[i + 1] = 0; //G
            bufferArr[i + 2] = 255; //R
            bufferArr[i + 3] = 255; //A
        }
    })
    creator.queueImage(img, (err : BusinessError) => {
        if (err) {
            console.error('queueImage failed: ' + err);
        } else {
            console.info('queueImage succeeded');
        }
    })
})
Copy to clipboardErrorCopied

queueImage9+

queueImage(interface: Image): Promise<void>

将绘制好的图片放入Dirty队列,并使用promise返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageCreator

参数:

参数名类型必填说明
interfaceImage绘制好的buffer图像。

返回值:

类型说明
Promise<void>获取回调,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
creator.dequeueImage().then((img : image.Image) => {
    //绘制图片
    img.getComponent(4).then((component : image.Component) => {
        let bufferArr : Uint8Array = new Uint8Array(component.byteBuffer);
        for (let i = 0; i < bufferArr.length; i += 4) {
            bufferArr[i] = 0; //B
            bufferArr[i + 1] = 0; //G
            bufferArr[i + 2] = 255; //R
            bufferArr[i + 3] = 255; //A
        }
    })
    creator.queueImage(img).then(() => {
        console.info('queueImage succeeded.');
    }).catch((error : BusinessError) => {
        console.error('queueImage failed: ' + error);
    })
})
Copy to clipboardErrorCopied

on9+

on(type: 'imageRelease', callback: AsyncCallback<void>): void

监听imageRelease事件,并使用callback返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageCreator

参数:

参数名类型必填说明
typestring监听事件类型,如'imageRelease'。
callbackAsyncCallback<void>获取回调,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
creator.on('imageRelease', (err : BusinessError) => {
    if (err) {
        console.error('on faild' + err);
    } else {
        console.info('on succeeded');
    }
})Copy to clipboardErrorCopied

release9+

release(callback: AsyncCallback<void>): void

释放当前图像,并使用callback返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageCreator

参数:

参数名类型必填说明
callbackAsyncCallback<void>获取回调,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
creator.release((err : BusinessError) => {
    if (err) {
        console.error('release failed: ' + err);
    } else {
        console.info('release succeeded');
    }
});Copy to clipboardErrorCopied

release9+

release(): Promise<void>

释放当前图像,并使用promise返回结果。

系统能力: SystemCapability.Multimedia.Image.ImageCreator

返回值:

类型说明
Promise<void>获取回调,失败时返回错误信息。

示例:

import {BusinessError} from '@ohos.base';
creator.release().then(() => {
    console.info('release succeeded');
}).catch((error : BusinessError) => {
    console.error('release failed');
})Copy to clipboardErrorCopied

Image9+

提供基本的图像操作,包括获取图像信息、读写图像数据。调用readNextImagereadLatestImage接口时会返回image。

属性

系统能力: SystemCapability.Multimedia.Image.Core

名称类型可读可写说明
clipRectRegion要裁剪的图像区域。
sizeSize图像大小。
formatnumber图像格式,参考PixelMapFormat

getComponent9+

getComponent(componentType: ComponentType, callback: AsyncCallback<Component>): void

根据图像的组件类型从图像中获取组件缓存并使用callback返回结果。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
componentTypeComponentType图像的组件类型。
callbackAsyncCallback<Component>用于返回组件缓冲区。

示例:

import {BusinessError} from '@ohos.base';
img.getComponent(4, (err : BusinessError, component : image.Component) => {
    if(err) {
        console.error('getComponent failed.');
    } else {
        console.log('getComponent succeeded.');
    }
})Copy to clipboardErrorCopied

getComponent9+

getComponent(componentType: ComponentType): Promise<Component>

根据图像的组件类型从图像中获取组件缓存并使用Promise方式返回结果。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
componentTypeComponentType图像的组件类型。

返回值:

类型说明
Promise<Component>用于返回组件缓冲区的promise实例。

示例:

img.getComponent(4).then((component : image.Component) => { })复制到剪贴板错误复制

release9+

release(callback: AsyncCallback<void>): void

释放当前图像并使用callback返回结果。

在接收另一个图像前必须先释放对应资源。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名类型必填说明
回调AsyncCallback<void>返回操作结果。

示例:

import {BusinessError} from '@ohos.base';
img.release((err : BusinessError) =>{ 
    if (err != undefined) {
        console.error('Failed to release the image source instance.');
    } else {
        console.log('Succeeded in releasing the image source instance.');
    }
}) 复制到剪贴板错误复制

release9+

release(): Promise<void>

释放当前图像并使用Promise方式返回结果。

在接收另一个图像前必须先释放对应资源。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型说明
承诺<无效>promise返回操作结果。

示例:

import {BusinessError} from '@ohos.base';
img.release().then(() =>{
    console.log('release succeeded.');
}).catch((error : BusinessError) => {
    console.error('release failed.');
})复制到剪贴板错误复制

PositionArea7+

表示图片指定区域内的数据。

系统能力: SystemCapability.Multimedia.Image.Core

名称类型可读可写说明
pixelsArrayBuffer像素。
抵消偏移量。
stride像素间距,stride >= region.size.width*4。
regionRegion区域,按照区域读写。写入的区域宽度加X坐标不能大于原图的宽度,写入的区域高度加Y坐标不能大于原图的高度。

ImageInfo

表示图片信息。

系统能力: SystemCapability.Multimedia.Image.Core

名称类型可读可写说明
sizeSize图片大小。
density9+number像素密度,单位为ppi。

Size

表示图片尺寸。

系统能力: SystemCapability.Multimedia.Image.Core

名称类型可读可写说明
heightnumber输出图片的高。
widthnumber输出图片的宽。

PixelMapFormat7+

枚举,图片像素格式。

系统能力: SystemCapability.Multimedia.Image.Core

名称说明
UNKNOWN0未知格式。
RGB_5652格式为RGB_565
RGBA_88883格式为RGBA_8888
BGRA_88889+4格式为BGRA_8888
RGB_8889+5格式为RGB_888
ALPHA_89+6格式为ALPHA_8
RGBA_F169+7格式为RGBA_F16
NV219+8格式为NV21
NV129+9格式为NV12

AlphaType9+

枚举,图像的透明度类型。

系统能力: SystemCapability.Multimedia.Image.Core

名称说明
UNKNOWN0未知透明度。
OPAQUE1没有alpha或图片全透明。
PREMUL2RGB前乘alpha。
UNPREMUL3RGB不前乘alpha。

ScaleMode9+

枚举,图像的缩放模式。

系统能力: SystemCapability.Multimedia.Image.Core

名称说明
CENTER_CROP1缩放图像以填充目标图像区域并居中裁剪区域外的效果。
FIT_TARGET_SIZE0图像适合目标尺寸的效果。

SourceOptions9+

ImageSource的初始化选项。

系统能力: SystemCapability.Multimedia.Image.Core

名称类型可读可写说明
sourceDensitynumberImageSource的密度。
sourcePixelFormatPixelMapFormat图片像素格式。
sourceSizeSize图像像素大小。

InitializationOptions8+

PixelMap的初始化选项。

系统能力: SystemCapability.Multimedia.Image.Core

名称类型可读可写说明
alphaType9+AlphaType透明度。
editableboolean是否可编辑。
pixelFormatPixelMapFormat像素格式。
scaleMode9+ScaleMode缩略值。
sizeSize创建图片大小。

DecodingOptions7+

图像解码设置选项。

系统能力: SystemCapability.Multimedia.Image.ImageSource

名称类型可读可写说明
sampleSizenumber缩略图采样大小,当前只能取1。
rotatenumber旋转角度。
editableboolean是否可编辑。当取值为false时,图片不可二次编辑,如crop等操作将失败。
desiredSizeSize期望输出大小。
desiredRegionRegion解码区域。
desiredPixelFormatPixelMapFormat解码的像素格式。
indexnumber解码图片序号。
fitDensity9+number图像像素密度,单位为ppi。

Region7+

表示区域信息。

系统能力: SystemCapability.Multimedia.Image.Core

名称类型可读可写说明
sizeSize区域大小。
xnumber区域横坐标。
ynumber区域纵坐标。

PackingOption

表示图片打包选项。

系统能力: SystemCapability.Multimedia.Image.ImagePacker

名称类型可读可写说明
format字符串目标格式。
当前只支持jpg、webp 和 png。
qualityJPEG编码中设定输出图片质量的参数,取值范围为1-100。
bufferSize9+接收编码数据的缓冲区大小,单位为Byte。默认为10MB。bufferSize需大于编码后图片大小。

GetImagePropertyOptions7+

表示查询图片属性的索引。

系统能力: SystemCapability.Multimedia.Image.ImageSource

名称类型可读可写说明
指数图片序号。
defaultValue字符串默认属性值。

PropertyKey7+

枚举,Exif(Exchangeable image file format)图片信息。

系统能力: SystemCapability.Multimedia.Image.Core

名称说明
BITS_PER_SAMPLE"BitsPerSample"每个像素比特数。
ORIENTATION"Orientation"图片方向。
IMAGE_LENGTH"ImageLength"图片长度。
IMAGE_WIDTH"ImageWidth"图片宽度。
GPS_LATITUDE"GPSLatitude"图片纬度。
GPS_LONGITUDE"GPSLongitude"图片经度。
GPS_LATITUDE_REF"GPSLatitudeRef"纬度引用,例如N或S。
GPS_LONGITUDE_REF"GPSLongitudeRef"经度引用,例如W或E。
DATE_TIME_ORIGINAL9+"DateTimeOriginal"拍摄时间,例如2022:09:06 15:48:00。当前为只读属性。
EXPOSURE_TIME9+"ExposureTime"曝光时间,例如1/33 sec。当前为只读属性。
SCENE_TYPE9+"SceneType"拍摄场景模式,例如人像、风光、运动、夜景等。当前为只读属性。
ISO_SPEED_RATINGS9+"ISOSpeedRatings"ISO感光度,例如400。当前为只读属性。
F_NUMBER9+"FNumber"光圈值,例如f/1.8。当前为只读属性。
DATE_TIME10+"DateTime"日期时间,当前为只读属性。
GPS_TIME_STAMP10+"GPSTimeStamp"GPS时间戳,当前为只读属性。
GPS_DATE_STAMP10+"GPSDateStamp"GPS日期戳,当前为只读属性。
IMAGE_DESCRIPTION10+"ImageDescription"图像信息描述,当前为只读属性。
MAKE10+"Make"生产商,当前为只读属性。
MODEL10+"Model"设备型号,当前为只读属性。
PHOTO_MODE10+"PhotoMode "拍照模式,当前为只读属性。
SENSITIVITY_TYPE10+"SensitivityType"灵敏度类型,当前为只读属性。
STANDARD_OUTPUT_SENSITIVITY10+"StandardOutputSensitivity"标准输出灵敏度,当前为只读属性。
RECOMMENDED_EXPOSURE_INDEX10+"RecommendedExposureIndex"推荐曝光指数,当前为只读属性。
ISO_SPEED10+"ISOSpeedRatings"ISO速度等级,当前为只读属性。
APERTURE_VALUE10+"ApertureValue"光圈值,当前为只读属性。
EXPOSURE_BIAS_VALUE10+"ExposureBiasValue"曝光偏差值,当前为只读属性。
METERING_MODE10+"MeteringMode"测光模式,当前为只读属性。
LIGHT_SOURCE10+"LightSource"光源,当前为只读属性。
FLASH 10+"Flash"闪光灯,记录闪光灯状态,当前为只读属性。
FOCAL_LENGTH 10+"FocalLength"焦距,当前为只读属性。
USER_COMMENT 10+"UserComment"用户注释,当前为只读属性。
PIXEL_X_DIMENSION 10+"PixelXDimension"像素X尺寸,当前为只读属性。
PIXEL_Y_DIMENSION10+"PixelYDimension"像素Y尺寸,当前为只读属性。
WHITE_BALANCE 10+"WhiteBalance"白平衡,当前为只读属性。
FOCAL_LENGTH_IN_35_MM_FILM 10+"FocalLengthIn35mmFilm"焦距35毫米胶片,当前为只读属性。
CAPTURE_MODE 10+"HwMnoteCaptureMode"捕获模式,当前为只读属性。
PHYSICAL_APERTURE 10+"HwMnotePhysicalAperture"物理孔径,光圈大小,当前为只读属性。

ImageFormat9+

枚举,图片格式。

系统能力: SystemCapability.Multimedia.Image.Core

名称说明
YCBCR_422_SP1000YCBCR422半平面格式。
JPEG2000JPEG编码格式。

ComponentType9+

枚举,图像的组件类型。

系统能力: SystemCapability.Multimedia.Image.ImageReceiver

名称说明
YUV_Y1亮度信息。
YUV_U2色度信息。
YUV_V3色度信息。
JPEG4JPEG 类型。

Component9+

描述图像颜色分量。

系统能力: SystemCapability.Multimedia.Image.Core

名称类型可读可写说明
componentTypeComponentType组件类型。
rowStride行距。
pixelStride像素间距。
byteBufferArrayBuffer组件缓冲区。

最后

有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(HarmonyOS NEXT)资料用来跟着学习是非常有必要的。 

这份鸿蒙(HarmonyOS NEXT)资料包含了鸿蒙开发必掌握的核心知识要点,内容包含了ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(HarmonyOS NEXT)技术知识点。

希望这一份鸿蒙学习资料能够给大家带来帮助,有需要的小伙伴自行领取,限时开源,先到先得~无套路领取!!

获取这份完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料

鸿蒙(HarmonyOS NEXT)最新学习路线

  •  HarmonOS基础技能

  • HarmonOS就业必备技能 
  •  HarmonOS多媒体技术

  • 鸿蒙NaPi组件进阶

  • HarmonOS高级技能

  • 初识HarmonOS内核 
  • 实战就业级设备开发

有了路线图,怎么能没有学习资料呢,小编也准备了一份联合鸿蒙官方发布笔记整理收纳的一套系统性的鸿蒙(OpenHarmony )学习手册(共计1236页)鸿蒙(OpenHarmony )开发入门教学视频,内容包含:ArkTS、ArkUI、Web开发、应用模型、资源分类…等知识点。

获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料

《鸿蒙 (OpenHarmony)开发入门教学视频》

《鸿蒙生态应用开发V2.0白皮书》

图片

《鸿蒙 (OpenHarmony)开发基础到实战手册》

OpenHarmony北向、南向开发环境搭建

图片

 《鸿蒙开发基础》

  • ArkTS语言
  • 安装DevEco Studio
  • 运用你的第一个ArkTS应用
  • ArkUI声明式UI开发
  • .……

图片

 《鸿蒙开发进阶》

  • Stage模型入门
  • 网络管理
  • 数据管理
  • 电话服务
  • 分布式应用开发
  • 通知与窗口管理
  • 多媒体技术
  • 安全技能
  • 任务管理
  • WebGL
  • 国际化开发
  • 应用测试
  • DFX面向未来设计
  • 鸿蒙系统移植和裁剪定制
  • ……

图片

《鸿蒙进阶实战》

  • ArkTS实践
  • UIAbility应用
  • 网络案例
  • ……

图片

 获取以上完整鸿蒙HarmonyOS学习资料,请点击→纯血版全套鸿蒙HarmonyOS学习资料

总结

总的来说,华为鸿蒙不再兼容安卓,对中年程序员来说是一个挑战,也是一个机会。只有积极应对变化,不断学习和提升自己,他们才能在这个变革的时代中立于不败之地。

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值