harmony 鸿蒙@ohos.multimedia.image (Image Processing)
@ohos.multimedia.image (Image Processing)
The Image module provides APIs for image processing. You can use the APIs to create a PixelMap object with specified properties or read image pixel data (even in an area).
NOTE
The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Modules to Import
import image from '@ohos.multimedia.image';
image.createPixelMap8+
createPixelMap(colors: ArrayBuffer, options: InitializationOptions): Promise<PixelMap>
Creates a PixelMap object with the default BGRA_8888 format and pixel properties specified. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
colors | ArrayBuffer | Yes | Color array in BGRA_8888 format. |
options | InitializationOptions | Yes | Pixel properties, including the alpha type, size, scale mode, pixel format, and editable. |
Return value
Type | Description |
---|---|
Promise<PixelMap> | Promise used to return the PixelMap object. If the size of the created pixel map exceeds that of the original image, the pixel map size of the original image is returned. |
Example
import {BusinessError} from '@ohos.base'
const color : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 4.
let bufferArr : Uint8Array = new Uint8Array(color);
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.log('Failed to create pixelmap.');
})
image.createPixelMap8+
createPixelMap(colors: ArrayBuffer, options: InitializationOptions, callback: AsyncCallback<PixelMap>): void
Creates a PixelMap object with the default BGRA_8888 format and pixel properties specified. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
colors | ArrayBuffer | Yes | Color array in BGRA_8888 format. |
options | InitializationOptions | Yes | Pixel properties. |
callback | AsyncCallback<PixelMap> | Yes | Callback used to return the PixelMap object. |
Example
import {BusinessError} from '@ohos.base'
const color : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 4.
let bufferArr : Uint8Array = new Uint8Array(color);
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.log('Failed to create pixelmap.');
} else {
console.log('Succeeded in creating pixelmap.');
}
})
image.createPixelMapFromParcel11+
createPixelMapFromParcel(sequence: rpc.MessageSequence): PixelMap
Unmarshals a MessageSequence object to obtain a PixelMap object.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
sequence | rpc.MessageSequence | Yes | MessageSequence object that stores the PixelMap information. |
Return value
Type | Description |
---|---|
PixelMap | Returns a PixelMap object if the operation is successful; throws an error otherwise. |
Error codes
For details about the error codes, see Image Error Codes.
ID | Error Message |
---|---|
62980096 | If transaction operation failed |
62980097 | If the ipc error |
62980115 | If the input parameter invalid |
62980105 | Get data error |
62980177 | Napi environmental abnormality |
62980178 | Pixelmap create failed |
62980179 | Unmarshalling bufferSize parcelling error |
62980180 | Fd acquisition failed |
62980246 | Read pixelmap failed |
Example
import image from '@ohos.multimedia.image'
import rpc from '@ohos.rpc'
class MySequence implements rpc.Parcelable {
pixel_map;
constructor(pixelmap : image.PixelMap) {
this.pixel_map = pixelmap;
}
marshalling(messageSequence : rpc.MessageSequence) {
this.pixel_map.marshalling(messageSequence);
return true;
}
unmarshalling(messageSequence : rpc.MessageSequence) {
try {
this.pixel_map = image.createPixelMapFromParcel(messageSequence);
} catch(e) {
console.log('createPixelMapFromParcel error: '+ e);
}
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((pixelmap : image.PixelMap) => {
pixelMap = pixelmap;
})
if (pixelMap != undefined) {
// Implement serialization.
let parcelable : MySequence = new MySequence(pixelMap);
let data : rpc.MessageSequence = rpc.MessageSequence.create();
data.writeParcelable(parcelable);
// Deserialize to obtain data through the RPC.
let ret : MySequence = new MySequence(pixelMap);
data.readParcelable(ret);
// Obtain the PixelMap object.
let unmarshPixelmap = ret.pixel_map;
}
}
PixelMap7+
Provides APIs to read or write image pixel map data and obtain image pixel map information. Before calling any API in PixelMap, you must use createPixelMap to create a PixelMap object. Currently, the maximum size of a serialized pixel map is 128 MB. A larger size will cause a display failure. The size is calculated as follows: Width * Height * Number of bytes occupied by each pixel.
Attributes
System capability: SystemCapability.Multimedia.Image.Core
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
isEditable | boolean | Yes | No | Whether the image pixel map is editable. |
readPixelsToBuffer7+
readPixelsToBuffer(dst: ArrayBuffer): Promise<void>
Reads data of this pixel map and writes the data to an ArrayBuffer. This API uses a promise to return the result. If this pixel map is created in the BGRA_8888 format, the data read is the same as the original data.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
dst | ArrayBuffer | Yes | Buffer to which the image pixel map data will be written. The buffer size is obtained by calling getPixelBytesNumber. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
const readBuffer : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 4.
pixelmap.readPixelsToBuffer(readBuffer).then(() => {
console.log('Succeeded in reading image pixel data.'); // Called if the condition is met.
}).catch((error : BusinessError) => {
console.log('Failed to read image pixel data.'); // Called if no condition is met.
})
readPixelsToBuffer7+
readPixelsToBuffer(dst: ArrayBuffer, callback: AsyncCallback<void>): void
Reads data of this pixel map and writes the data to an ArrayBuffer. This API uses an asynchronous callback to return the result. If this pixel map is created in the BGRA_8888 format, the data read is the same as the original data.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
dst | ArrayBuffer | Yes | Buffer to which the image pixel map data will be written. The buffer size is obtained by calling getPixelBytesNumber. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
const readBuffer : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 4.
pixelmap.readPixelsToBuffer(readBuffer, (err : BusinessError, res : void) => {
if(err) {
console.log('Failed to read image pixel data.'); // Called if no condition is met.
} else {
console.log('Succeeded in reading image pixel data.'); // Called if the condition is met.
}
})
readPixels7+
readPixels(area: PositionArea): Promise<void>
Reads image pixel map data in an area. This API uses a promise to return the data read.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
area | PositionArea | Yes | Area from which the image pixel map data will be read. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. If the operation fails, an error message is returned. |
Example
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.'); // Called if the condition is met.
}).catch((error : BusinessError) => {
console.log('Failed to read the image data in the area.'); // Called if no condition is met.
})
readPixels7+
readPixels(area: PositionArea, callback: AsyncCallback<void>): void
Reads image pixel map data in an area. This API uses an asynchronous callback to return the data read.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
area | PositionArea | Yes | Area from which the image pixel map data will be read. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
const color : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 4.
let bufferArr : Uint8Array = new Uint8Array(color);
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.info('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.info('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>
Writes image pixel map data to an area. This API uses a promise to return the operation result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
area | PositionArea | Yes | Area to which the image pixel map data will be written. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
const color : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 4.
let bufferArr : Uint8Array = new Uint8Array(color);
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.info('createPixelMap failed.');
}
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.log('error: ' + error);
})
writePixels7+
writePixels(area: PositionArea, callback: AsyncCallback<void>): void
Writes image pixel map data to an area. This API uses an asynchronous callback to return the operation result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
area | PositionArea | Yes | Area to which the image pixel map data will be written. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation fails, an error message is returned. |
Example
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.info('Failed to write pixelmap into the specified area.');
} else {
console.info('Succeeded to write pixelmap into the specified area.');
}
})
writeBufferToPixels7+
writeBufferToPixels(src: ArrayBuffer): Promise<void>
Reads image data in an ArrayBuffer and writes the data to a PixelMap object. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
src | ArrayBuffer | Yes | Buffer from which the image data will be read. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
const color : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 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.");
})
writeBufferToPixels7+
writeBufferToPixels(src: ArrayBuffer, callback: AsyncCallback<void>): void
Reads image data in an ArrayBuffer and writes the data to a PixelMap object. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
src | ArrayBuffer | Yes | Buffer from which the image data will be read. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
const color : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 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.");
}
});
getImageInfo7+
getImageInfo(): Promise<ImageInfo>
Obtains pixel map information of this image. This API uses a promise to return the information.
System capability: SystemCapability.Multimedia.Image.Core
Return value
Type | Description |
---|---|
Promise<ImageInfo> | Promise used to return the pixel map information. If the operation fails, an error message is returned. |
Example
const color : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 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
Obtains pixel map information of this image. This API uses an asynchronous callback to return the information.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<ImageInfo> | Yes | Callback used to return the pixel map information. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
const color : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 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
Obtains the number of bytes per row of this image pixel map.
System capability: SystemCapability.Multimedia.Image.Core
Return value
Type | Description |
---|---|
number | Number of bytes per row. |
Example
import {BusinessError} from '@ohos.base'
const color : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 4.
let bufferArr : Uint8Array = new Uint8Array(color);
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
Obtains the total number of bytes of this image pixel map.
System capability: SystemCapability.Multimedia.Image.Core
Return value
Type | Description |
---|---|
number | Total number of bytes. |
Example
let pixelBytesNumber : number = pixelmap.getPixelBytesNumber();
getDensity9+
getDensity():number
Obtains the density of this image pixel map.
System capability: SystemCapability.Multimedia.Image.Core
Return value
Type | Description |
---|---|
number | Density of the image pixel map. |
Example
let getDensity : number = pixelmap.getDensity();
opacity9+
opacity(rate: number, callback: AsyncCallback<void>): void
Sets an opacity rate for this image pixel map. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
rate | number | Yes | Opacity rate to set. The value ranges from 0 to 1. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation fails, an error message is returned. |
Example
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.");
}
})
opacity9+
opacity(rate: number): Promise<void>
Sets an opacity rate for this image pixel map. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
rate | number | Yes | Opacity rate to set. The value ranges from 0 to 1. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. If the operation fails, an error message is returned. |
Example
async function Demo() {
await pixelmap.opacity(0.5);
}
createAlphaPixelmap9+
createAlphaPixelmap(): Promise<PixelMap>
Creates a PixelMap object that contains only the alpha channel information. This object can be used for the shadow effect. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Return value
Type | Description |
---|---|
Promise<PixelMap> | Promise used to return the PixelMap object. |
Example
async function Demo() {
await pixelmap.createAlphaPixelmap();
}
createAlphaPixelmap9+
createAlphaPixelmap(callback: AsyncCallback<PixelMap>): void
Creates a PixelMap object that contains only the alpha channel information. This object can be used for the shadow effect. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<PixelMap> | Yes | Callback used to return the PixelMap object. |
Example
import {BusinessError} from '@ohos.base'
pixelmap.createAlphaPixelmap((err : BusinessError, alphaPixelMap : image.PixelMap) => {
if (alphaPixelMap == undefined) {
console.info('Failed to obtain new pixel map.');
} else {
console.info('Succeed in obtaining new pixel map.');
}
})
scale9+
scale(x: number, y: number, callback: AsyncCallback<void>): void
Scales this image based on the input width and height. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
x | number | Yes | Scaling ratio of the width. |
y | number | Yes | Scaling ratio of the height. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation fails, an error message is returned. |
Example
async function Demo() {
await pixelmap.scale(2.0, 1.0);
}
scale9+
scale(x: number, y: number): Promise<void>
Scales this image based on the input width and height. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
x | number | Yes | Scaling ratio of the width. |
y | number | Yes | Scaling ratio of the height. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. |
Example
async function Demo() {
await pixelmap.scale(2.0, 1.0);
}
translate9+
translate(x: number, y: number, callback: AsyncCallback<void>): void
Translates this image based on the input coordinates. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
x | number | Yes | X coordinate to translate. |
y | number | Yes | Y coordinate to translate. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation fails, an error message is returned. |
Example
async function Demo() {
await pixelmap.translate(3.0, 1.0);
}
translate9+
translate(x: number, y: number): Promise<void>
Translates this image based on the input coordinates. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
x | number | Yes | X coordinate to translate. |
y | number | Yes | Y coordinate to translate. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. |
Example
async function Demo() {
await pixelmap.translate(3.0, 1.0);
}
rotate9+
rotate(angle: number, callback: AsyncCallback<void>): void
Rotates this image based on the input angle. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
angle | number | Yes | Angle to rotate. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation fails, an error message is returned. |
Example
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.");
}
})
rotate9+
rotate(angle: number): Promise<void>
Rotates this image based on the input angle. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
angle | number | Yes | Angle to rotate. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. |
Example
async function Demo() {
await pixelmap.rotate(90.0);
}
flip9+
flip(horizontal: boolean, vertical: boolean, callback: AsyncCallback<void>): void
Flips this image horizontally or vertically, or both. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
horizontal | boolean | Yes | Whether to flip the image horizontally. |
vertical | boolean | Yes | Whether to flip the image vertically. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation fails, an error message is returned. |
Example
async function Demo() {
await pixelmap.flip(false, true);
}
flip9+
flip(horizontal: boolean, vertical: boolean): Promise<void>
Flips this image horizontally or vertically, or both. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
horizontal | boolean | Yes | Whether to flip the image horizontally. |
vertical | boolean | Yes | Whether to flip the image vertically. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. |
Example
async function Demo() {
await pixelmap.flip(false, true);
}
crop9+
crop(region: Region, callback: AsyncCallback<void>): void
Crops this image based on the input size. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
region | Region | Yes | Size of the image after cropping. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation fails, an error message is returned. |
Example
async function Demo() {
await pixelmap.crop({ x: 0, y: 0, size: { height: 100, width: 100 } } as image.Region);
}
crop9+
crop(region: Region): Promise<void>
Crops this image based on the input size. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
region | Region | Yes | Size of the image after cropping. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. |
Example
async function Demo() {
await pixelmap.crop({ x: 0, y: 0, size: { height: 100, width: 100 } } as image.Region);
}
getColorSpace10+
getColorSpace(): colorSpaceManager.ColorSpaceManager
Obtains the color space of this image.
System capability: SystemCapability.Multimedia.Image.Core
Return value
Type | Description |
---|---|
colorSpaceManager.ColorSpaceManager | Color space obtained. |
Error codes
For details about the error codes, see Image Error Codes.
ID | Error Message |
---|---|
62980101 | If the image data abnormal |
62980103 | If the image data unsupport |
62980115 | If the image parameter invalid |
Example
import colorSpaceManager from '@ohos.graphics.colorSpaceManager';
async function Demo() {
let csm : Object = pixelmap.getColorSpace();
}
setColorSpace10+
setColorSpace(colorSpace: colorSpaceManager.ColorSpaceManager): void
Sets the color space for this image.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
colorSpace | colorSpaceManager.ColorSpaceManager | Yes | Color space to set. |
Error codes
For details about the error codes, see Image Error Codes.
ID | Error Message |
---|---|
62980111 | If the operation invalid |
62980115 | If the image parameter invalid |
Example
import colorSpaceManager from '@ohos.graphics.colorSpaceManager';
async function Demo() {
let colorSpaceName = colorSpaceManager.ColorSpace.SRGB;
let csm : colorSpaceManager.ColorSpaceManager = colorSpaceManager.create(colorSpaceName);
pixelmap.setColorSpace(csm);
}
marshalling10+
marshalling(sequence: rpc.MessageSequence): void
Marshals this PixelMap object and writes it to a MessageSequence object.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
sequence | rpc.MessageSequence | Yes | MessageSequence object. |
Error codes
For details about the error codes, see Image Error Codes.
ID | Error Message |
---|---|
62980115 | If the input parameter invalid |
62980097 | If the ipc error |
Example
import image from '@ohos.multimedia.image'
import rpc from '@ohos.rpc'
class MySequence implements rpc.Parcelable {
pixel_map;
constructor(pixelmap : image.PixelMap) {
this.pixel_map = pixelmap;
}
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((pixelmap : image.PixelMap) => {
pixelMap = pixelmap;
})
if (pixelMap != undefined) {
// Implement serialization.
let parcelable : MySequence = new MySequence(pixelMap);
let data : rpc.MessageSequence = rpc.MessageSequence.create();
data.writeParcelable(parcelable);
// Deserialize to obtain data through the RPC.
let ret : MySequence = new MySequence(pixelMap);
data.readParcelable(ret);
}
}
unmarshalling10+
unmarshalling(sequence: rpc.MessageSequence): Promise<PixelMap>
Unmarshals a MessageSequence object to obtain a PixelMap object. To create a PixelMap object in synchronous mode, use createPixelMapFromParcel.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
sequence | rpc.MessageSequence | Yes | MessageSequence object that stores the PixelMap information. |
Return value
Type | Description |
---|---|
Promise<PixelMap> | Promise used to return the PixelMap object. |
Error codes
For details about the error codes, see Image Error Codes.
ID | Error Message |
---|---|
62980115 | If the input parameter invalid |
62980097 | If the ipc error |
62980096 | If fail to create async work |
Example
import image from '@ohos.multimedia.image'
import rpc from '@ohos.rpc'
class MySequence implements rpc.Parcelable {
pixel_map;
constructor(pixelmap : image.PixelMap) {
this.pixel_map = pixelmap;
}
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((pixelmap : image.PixelMap) => {
pixelMap = pixelmap;
})
if (pixelMap != undefined) {
// Implement serialization.
let parcelable : MySequence = new MySequence(pixelMap);
let data : rpc.MessageSequence = rpc.MessageSequence.create();
data.writeParcelable(parcelable);
// Deserialize to obtain data through the RPC.
let ret : MySequence = new MySequence(pixelMap);
data.readParcelable(ret);
}
}
release7+
release():Promise<void>
Releases this PixelMap object. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. |
Example
import {BusinessError} from '@ohos.base'
pixelmap.release().then(() => {
console.log('Succeeded in releasing pixelmap object.');
}).catch((error : BusinessError) => {
console.log('Failed to release pixelmap object.');
})
release7+
release(callback: AsyncCallback<void>): void
Releases this PixelMap object. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<void> | Yes | Callback used to return the result. |
Example
import {BusinessError} from '@ohos.base'
pixelmap.release((err : BusinessError) => {
if (err != undefined) {
console.log('Failed to release pixelmap object.');
} else {
console.log('Succeeded in releasing pixelmap object.');
}
})
image.createImageSource
createImageSource(uri: string): ImageSource
Creates an ImageSource instance based on the URI.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
uri | string | Yes | Image path. Currently, only the application sandbox path is supported. Currently, the following formats are supported: .jpg, .png, .gif, .bmp, .webp, and raw. For details, see SVG Tags10+. |
Return value
Type | Description |
---|---|
ImageSource | Returns the ImageSource instance if the operation is successful; returns undefined otherwise. |
Example
// Stage model
const context : Context = getContext(this);
const path : string = context.cacheDir + "/test.jpg";
const imageSourceApi : image.ImageSource = image.createImageSource(path);
// FA model
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
Creates an ImageSource instance based on the URI.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
uri | string | Yes | Image path. Currently, only the application sandbox path is supported. Currently, the following formats are supported: .jpg, .png, .gif, .bmp, .webp, and raw. For details, see SVG Tags10+. |
options | SourceOptions | Yes | Image properties, including the image index and default property value. |
Return value
Type | Description |
---|---|
ImageSource | Returns the ImageSource instance if the operation is successful; returns undefined otherwise. |
Example
let sourceOptions : image.SourceOptions = { sourceDensity: 120 };
let imageSource : image.ImageSource = image.createImageSource('test.png', sourceOptions);
image.createImageSource7+
createImageSource(fd: number): ImageSource
Creates an ImageSource instance based on the file descriptor.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
fd | number | Yes | File descriptor. |
Return value
Type | Description |
---|---|
ImageSource | Returns the ImageSource instance if the operation is successful; returns undefined otherwise. |
Example
const imageSourceApi : image.ImageSource = image.createImageSource(0);
image.createImageSource9+
createImageSource(fd: number, options: SourceOptions): ImageSource
Creates an ImageSource instance based on the file descriptor.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
fd | number | Yes | File descriptor. |
options | SourceOptions | Yes | Image properties, including the image index and default property value. |
Return value
Type | Description |
---|---|
ImageSource | Returns the ImageSource instance if the operation is successful; returns undefined otherwise. |
Example
let sourceOptions : image.SourceOptions = { sourceDensity: 120 };
const imageSourceApi : image.ImageSource = image.createImageSource(0, sourceOptions);
image.createImageSource9+
createImageSource(buf: ArrayBuffer): ImageSource
Creates an ImageSource instance based on the buffers.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
buf | ArrayBuffer | Yes | Array of image buffers. |
Example
const buf : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 4.
const imageSourceApi : image.ImageSource = image.createImageSource(buf);
image.createImageSource9+
createImageSource(buf: ArrayBuffer, options: SourceOptions): ImageSource
Creates an ImageSource instance based on the buffers.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
buf | ArrayBuffer | Yes | Array of image buffers. |
options | SourceOptions | Yes | Image properties, including the image index and default property value. |
Return value
Type | Description |
---|---|
ImageSource | Returns the ImageSource instance if the operation is successful; returns undefined otherwise. |
Example
const data : ArrayBuffer= new ArrayBuffer(112);
const imageSourceApi : image.ImageSource = image.createImageSource(data);
image.CreateIncrementalSource9+
CreateIncrementalSource(buf: ArrayBuffer): ImageSource
Creates an ImageSource instance in incremental mode based on the buffers.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
buf | ArrayBuffer | Yes | Incremental data. |
Return value
Type | Description |
---|---|
ImageSource | Returns the ImageSource instance if the operation is successful; returns undefined otherwise. |
Example
const buf : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 4.
const imageSourceIncrementalSApi : image.ImageSource = image.CreateIncrementalSource(buf);
image.CreateIncrementalSource9+
CreateIncrementalSource(buf: ArrayBuffer, options?: SourceOptions): ImageSource
Creates an ImageSource instance in incremental mode based on the buffers.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
buf | ArrayBuffer | Yes | Incremental data. |
options | SourceOptions | No | Image properties, including the image index and default property value. |
Return value
Type | Description |
---|---|
ImageSource | Returns the ImageSource instance if the operation is successful; returns undefined otherwise. |
Example
const buf : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 4.
const imageSourceIncrementalSApi : image.ImageSource = image.CreateIncrementalSource(buf);
ImageSource
Provides APIs to obtain image information. Before calling any API in ImageSource, you must use createImageSource to create an ImageSource instance.
Attributes
System capability: SystemCapability.Multimedia.Image.ImageSource
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
supportedFormats | Array<string> | Yes | No | Supported image formats, including PNG, JPEG, BMP, GIF, WebP, and RAW. |
getImageInfo
getImageInfo(index: number, callback: AsyncCallback<ImageInfo>): void
Obtains information about an image with the specified index. This API uses an asynchronous callback to return the information.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
index | number | Yes | Index of the image. |
callback | AsyncCallback<ImageInfo> | Yes | Callback used to return the image information. |
Example
import {BusinessError} from '@ohos.base'
imageSourceApi.getImageInfo(0,(error : BusinessError, imageInfo : image.ImageInfo) => {
if(error) {
console.log('getImageInfo failed.');
} else {
console.log('getImageInfo succeeded.');
}
})
getImageInfo
getImageInfo(callback: AsyncCallback<ImageInfo>): void
Obtains information about this image. This API uses an asynchronous callback to return the information.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<ImageInfo> | Yes | Callback used to return the image information. |
Example
import {BusinessError} from '@ohos.base'
imageSourceApi.getImageInfo((err : BusinessError, imageInfo : image.ImageInfo) => {
console.log('Succeeded in obtaining the image information.');
})
getImageInfo
getImageInfo(index?: number): Promise<ImageInfo>
Obtains information about an image with the specified index. This API uses a promise to return the image information.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
index | number | No | Index of the image. If this parameter is not set, the default value 0 is used. |
Return value
Type | Description |
---|---|
Promise<ImageInfo> | Promise used to return the image information. |
Example
import {BusinessError} from '@ohos.base'
imageSourceApi.getImageInfo(0)
.then((imageInfo : image.ImageInfo) => {
console.log('Succeeded in obtaining the image information.');
}).catch((error : BusinessError) => {
console.log('Failed to obtain the image information.');
})
getImageProperty7+
getImageProperty(key:string, options?: GetImagePropertyOptions): Promise<string>
Obtains the value of a property with the specified index in this image. This API uses a promise to return the result. The image must be in JPEG format and contain EXIF information.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
key | string | Yes | Name of the property. |
options | GetImagePropertyOptions | No | Image properties, including the image index and default property value. |
Return value
Type | Description |
---|---|
Promise<string> | Promise used to return the property value. If the operation fails, the default value is returned. |
Example
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
Obtains the value of a property with the specified index in this image. This API uses an asynchronous callback to return the result. The image must be in JPEG format and contain EXIF information.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
key | string | Yes | Name of the property. |
callback | AsyncCallback<string> | Yes | Callback used to return the property value. If the operation fails, the default value is returned. |
Example
import {BusinessError} from '@ohos.base'
imageSourceApi.getImageProperty("BitsPerSample",(error : BusinessError, data : string) => {
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
Obtains the value of a property in this image. This API uses an asynchronous callback to return the property value in a string. The image must be in JPEG format and contain EXIF information.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
key | string | Yes | Name of the property. |
options | GetImagePropertyOptions | Yes | Image properties, including the image index and default property value. |
callback | AsyncCallback<string> | Yes | Callback used to return the property value. If the operation fails, the default value is returned. |
Example
import {BusinessError} from '@ohos.base'
let property : image.GetImagePropertyOptions = { index: 0, defaultValue: '9999' }
imageSourceApi.getImageProperty("BitsPerSample",property,(error : BusinessError, data : string) => {
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>
Modifies the value of a property in this image. This API uses a promise to return the result. The image must be in JPEG format and contain EXIF information.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
key | string | Yes | Name of the property. |
value | string | Yes | New value of the property. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. |
Example
imageSourceApi.modifyImageProperty("ImageWidth", "120").then(() => {
imageSourceApi.getImageProperty("ImageWidth").then( (w : string) => {
console.info('w', w);
})
})
modifyImageProperty9+
modifyImageProperty(key: string, value: string, callback: AsyncCallback<void>): void
Modifies the value of a property in this image. This API uses an asynchronous callback to return the result. The image must be in JPEG format and contain EXIF information.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
key | string | Yes | Name of the property. |
value | string | Yes | New value of the property. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. |
Example
import {BusinessError} from '@ohos.base'
imageSourceApi.modifyImageProperty("ImageWidth", "120",(err : BusinessError) => {
if (err != undefined) {
console.info('modifyImageProperty Failed');
} else {
console.info('modifyImageProperty Succeeded');
}
})
updateData9+
updateData(buf: ArrayBuffer, isFinished: boolean, value: number, length: number): Promise<void>
Updates incremental data. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
buf | ArrayBuffer | Yes | Incremental data. |
isFinished | boolean | Yes | Whether the update is complete. |
value | number | Yes | Offset for data reading. |
length | number | Yes | Array length. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. |
Example
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}`);
})
updateData9+
updateData(buf: ArrayBuffer, isFinished: boolean, value: number, length: number, callback: AsyncCallback<void>): void
Updates incremental data. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
buf | ArrayBuffer | Yes | Incremental data. |
isFinished | boolean | Yes | Whether the update is complete. |
value | number | Yes | Offset for data reading. |
length | number | Yes | Array length. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. |
Example
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.');
}
})
createPixelMap7+
createPixelMap(options?: DecodingOptions): Promise<PixelMap>
Creates a PixelMap object based on image decoding parameters. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
options | DecodingOptions | No | Image decoding parameters. |
Return value
Type | Description |
---|---|
Promise<PixelMap> | Promise used to return the PixelMap object. |
Example
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.log('Failed to create pixelmap object through image decoding parameters.');
})
createPixelMap7+
createPixelMap(callback: AsyncCallback<PixelMap>): void
Creates a PixelMap object based on the default parameters. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<PixelMap> | Yes | Callback used to return the PixelMap object. |
Example
import {BusinessError} from '@ohos.base'
imageSourceApi.createPixelMap((err : BusinessError, pixelmap : image.PixelMap) => {
console.info('Succeeded in creating pixelmap object.');
})
createPixelMap7+
createPixelMap(options: DecodingOptions, callback: AsyncCallback<PixelMap>): void
Creates a PixelMap object based on image decoding parameters. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
options | DecodingOptions | Yes | Image decoding parameters. |
callback | AsyncCallback<PixelMap> | Yes | Callback used to return the PixelMap object. |
Example
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.');
})
createPixelMapList10+
createPixelMapList(options?: DecodingOptions): Promise
Creates an array of PixelMap objects based on image decoding parameters. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
options | DecodingOptions | No | Image decoding parameters. |
Return value
Type | Description |
---|---|
Promise |
Promise used to return an array of PixelMap objects. |
Error codes
For details about the error codes, see Image Error Codes.
ID | Error Message |
---|---|
62980096 | If the operation failed |
62980103 | If the image data unsupport |
62980110 | If the image source data error |
62980111 | If the image source data incomplete |
62980118 | If the image plugin create failed |
Example
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
Creates an array of PixelMap objects based on the default parameters. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback |
Yes | Callback used to return an array of PixelMap objects. |
Error codes
For details about the error codes, see Image Error Codes.
ID | Error Message |
---|---|
62980096 | If the operation failed |
62980103 | If the image data unsupport |
62980110 | If the image source data error |
62980111 | If the image source data incomplete |
62980118 | If the image plugin create failed |
Example
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
Creates an array of PixelMap objects based on image decoding parameters. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
options | DecodingOptions | Yes | Image decoding parameters. |
callback | AsyncCallback |
Yes | Callback used to return an array of PixelMap objects. |
Error codes
For details about the error codes, see Image Error Codes.
ID | Error Message |
---|---|
62980096 | If the operation failed |
62980103 | If the image data unsupport |
62980110 | If the image source data error |
62980111 | If the image source data incomplete |
62980118 | If the image plugin create failed |
Example
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.');
}
})
getDelayTimeList10+
getDelayTimeList(callback: AsyncCallback
Obtains an array of delay times. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback |
Yes | Callback used to return an array of delay times. |
Error codes
For details about the error codes, see Image Error Codes.
ID | Error Message |
---|---|
62980096 | If the operation failed |
62980110 | If the image source data error |
62980111 | If the image source data incomplete |
62980113 | If the image format unknown |
62980116 | If the image decode failed |
62980118 | If the image plugin create failed |
62980122 | If the image decode head abnormal |
Example
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.');
}
})
getDelayTimeList10+
getDelayTimeList(): Promise
Obtains an array of delay times. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImageSource
Return value
Type | Description |
---|---|
Promise |
Promise used to return an array of delay times. |
Error codes
For details about the error codes, see Image Error Codes.
ID | Error Message |
---|---|
62980096 | If the operation failed |
62980110 | If the image source data error |
62980111 | If the image source data incomplete |
62980113 | If the image format unknown |
62980116 | If the image decode failed |
62980118 | If the image plugin create failed |
62980122 | If the image decode head abnormal |
Example
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}`);
})
getFrameCount10+
getFrameCount(callback: AsyncCallback<number>): void;
Obtains the number of frames. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<number> | Yes | Callback used to return the number of frames. |
Error codes
For details about the error codes, see Image Error Codes.
ID | Error Message |
---|---|
62980096 | If the operation failed |
62980110 | If the image source data error |
62980111 | If the image source data incomplete |
62980113 | If the image format unknown |
62980116 | If the image decode failed |
62980118 | If the image plugin create failed |
62980122 | If the image decode head abnormal |
Example
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.');
}
})
getFrameCount10+
getFrameCount(): Promise<number>;
Obtains the number of frames. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImageSource
Return value
Type | Description |
---|---|
Promise<number> | Promise used to return the number of frames. |
Error codes
For details about the error codes, see Image Error Codes.
ID | Error Message |
---|---|
62980096 | If the operation failed |
62980110 | If the image source data error |
62980111 | If the image source data incomplete |
62980113 | If the image format unknown |
62980116 | If the image decode failed |
62980118 | If the image plugin create failed |
62980122 | If the image decode head abnormal |
Example
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
Releases this ImageSource instance. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageSource
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<void> | Yes | Callback invoked for instance release. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
imageSourceApi.release((err : BusinessError) => {
if (err != undefined) {
console.log('Failed to release the image source instance.');
} else {
console.log('Succeeded in releasing the image source instance.');
}
})
release
release(): Promise<void>
Releases this ImageSource instance. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImageSource
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. |
Example
import {BusinessError} from '@ohos.base'
imageSourceApi.release().then(()=>{
console.log('Succeeded in releasing the image source instance.');
}).catch((error : BusinessError) => {
console.log('Failed to release the image source instance.');
})
image.createImagePacker
createImagePacker(): ImagePacker
Creates an ImagePacker instance.
System capability: SystemCapability.Multimedia.Image.ImagePacker
Return value
Type | Description |
---|---|
ImagePacker | ImagePacker instance created. |
Example
const imagePackerApi : image.ImagePacker = image.createImagePacker();
ImagePacker
Provides APIs to pack images. Before calling any API in ImagePacker, you must use createImagePacker to create an ImagePacker instance. The image formats JPEG, WebP, and PNG are supported.
Attributes
System capability: SystemCapability.Multimedia.Image.ImagePacker
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
supportedFormats | Array<string> | Yes | No | Supported image formats, which can be JPEG, WebP, and PNG. |
packing
packing(source: ImageSource, option: PackingOption, callback: AsyncCallback<ArrayBuffer>): void
Packs an image. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImagePacker
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
source | ImageSource | Yes | Image to pack. |
option | PackingOption | Yes | Option for image packing. |
callback | AsyncCallback<ArrayBuffer> | Yes | Callback used to return the packed data. |
Example
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) => {})
packing
packing(source: ImageSource, option: PackingOption): Promise<ArrayBuffer>
Packs an image. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImagePacker
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
source | ImageSource | Yes | Image to pack. |
option | PackingOption | Yes | Option for image packing. |
Return value
Type | Description |
---|---|
Promise<ArrayBuffer> | Promise used to return the packed data. |
Example
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.log('packing failed.');
})
packing8+
packing(source: PixelMap, option: PackingOption, callback: AsyncCallback<ArrayBuffer>): void
Packs an image. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImagePacker
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
source | PixelMap | Yes | PixelMap object to pack. |
option | PackingOption | Yes | Option for image packing. |
callback | AsyncCallback<ArrayBuffer> | Yes | Callback used to return the packed data. |
Example
import {BusinessError} from '@ohos.base'
const color : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 4.
let bufferArr : Uint8Array = new Uint8Array(color);
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.');
})
})
packing8+
packing(source: PixelMap, option: PackingOption): Promise<ArrayBuffer>
Packs an image. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImagePacker
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
source | PixelMap | Yes | PixelMap object to pack. |
option | PackingOption | Yes | Option for image packing. |
Return value
Type | Description |
---|---|
Promise<ArrayBuffer> | Promise used to return the packed data. |
Example
import {BusinessError} from '@ohos.base'
const color : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel map buffer to create. The value is calculated as follows: height x width x 4.
let bufferArr : Uint8Array = new Uint8Array(color);
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.log('Failed to pack the image..');
})
})
release
release(callback: AsyncCallback<void>): void
Releases this ImagePacker instance. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImagePacker
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<void> | Yes | Callback invoked for instance release. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
imagePackerApi.release((err : BusinessError)=>{
if (err != undefined) {
console.log('Failed to release image packaging.');
} else {
console.log('Succeeded in releasing image packaging.');
}
})
release
release(): Promise<void>
Releases this ImagePacker instance. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImagePacker
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the instance release result. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
imagePackerApi.release().then(()=>{
console.log('Succeeded in releasing image packaging.');
}).catch((error : BusinessError)=>{
console.log('Failed to release image packaging.');
})
image.createImageReceiver9+
createImageReceiver(width: number, height: number, format: number, capacity: number): ImageReceiver
Creates an ImageReceiver instance by specifying the image width, height, format, and capacity.
System capability: SystemCapability.Multimedia.Image.ImageReceiver
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
width | number | Yes | Default image width. |
height | number | Yes | Default image height. |
format | number | Yes | Image format, which is a constant of ImageFormat. (Only ImageFormat:JPEG and 4 are supported.) |
capacity | number | Yes | Maximum number of images that can be accessed at the same time. |
Return value
Type | Description |
---|---|
ImageReceiver | Returns an ImageReceiver instance if the operation is successful. |
Example
let receiver : image.ImageReceiver = image.createImageReceiver(8192, 8, 2000, 8);
ImageReceiver9+
Provides APIs to obtain the surface ID of a component, read the latest image, read the next image, and release the ImageReceiver instance.
Before calling any APIs in ImageReceiver, you must create an ImageReceiver instance.
Attributes
System capability: SystemCapability.Multimedia.Image.ImageReceiver
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
size | Size | Yes | No | Image size. |
capacity | number | Yes | No | Maximum number of images that can be accessed at the same time. |
format | ImageFormat | Yes | No | Image format. |
getReceivingSurfaceId9+
getReceivingSurfaceId(callback: AsyncCallback<string>): void
Obtains a surface ID for the camera or other components. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageReceiver
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<string> | Yes | Callback used to return the surface ID. |
Example
import {BusinessError} from '@ohos.base'
receiver.getReceivingSurfaceId((err : BusinessError, id : string) => {
if(err) {
console.log('getReceivingSurfaceId failed.');
} else {
console.log('getReceivingSurfaceId succeeded.');
}
});
getReceivingSurfaceId9+
getReceivingSurfaceId(): Promise<string>
Obtains a surface ID for the camera or other components. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImageReceiver
Return value
Type | Description |
---|---|
Promise<string> | Promise used to return the surface ID. |
Example
import {BusinessError} from '@ohos.base'
receiver.getReceivingSurfaceId().then( (id : string) => {
console.log('getReceivingSurfaceId succeeded.');
}).catch((error : BusinessError) => {
console.log('getReceivingSurfaceId failed.');
})
readLatestImage9+
readLatestImage(callback: AsyncCallback<Image>): void
Reads the latest image from the ImageReceiver instance. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageReceiver
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<Image> | Yes | Callback used to return the latest image. |
Example
import {BusinessError} from '@ohos.base'
receiver.readLatestImage((err : BusinessError, img : image.Image) => {
if(err) {
console.log('readLatestImage failed.');
} else {
console.log('readLatestImage succeeded.');
}
});
readLatestImage9+
readLatestImage(): Promise<Image>
Reads the latest image from the ImageReceiver instance. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImageReceiver
Return value
Type | Description |
---|---|
Promise<Image> | Promise used to return the latest image. |
Example
import {BusinessError} from '@ohos.base'
receiver.readLatestImage().then((img : image.Image) => {
console.log('readLatestImage succeeded.');
}).catch((error : BusinessError) => {
console.log('readLatestImage failed.');
})
readNextImage9+
readNextImage(callback: AsyncCallback<Image>): void
Reads the next image from the ImageReceiver instance. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageReceiver
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<Image> | Yes | Callback used to return the next image. |
Example
import {BusinessError} from '@ohos.base'
receiver.readNextImage((err : BusinessError, img : image.Image) => {
if(err) {
console.log('readNextImage failed.');
} else {
console.log('readNextImage succeeded.');
}
});
readNextImage9+
readNextImage(): Promise<Image>
Reads the next image from the ImageReceiver instance. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImageReceiver
Return value
Type | Description |
---|---|
Promise<Image> | Promise used to return the next image. |
Example
import {BusinessError} from '@ohos.base'
receiver.readNextImage().then((img : image.Image) => {
console.log('readNextImage succeeded.');
}).catch((error : BusinessError) => {
console.log('readNextImage failed.');
})
on9+
on(type: ‘imageArrival’, callback: AsyncCallback<void>): void
Listens for image arrival events.
System capability: SystemCapability.Multimedia.Image.ImageReceiver
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | string | Yes | Type of event to listen for. The value is fixed at imageArrival, which is triggered when an image is received. |
callback | AsyncCallback<void> | Yes | Callback invoked for the event. |
Example
receiver.on('imageArrival', () => {})
release9+
release(callback: AsyncCallback<void>): void
Releases this ImageReceiver instance. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageReceiver
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<void> | Yes | Callback used to return the result. |
Example
import {BusinessError} from '@ohos.base'
receiver.release((err : BusinessError) => {})
release9+
release(): Promise<void>
Releases this ImageReceiver instance. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImageReceiver
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. |
Example
import {BusinessError} from '@ohos.base'
receiver.release().then(() => {
console.log('release succeeded.');
}).catch((error : BusinessError) => {
console.log('release failed.');
})
image.createImageCreator9+
createImageCreator(width: number, height: number, format: number, capacity: number): ImageCreator
Creates an ImageCreator instance by specifying the image width, height, format, and capacity.
System capability: SystemCapability.Multimedia.Image.ImageCreator
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
width | number | Yes | Default image width. |
height | number | Yes | Default image height. |
format | number | Yes | Image format, for example, YCBCR_422_SP or JPEG. |
capacity | number | Yes | Maximum number of images that can be accessed at the same time. |
Return value
Type | Description |
---|---|
ImageCreator | Returns an ImageCreator instance if the operation is successful. |
Example
let creator : image.ImageCreator = image.createImageCreator(8192, 8, 4, 8);
ImageCreator9+
Provides APIs for applications to request an image native data area and compile native image data. Before calling any APIs in ImageCreator, you must create an ImageCreator instance. ImageCreator does not support multiple threads.
Attributes
System capability: SystemCapability.Multimedia.Image.ImageCreator
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
capacity | number | Yes | No | Maximum number of images that can be accessed at the same time. |
format | ImageFormat | Yes | No | Image format. |
dequeueImage9+
dequeueImage(callback: AsyncCallback<Image>): void
Obtains an image buffer from the idle queue and writes image data into it. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageCreator
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<Image> | Yes | Callback used to return the drawn image. |
Example
import {BusinessError} from '@ohos.base'
creator.dequeueImage((err : BusinessError, img : image.Image) => {
if (err) {
console.info('dequeueImage failed.');
}
console.info('dequeueImage succeeded.');
});
dequeueImage9+
dequeueImage(): Promise<Image>
Obtains an image buffer from the idle queue and writes image data into it. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImageCreator
Return value
Type | Description |
---|---|
Promise<Image> | Promise used to return the drawn image. |
Example
import {BusinessError} from '@ohos.base'
creator.dequeueImage().then((img : image.Image) => {
console.info('dequeueImage succeeded.');
}).catch((error : BusinessError) => {
console.log('dequeueImage failed: ' + error);
})
queueImage9+
queueImage(interface: Image, callback: AsyncCallback<void>): void
Places the drawn image in the dirty queue. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageCreator
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
interface | Image | Yes | Drawn image. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
creator.dequeueImage().then((img : image.Image) => {
// Draw the 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.info('queueImage failed: ' + err);
}
console.info('queueImage succeeded');
})
})
queueImage9+
queueImage(interface: Image): Promise<void>
Places the drawn image in the dirty queue. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImageCreator
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
interface | Image | Yes | Drawn image. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
creator.dequeueImage().then((img : image.Image) => {
// Draw the 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.info('queueImage failed: ' + error);
})
})
on9+
on(type: ‘imageRelease’, callback: AsyncCallback<void>): void
Listens for image release events. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageCreator
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | string | Yes | Type of event, which is ‘imageRelease’. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
creator.on('imageRelease', (err : BusinessError) => {
if (err) {
console.info('on faild' + err);
}
console.info('on succeeded');
})
release9+
release(callback: AsyncCallback<void>): void
Releases this ImageCreator instance. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.ImageCreator
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
creator.release((err : BusinessError) => {
if (err) {
console.info('release failed: ' + err);
}
console.info('release succeeded');
});
release9+
release(): Promise<void>
Releases this ImageCreator instance. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.ImageCreator
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. If the operation fails, an error message is returned. |
Example
import {BusinessError} from '@ohos.base'
creator.release().then(() => {
console.info('release succeeded');
}).catch((error : BusinessError) => {
console.info('release failed');
})
Image9+
Provides APIs for basic image operations, including obtaining image information and reading and writing image data. An Image instance is returned when readNextImage and readLatestImage are called.
Attributes
System capability: SystemCapability.Multimedia.Image.Core
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
clipRect | Region | Yes | Yes | Image area to be cropped. |
size | Size | Yes | No | Image size. |
format | number | Yes | No | Image format. For details, see PixelMapFormat. |
getComponent9+
getComponent(componentType: ComponentType, callback: AsyncCallback<Component>): void
Obtains the component buffer from the Image instance based on the color component type. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
componentType | ComponentType | Yes | Color component type of the image. |
callback | AsyncCallback<Component> | Yes | Callback used to return the component buffer. |
Example
import {BusinessError} from '@ohos.base'
img.getComponent(4, (err : BusinessError, component : image.Component) => {
if(err) {
console.log('getComponent failed.');
} else {
console.log('getComponent succeeded.');
}
})
getComponent9+
getComponent(componentType: ComponentType): Promise<Component>
Obtains the component buffer from the Image instance based on the color component type. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
componentType | ComponentType | Yes | Color component type of the image. |
Return value
Type | Description |
---|---|
Promise<Component> | Promise used to return the component buffer. |
Example
img.getComponent(4).then((component : image.Component) => { })
release9+
release(callback: AsyncCallback<void>): void
Releases this Image instance. This API uses an asynchronous callback to return the result.
The corresponding resources must be released before another image arrives.
System capability: SystemCapability.Multimedia.Image.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<void> | Yes | Callback used to return the result. |
Example
import {BusinessError} from '@ohos.base'
img.release((err : BusinessError) =>{
if (err != undefined) {
console.log('Failed to release the image source instance.');
} else {
console.log('Succeeded in releasing the image source instance.');
}
})
release9+
release(): Promise<void>
Releases this Image instance. This API uses a promise to return the result.
The corresponding resources must be released before another image arrives.
System capability: SystemCapability.Multimedia.Image.Core
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. |
Example
import {BusinessError} from '@ohos.base'
img.release().then(() =>{
console.log('release succeeded.');
}).catch((error : BusinessError) => {
console.log('release failed.');
})
PositionArea7+
Describes area information in an image.
System capability: SystemCapability.Multimedia.Image.Core
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
pixels | ArrayBuffer | Yes | No | Pixels of the image. |
offset | number | Yes | No | Offset for data reading. |
stride | number | Yes | No | Number of bytes from one row of pixels in memory to the next row of pixels in memory. The value of stride must be greater than or equal to the value of region.size.width multiplied by 4. |
region | Region | Yes | No | Region to read or write. The width of the region to write plus the X coordinate cannot be greater than the width of the original image. The height of the region to write plus the Y coordinate cannot be greater than the height of the original image. |
ImageInfo
Describes image information.
System capability: SystemCapability.Multimedia.Image.Core
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
size | Size | Yes | Yes | Image size. |
density9+ | number | Yes | Yes | Pixel density, in ppi. |
Size
Describes the size of an image.
System capability: SystemCapability.Multimedia.Image.Core
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
height | number | Yes | Yes | Image height. |
width | number | Yes | Yes | Image width. |
PixelMapFormat7+
Enumerates the pixel formats of images.
System capability: SystemCapability.Multimedia.Image.Core
Name | Value | Description |
---|---|---|
UNKNOWN | 0 | Unknown format. |
RGB_565 | 2 | RGB_565. |
RGBA_8888 | 3 | 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+
Enumerates the alpha types of images.
System capability: SystemCapability.Multimedia.Image.Core
Name | Value | Description |
---|---|---|
UNKNOWN | 0 | Unknown alpha type. |
OPAQUE | 1 | There is no alpha or the image is opaque. |
PREMUL | 2 | Premultiplied alpha. |
UNPREMUL | 3 | Unpremultiplied alpha, that is, straight alpha. |
ScaleMode9+
Enumerates the scale modes of images.
System capability: SystemCapability.Multimedia.Image.Core
Name | Value | Description |
---|---|---|
CENTER_CROP | 1 | Scales the image so that it fills the requested bounds of the target and crops the extra. |
FIT_TARGET_SIZE | 0 | Reduces the image size to the dimensions of the target. |
SourceOptions9+
Defines image source initialization options.
System capability: SystemCapability.Multimedia.Image.Core
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
sourceDensity | number | Yes | Yes | Density of the image source. |
sourcePixelFormat | PixelMapFormat | Yes | Yes | Pixel format of the image source. |
sourceSize | Size | Yes | Yes | Pixel size of the image source. |
InitializationOptions8+
Defines pixel map initialization options.
System capability: SystemCapability.Multimedia.Image.Core
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
alphaType9+ | AlphaType | Yes | Yes | Alpha type. |
editable | boolean | Yes | Yes | Whether the image is editable. |
pixelFormat | PixelMapFormat | Yes | Yes | Pixel map format. |
scaleMode9+ | ScaleMode | Yes | Yes | Scale mode. |
size | Size | Yes | Yes | Image size. |
DecodingOptions7+
Defines image decoding options.
System capability: SystemCapability.Multimedia.Image.ImageSource
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
sampleSize | number | Yes | Yes | Thumbnail sampling size. Currently, this option can only be set to 1. |
rotate | number | Yes | Yes | Rotation angle. |
editable | boolean | Yes | Yes | Whether the image is editable. If this option is set to false, the image cannot be edited again, and operations such as cropping will fail. |
desiredSize | Size | Yes | Yes | Expected output size. |
desiredRegion | Region | Yes | Yes | Region to decode. |
desiredPixelFormat | PixelMapFormat | Yes | Yes | Pixel map format for decoding. |
index | number | Yes | Yes | Index of the image to decode. |
fitDensity9+ | number | Yes | Yes | Image pixel density, in ppi. |
Region7+
Describes region information.
System capability: SystemCapability.Multimedia.Image.Core
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
size | Size | Yes | Yes | Region size. |
x | number | Yes | Yes | X coordinate to translate. |
y | number | Yes | Yes | Y coordinate of the region. |
PackingOption
Defines the option for image packing.
System capability: SystemCapability.Multimedia.Image.ImagePacker
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
format | string | Yes | Yes | Format of the packed image. Currently, only JPG, WebP, and PNG are supported. |
quality | number | Yes | Yes | Quality of the output image in JPEG encoding. The value ranges from 1 to 100. |
bufferSize9+ | number | Yes | Yes | Buffer size, which is used to set the image size. The default value is 10 MB. |
GetImagePropertyOptions7+
Describes image properties.
System capability: SystemCapability.Multimedia.Image.ImageSource
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
index | number | Yes | Yes | Index of an image. |
defaultValue | string | Yes | Yes | Default property value. |
PropertyKey7+
Describes the exchangeable image file format (EXIF) data of an image.
System capability: SystemCapability.Multimedia.Image.Core
Name | Value | Description |
---|---|---|
BITS_PER_SAMPLE | “BitsPerSample” | Number of bits per pixel. |
ORIENTATION | “Orientation” | Image orientation. |
IMAGE_LENGTH | “ImageLength” | Image length. |
IMAGE_WIDTH | “ImageWidth” | Image width. |
GPS_LATITUDE | “GPSLatitude” | Image latitude. |
GPS_LONGITUDE | “GPSLongitude” | Image longitude. |
GPS_LATITUDE_REF | “GPSLatitudeRef” | Latitude reference, for example, N or S. |
GPS_LONGITUDE_REF | “GPSLongitudeRef” | Longitude reference, for example, W or E. |
DATE_TIME_ORIGINAL9+ | “DateTimeOriginal” | Shooting time, for example, 2022:09:06 15:48:00. This attribute is read-only. |
EXPOSURE_TIME9+ | “ExposureTime” | Exposure time, for example, 1⁄33 sec. Currently, this attribute is read-only. |
SCENE_TYPE9+ | “SceneType” | Shooting scene type, for example, portrait, scenery, motion, and night. Currently, this attribute is read-only. |
ISO_SPEED_RATINGS9+ | “ISOSpeedRatings” | ISO sensitivity or ISO speed, for example, 400. Currently, this attribute is read-only. |
F_NUMBER9+ | “FNumber” | Aperture, for example, f/1.8. Currently, this attribute is read-only. |
DATE_TIME10+ | “DateTime” | Date and time. Currently, this attribute is read-only. |
GPS_TIME_STAMP10+ | “GPSTimeStamp” | GPS timestamp. Currently, this parameter is read-only. |
GPS_DATE_STAMP10+ | “GPSDateStamp” | GPS date stamp. Currently, this attribute is read-only. |
IMAGE_DESCRIPTION10+ | “ImageDescription” | Image description. Currently, this attribute is read-only. |
MAKE10+ | “Make” | Vendor. Currently, this attribute is read-only. |
MODEL10+ | “Model” | Device model. Currently, this attribute is read-only. |
PHOTO_MODE10+ | “PhotoMode “ | Photo mode. Currently, this attribute is read-only. |
SENSITIVITY_TYPE10+ | “SensitivityType” | Sensitivity type. Currently, this attribute is read-only. |
STANDARD_OUTPUT_SENSITIVITY10+ | “StandardOutputSensitivity” | Standard output sensitivity. Currently, this attribute is read-only. |
RECOMMENDED_EXPOSURE_INDEX10+ | “RecommendedExposureIndex” | Recommended exposure index. Currently, this attribute is read-only. |
ISO_SPEED10+ | “ISOSpeedRatings” | ISO speed. Currently, this attribute is read-only. |
APERTURE_VALUE10+ | “ApertureValue” | Aperture value. Currently, this attribute is read-only. |
EXPOSURE_BIAS_VALUE10+ | “ExposureBiasValue” | Exposure bias value. Currently, this attribute is read-only. |
METERING_MODE10+ | “MeteringMode” | Metering mode. Currently, this attribute is read-only. |
LIGHT_SOURCE10+ | “LightSource” | Light source. Currently, this attribute is read-only. |
FLASH 10+ | “Flash” | Flash status. Currently, this attribute is read-only. |
FOCAL_LENGTH 10+ | “FocalLength” | Focal length. Currently, this attribute is read-only. |
USER_COMMENT 10+ | “UserComment” | User comment. Currently, this attribute is read-only. |
PIXEL_X_DIMENSION 10+ | “PixelXDimension” | Pixel X dimension. Currently, this attribute is read-only. |
PIXEL_Y_DIMENSION10+ | “PixelYDimension” | Pixel Y dimension. Currently, this attribute is read-only. |
WHITE_BALANCE 10+ | “WhiteBalance” | White balance. Currently, this attribute is read-only. |
FOCAL_LENGTH_IN_35_MM_FILM 10+ | “FocalLengthIn35mmFilm” | Focal length in 35mm film. Currently, this attribute is read-only. |
CAPTURE_MODE 10+ | “HwMnoteCaptureMode” | Capture mode. Currently, this parameter is read-only. |
PHYSICAL_APERTURE 10+ | “HwMnotePhysicalAperture” | Physical aperture. Currently, this parameter is read-only. |
ImageFormat9+
Enumerates the image formats.
System capability: SystemCapability.Multimedia.Image.Core
Name | Value | Description |
---|---|---|
YCBCR_422_SP | 1000 | YCBCR422 semi-planar format. |
JPEG | 2000 | JPEG encoding format. |
ComponentType9+
Enumerates the color component types of images.
System capability: SystemCapability.Multimedia.Image.ImageReceiver
Name | Value | Description |
---|---|---|
YUV_Y | 1 | Luminance component. |
YUV_U | 2 | Chrominance component. |
YUV_V | 3 | Chrominance component. |
JPEG | 4 | JPEG type. |
Component9+
Describes the color components of an image.
System capability: SystemCapability.Multimedia.Image.Core
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
componentType | ComponentType | Yes | No | Color component type. |
rowStride | number | Yes | No | Row stride. |
pixelStride | number | Yes | No | Pixel stride. |
byteBuffer | ArrayBuffer | Yes | No | Component buffer. |
Supplementary Information
SVG Tags
The SVG tags are supported since API version 10. The used version is (SVG) 1.1. Currently, the following tags are supported: - a - circla - clipPath - defs - ellipse - feBlend - feColorMatrix - feComposite - feDiffuseLighting - feDisplacementMap - feDistantLight - feFlood - feGaussianBlur - feImage - feMorphology - feOffset - fePointLight - feSpecularLighting - feSpotLight - feTurbulence - filter - g - image - line - linearGradient - mask - path - pattern - polygon - polyline - radialGradient - rect - stop - svg - text - textPath - tspan - use
你可能感兴趣的鸿蒙文章
harmony 鸿蒙System Common Events (To Be Deprecated Soon)
harmony 鸿蒙System Common Events
harmony 鸿蒙API Reference Document Description
harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)
harmony 鸿蒙BundleStatusCallback
harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)
harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)
harmony 鸿蒙@ohos.bundle (Bundle)
harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦