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

2024-05-31 17:12

本文主要是介绍鸿蒙开发接口媒体:【@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

参数:

名称类型必填说明
colorsArrayBufferBGRA_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

参数:

名称类型必填说明
colorsArrayBufferBGRA_8888格式的颜色数组。
options[InitializationOptions]属性。
callbackAsyncCallback<[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

参数:

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

返回值:

类型说明
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

参数:

参数名类型必填说明
dstArrayBuffer缓冲区,函数执行结束后获取的图像像素数据写入到该内存区域内。
callbackAsyncCallback<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]区域大小,根据区域读取。
callbackAsyncCallback<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.heightregion: { 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

参数:

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

返回值:

类型说明
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

参数:

参数名类型必填说明
srcArrayBuffer图像像素数据。
callbackAsyncCallback<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 in obtaining the image pixel map information.");
}).catch((err) => {console.error("Failed to obtain the image pixel map information.");
});

getImageInfo7+

getImageInfo(callback: AsyncCallback<ImageInfo>): void

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

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

参数:

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

示例:

pixelmap.getImageInfo((imageInfo) => { console.log("Succeeded in obtaining the image pixel map information..");
})

getBytesNumberPerRow7+

getBytesNumberPerRow(): number

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

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

返回值:

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

示例:

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) => {let rowCount = pixelmap.getBytesNumberPerRow();
})

getPixelBytesNumber7+

getPixelBytesNumber(): number

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

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

返回值:

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

示例:

let pixelBytesNumber = pixelmap.getPixelBytesNumber();

release7+

release():Promise<void>

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

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

返回值:

类型说明
Promise<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, (pixelmap) => {pixelmap.release().then(() => {console.log('Succeeded in releasing pixelmap object.');}).catch(error => {console.log('Failed to release pixelmap object.');})
})

release7+

release(callback: AsyncCallback<void>): void

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

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

参数:

名称类型必填说明
callbackAsyncCallback<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, (pixelmap) => {pixelmap.release().then(() => {console.log('Succeeded in releasing pixelmap object.');}).catch(error => {console.log('Failed to release pixelmap object.');})
})

image.createImageSource

createImageSource(uri: string): ImageSource

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

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

参数:

参数名类型必填说明
uristring图片路径,当前仅支持应用沙箱路径。

返回值:

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

示例:

let path = this.context.getApplicationContext().fileDirs + "test.jpg";
const imageSourceApi = image.createImageSource(path);

image.createImageSource7+

createImageSource(fd: number): ImageSource

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

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

参数:

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

返回值:

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

示例:

const imageSourceApi = image.createImageSource(0)

image.createImageSource9+

createImageSource(buf: ArrayBuffer): ImageSource

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

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

参数:

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

示例:

const buf = new ArrayBuffer(96);
image.createImageSource(buf, () => { })

image.CreateIncrementalSource9+

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

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

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

参数:

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

返回值:

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

示例:

const buf = new ArrayBuffer(96);
const imageSourceApi = image.createIncrementalSource(buf);

ImageSource

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

属性

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

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

getImageInfo

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

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

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

参数:

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

示例:

imageSourceApi.getImageInfo(0,(error, imageInfo) => { if(error) {console.log('getImageInfo failed.');} else {console.log('getImageInfo succeeded.');}
})

getImageInfo

getImageInfo(callback: AsyncCallback<ImageInfo>): void

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

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

参数:

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

示例:

imageSourceApi.getImageInfo(imageInfo => { console.log('Succeeded in obtaining the image information.');
})

getImageInfo

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

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

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

参数:

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

返回值:

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

示例:

imageSourceApi.getImageInfo(0).then(imageInfo => {console.log('Succeeded in obtaining the image information.');}).catch(error => {console.log('Failed to obtain the image information.');})

getImageProperty7+

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

获取图片中给定索引处图像的指定属性键的值,用Promise形式返回结果。

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

参数:

名称类型必填说明
keystring图片属性名。
options[GetImagePropertyOptions]图片属性,包括图片序号与默认属性值。

返回值:

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

示例:

imageSourceApi.getImageProperty("BitsPerSample").then(data => {console.log('Succeeded in getting the value of the specified attribute key of the image.');})

getImageProperty7+

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

获取图片中给定索引处图像的指定属性键的值,用callback形式返回结果。

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

参数:

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

示例:

imageSourceApi.getImageProperty("BitsPerSample",(error,data) => { if(error) {console.log('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形式返回结果。

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

参数:

参数名类型必填说明
keystring图片属性名。
options[GetImagePropertyOptions]图片属性,包括图片序号与默认属性值。
callbackAsyncCallback<string>获取图片属性回调,返回图片属性值,如获取失败则返回属性默认值。

示例:

const property = new ArrayBuffer(400);
imageSourceApi.getImageProperty("BitsPerSample",property,(error,data) => { if(error) {console.log('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形式返回结果。

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

参数:

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

返回值:

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

示例:

imageSourceApi.modifyImageProperty("ImageWidth", "abc").then(() => {const w = imageSourceApi.getImageProperty("ImageWidth")console.info('w', w);})

modifyImageProperty9+

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

通过指定的键修改图片属性的值,callback形式返回结果。

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

参数:

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

示例:

imageSourceApi.modifyImageProperty("ImageWidth", "abc",() => {})

updateData9+

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

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

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

参数:

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

返回值:

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

示例:

const array = new ArrayBuffer(100);
imageSourceIncrementalSApi.updateData(array, false, 0, 10).then(data => {console.info('Succeeded in updating data.');})

updateData9+

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

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

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

参数:

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

示例:

const array = new ArrayBuffer(100);
imageSourceIncrementalSApi.updateData(array, false, 0, 10,(error,data )=> {if(data !== undefined){console.info('Succeeded in updating data.');     }})

createPixelMap7+

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

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

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

参数:

名称类型必填说明
options[DecodingOptions]解码参数。

返回值:

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

示例:

imageSourceApi.createPixelMap().then(pixelmap => {console.log('Succeeded in creating pixelmap object through image decoding parameters.');
}).catch(error => {console.log('Failed to create pixelmap object through image decoding parameters.');
})

createPixelMap7+

createPixelMap(callback: AsyncCallback<PixelMap>): void

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

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

参数:

名称类型必填说明
callbackAsyncCallback<[PixelMap]>通过回调返回PixelMap对象。

示例:

imageSourceApi.createPixelMap(pixelmap => { console.log('Succeeded in creating pixelmap object.');
}).catch(error => {console.log('Failed to create pixelmap object.');
})

createPixelMap7+

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

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

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

参数:

名称类型必填说明
options[DecodingOptions]解码参数。
callbackAsyncCallback<[PixelMap]>通过回调返回PixelMap对象。

示例:

const decodingOptions = new ArrayBuffer(400);
imageSourceApi.createPixelMap(decodingOptions, pixelmap => { console.log('Succeeded in creating pixelmap object.');
})

release

release(callback: AsyncCallback<void>): void

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

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

参数:

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

示例:

imageSourceApi.release(() => { console.log('release succeeded.');
})

release

release(): Promise<void>

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

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

返回值:

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

示例:

imageSourceApi.release().then(()=>{console.log('Succeeded in releasing the image source instance.');
}).catch(error => {console.log('Failed to release the image source instance.');
})

image.createImagePacker

createImagePacker(): ImagePacker

创建ImagePacker实例。

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

返回值:

类型说明
[ImagePacker]返回ImagePacker实例。

示例:

const imagePackerApi = image.createImagePacker();

ImagePacker

图片打包器类,用于图片压缩和打包。在调用ImagePacker的方法前,需要先通过createImagePacker构建一个ImagePacker实例。

属性

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

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

packing

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

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

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

参数:

参数名类型必填说明
source[ImageSource]打包的图片源。
option[PackingOption]设置打包参数。
callbackAsyncCallback<ArrayBuffer>获取图片打包回调,返回打包后数据。

示例:

let packOpts = { format:"image/jpeg", quality:98 };
const imageSourceApi = new ArrayBuffer(400);
imagePackerApi.packing(imageSourceApi, packOpts, data => {})

packing

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

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

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

参数:

参数名类型必填说明
source[ImageSource]打包的图片源。
option[PackingOption]设置打包参数。

返回值:

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

示例:

let packOpts = { format:"image/jpeg", quality:98 }
const imageSourceApi = new ArrayBuffer(400);
imagePackerApi.packing(imageSourceApi, packOpts).then( data => {console.log('packing succeeded.');}).catch(error => {console.log('packing failed.');})

packing8+

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

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

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

参数:

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

示例:

let packOpts = { format:"image/jpeg", quality:98 }
const pixelMapApi = new ArrayBuffer(400);
imagePackerApi.packing(pixelMapApi, packOpts, data => { console.log('Succeeded in packing the image.');
}).catch(error => {console.log('Failed to pack the image.');
})

packing8+

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

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

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

参数:

参数名类型必填说明
source[PixelMap]打包的PixelMap源。
option[PackingOption]设置打包参数。

返回值:

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

示例:

let packOpts = { format:"image/jpeg", quality:98 }
const pixelMapApi = new ArrayBuffer(400);
imagePackerApi.packing(pixelMapApi, packOpts).then( data => {console.log('Succeeded in packing the image.');}).catch(error => {console.log('Failed to pack the image..');})

release

release(callback: AsyncCallback<void>): void

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

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

参数:

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

示例:

imagePackerApi.release(()=>{ console.log('Succeeded in releasing image packaging.');
})

release

release(): Promise<void>

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

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

返回值:

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

示例:

imagePackerApi.release().then(()=>{console.log('Succeeded in releasing image packaging.');
}).catch((error)=>{ console.log('Failed to release image packaging.'); 
}) 

image.createImageReceiver9+

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

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

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

参数:

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

返回值:

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

示例:

var receiver = image.createImageReceiver(8192, 8, 4, 8);

ImageReceiver9+

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

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

属性

系统能力:  以下各项对应的系统能力均为SystemCapability.Multimedia.Image.ImageReceiver

名称类型可读可写说明
size9+[Size]图片大小。
capacity9+number同时访问的图像数。
format9+[ImageFormat]图像格式。

getReceivingSurfaceId9+

getReceivingSurfaceId(callback: AsyncCallback<string>): void

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

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

参数:

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

示例:

receiver.getReceivingSurfaceId((err, id) => { if(err) {console.log('getReceivingSurfaceId failed.');} else {console.log('getReceivingSurfaceId succeeded.');}
});

getReceivingSurfaceId9+

getReceivingSurfaceId(): Promise<string>

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

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

返回值:

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

示例:

receiver.getReceivingSurfaceId().then( id => { console.log('getReceivingSurfaceId succeeded.');
}).catch(error => {console.log('getReceivingSurfaceId failed.');
})

readLatestImage9+

readLatestImage(callback: AsyncCallback<Image>): void

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

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

参数:

名称类型必填说明
callbackAsyncCallback<[Image]>回调函数,返回最新图像。

示例:

receiver.readLatestImage((err, img) => { if(err) {console.log('readLatestImage failed.');} else {console.log('readLatestImage succeeded.');}
});

readLatestImage9+

readLatestImage(): Promise<Image>

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

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

返回值:

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

示例:

receiver.readLatestImage().then(img => {console.log('readLatestImage succeeded.');
}).catch(error => {console.log('readLatestImage failed.');
})

readNextImage9+

readNextImage(callback: AsyncCallback<Image>): void

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

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

参数:

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

示例:

receiver.readNextImage((err, img) => { if(err) {console.log('readNextImage failed.');} else {console.log('readNextImage succeeded.');}
});

readNextImage9+

readNextImage(): Promise<Image>

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

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

返回值:

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

示例:

receiver.readNextImage().then(img => {console.log('readNextImage succeeded.');
}).catch(error => {console.log('readNextImage failed.');
})

on('imageArrival')9+

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

接收图片时注册回调。

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

参数:

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

示例:

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

release9+

release(callback: AsyncCallback<void>): void

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

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

参数:

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

示例:

receiver.release(() => {})

release9+

release(): Promise<void>

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

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

返回值:

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

示例:

receiver.release().then(() => {console.log('release succeeded.');
}).catch(error => {console.log('release failed.');
})

Image9+

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

属性

系统能力:  以下各项对应的系统能力均为SystemCapability.Multimedia.Image.Core

名称类型可读可写说明
clipRect9+[Region]要裁剪的图像区域。
size9+[Size]图像大小。
format9+number图像格式,参考[PixelMapFormat]。

getComponent9+

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

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

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

参数:

名称类型必填说明
componentType[ComponentType]图像的组件类型。
callbackAsyncCallback<[Component]>用于返回组件缓冲区。

示例:

img.getComponent(4, (err, component) => {if(err) {console.log('getComponent failed.');} else {console.log('getComponent succeeded.');}
})

getComponent9+

getComponent(componentType: ComponentType): Promise<Component>

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

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

参数:

名称类型必填说明
componentType[ComponentType]图像的组件类型。

返回值:

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

示例:

img.getComponent(4).then(component => { })

release9+

release(callback: AsyncCallback<void>): void

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

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

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

参数:

名称类型必填说明
callbackAsyncCallback<void>返回操作结果。

示例:

img.release(() =>{ console.log('release succeeded.');
}).catch(error => {console.log('release failed.');
}) 

release9+

release(): Promise<void>

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

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

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

返回值:

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

示例:

img.release().then(() =>{console.log('release succeeded.');
}).catch(error => {console.log('release failed.');
})

PositionArea7+

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

系统能力:  以下各项对应的系统能力均为SystemCapability.Multimedia.Image.Core

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

ImageInfo

表示图片信息。

系统能力:  以下各项对应的系统能力均为SystemCapability.Multimedia.Image.Core

名称类型可读可写说明
size[Size]图片大小。

Size

表示图片尺寸。

系统能力:  以下各项对应的系统能力均为SystemCapability.Multimedia.Image.Core

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

PixelMapFormat7+

枚举,图片像素格式。

系统能力:  以下各项对应的系统能力均为SystemCapability.Multimedia.Image.Core

名称默认值描述
UNKNOWN0未知格式。
RGBA_88883格式为RGBA_8888。
RGB_5652格式为RGB_565。

AlphaType9+

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

系统能力:  以下各项对应的系统能力均为SystemCapability.Multimedia.Image.Core

名称默认值描述
UNKNOWN0未知透明度。
OPAQUE1没有alpha或图片全透明。
PREMUL2RGB前乘alpha。
UNPREMUL3RGB不前乘alpha。

ScaleMode9+

枚举,图像的缩放模式。

系统能力:  以下各项对应的系统能力均为SystemCapability.Multimedia.Image.Core

名称默认值描述
CENTER_CROP1缩放图像以填充目标图像区域并居中裁剪区域外的效果。
FIT_TARGET_SIZE2图像适合目标尺寸的效果。

InitializationOptions8+

PixelMap的初始化选项。

系统能力:  以下各项对应的系统能力均为SystemCapability.Multimedia.Image.Code

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

DecodingOptions7+

图像解码设置选项。

系统能力:  以下各项对应的系统能力均为SystemCapability.Multimedia.Image.ImageSource

名称类型可读可写说明
sampleSizenumber缩略图采样大小。
rotatenumber旋转角度。
editableboolean是否可编辑。
desiredSize[Size]期望输出大小。
desiredRegion[Region]解码区域。
desiredPixelFormat[PixelMapFormat]解码的像素格式。
indexnumber解码图片序号。

Region7+

表示区域信息。

系统能力:  以下各项对应的系统能力均为SystemCapability.Multimedia.Image.Core

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

PackingOption

表示图片打包选项。

系统能力:  以下各项对应的系统能力均为SystemCapability.Multimedia.Image.ImagePacker

名称类型可读可写说明
formatstring目标格式。
qualitynumberJPEG编码中设定输出图片质量的参数,取值范围为1-100。

GetImagePropertyOptions7+

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

系统能力:  以下各项对应的系统能力均为SystemCapability.Multimedia.Image.ImageSource

名称类型可读可写说明
indexnumber图片序号。
defaultValuestring默认属性值。

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。

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

名称类型可读可写说明
componentType[ComponentType]组件类型。
rowStridenumber行距。
pixelStridenumber像素间距。
byteBufferArrayBuffer组件缓冲区。

ResponseCode

编译错误返回的响应码。

搜狗高速浏览器截图20240326151344.png

名称 HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿说明
ERR_MEDIA_INVALID_VALUE-1无效大小。
SUCCESS0操作成功。
ERROR62980096操作失败。
ERR_IPC62980097ipc错误。
ERR_SHAMEM_NOT_EXIST62980098共享内存错误。
ERR_SHAMEM_DATA_ABNORMAL62980099共享内存错误。
ERR_IMAGE_DECODE_ABNORMAL62980100图像解码错误。
ERR_IMAGE_DATA_ABNORMAL62980101图像输入数据错误。
ERR_IMAGE_MALLOC_ABNORMAL62980102图像malloc错误。
ERR_IMAGE_DATA_UNSUPPORT62980103不支持图像类型。
ERR_IMAGE_INIT_ABNORMAL62980104图像初始化错误。
ERR_IMAGE_GET_DATA_ABNORMAL62980105图像获取数据错误。
ERR_IMAGE_TOO_LARGE62980106图像数据太大。
ERR_IMAGE_TRANSFORM62980107图像转换错误。
ERR_IMAGE_COLOR_CONVERT62980108图像颜色转换错误。
ERR_IMAGE_CROP62980109裁剪错误。
ERR_IMAGE_SOURCE_DATA62980110图像源数据错误。
ERR_IMAGE_SOURCE_DATA_INCOMPLETE62980111图像源数据不完整。
ERR_IMAGE_MISMATCHED_FORMAT62980112图像格式不匹配。
ERR_IMAGE_UNKNOWN_FORMAT62980113图像未知格式。
ERR_IMAGE_SOURCE_UNRESOLVED62980114图像源未解析。
ERR_IMAGE_INVALID_PARAMETER62980115图像无效参数。
ERR_IMAGE_DECODE_FAILED62980116解码失败。
ERR_IMAGE_PLUGIN_REGISTER_FAILED62980117注册插件失败。
ERR_IMAGE_PLUGIN_CREATE_FAILED62980118创建插件失败。
ERR_IMAGE_ENCODE_FAILED62980119图像编码失败。
ERR_IMAGE_ADD_PIXEL_MAP_FAILED62980120图像添加像素映射失败。
ERR_IMAGE_HW_DECODE_UNSUPPORT62980121不支持图像硬件解码。
ERR_IMAGE_DECODE_HEAD_ABNORMAL62980122图像解码头错误。
ERR_IMAGE_DECODE_EXIF_UNSUPPORT62980123图像解码exif取消支持。
ERR_IMAGE_PROPERTY_NOT_EXIST62980124图像属性不存在;错误代码被媒体占用,图像从150开始。
ERR_IMAGE_READ_PIXELMAP_FAILED62980246读取像素地图失败。
ERR_IMAGE_WRITE_PIXELMAP_FAILED62980247写入像素映射失败。
ERR_IMAGE_PIXELMAP_NOT_ALLOW_MODIFY62980248pixelmap不允许修改。
ERR_IMAGE_CONFIG_FAILED62980259配置错误。

鸿蒙开发岗位需要掌握那些核心要领?

目前还有很多小伙伴不知道要学习哪些鸿蒙技术?不知道重点掌握哪些?为了避免学习时频繁踩坑,最终浪费大量时间的。

自己学习时必须要有一份实用的鸿蒙(Harmony NEXT)资料非常有必要。 这里我推荐,根据鸿蒙开发官网梳理与华为内部人员的分享总结出的开发文档。内容包含了:【ArkTS、ArkUI、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战】等技术知识点。

废话就不多说了,接下来好好看下这份资料。

如果你是一名Android、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。鸿蒙OpenHarmony知识←前往。下面是鸿蒙开发的学习路线图。

针对鸿蒙成长路线打造的鸿蒙学习文档。鸿蒙(OpenHarmony )学习手册(共计1236页)与鸿蒙(OpenHarmony )开发入门教学视频,帮助大家在技术的道路上更进一步。

其中内容包含:

《鸿蒙开发基础》鸿蒙OpenHarmony知识←前往

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

《鸿蒙开发进阶》鸿蒙OpenHarmony知识←前往

  1. Stage模型入门
  2. 网络管理
  3. 数据管理
  4. 电话服务
  5. 分布式应用开发
  6. 通知与窗口管理
  7. 多媒体技术
  8. 安全技能
  9. 任务管理
  10. WebGL
  11. 国际化开发
  12. 应用测试
  13. DFX面向未来设计
  14. 鸿蒙系统移植和裁剪定制
  15. ……

《鸿蒙开发实战》鸿蒙OpenHarmony知识←前往

  1. ArkTS实践
  2. UIAbility应用
  3. 网络案例
  4. ……

最后

鸿蒙是完全具备无与伦比的机遇和潜力的;预计到年底将有 5,000 款的应用完成原生鸿蒙开发,这么多的应用需要开发,也就意味着需要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行!

这篇关于鸿蒙开发接口媒体:【@ohos.multimedia.image (图片处理)】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1018539

相关文章

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON:

PyQt5 GUI 开发的基础知识

《PyQt5GUI开发的基础知识》Qt是一个跨平台的C++图形用户界面开发框架,支持GUI和非GUI程序开发,本文介绍了使用PyQt5进行界面开发的基础知识,包括创建简单窗口、常用控件、窗口属性设... 目录简介第一个PyQt程序最常用的三个功能模块控件QPushButton(按钮)控件QLable(纯文本

Spring Boot 中的默认异常处理机制及执行流程

《SpringBoot中的默认异常处理机制及执行流程》SpringBoot内置BasicErrorController,自动处理异常并生成HTML/JSON响应,支持自定义错误路径、配置及扩展,如... 目录Spring Boot 异常处理机制详解默认错误页面功能自动异常转换机制错误属性配置选项默认错误处理

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

Java堆转储文件之1.6G大文件处理完整指南

《Java堆转储文件之1.6G大文件处理完整指南》堆转储文件是优化、分析内存消耗的重要工具,:本文主要介绍Java堆转储文件之1.6G大文件处理的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言文件为什么这么大?如何处理这个文件?分析文件内容(推荐)删除文件(如果不需要)查看错误来源如何避

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处