MiniX自绘渲染跨平台框架
  • 框架说明
  • 声明式开发范式
  • 内置Api
指南
接口
  • Minix CLI
示例
  • 类Web框架

    • 框架说明
    • 类Web开发范式
    • 内置Api
  • 指南
  • 组件
  • 接口
  • 示例
  • 规范
  • DophinHybrid

    • 快速上手 (opens new window)
    • UI 组件库 (opens new window)
    • jsBridge 接口 (opens new window)
  • DolphinWeex

    • 快速上手 (opens new window)
    • UI 组件库 (opens new window)
    • jsBridge 接口 (opens new window)
  • 发布消息
  • 常见问题
  • 更新日志
  • 框架说明
  • 声明式开发范式
  • 内置Api
指南
接口
  • Minix CLI
示例
  • 类Web框架

    • 框架说明
    • 类Web开发范式
    • 内置Api
  • 指南
  • 组件
  • 接口
  • 示例
  • 规范
  • DophinHybrid

    • 快速上手 (opens new window)
    • UI 组件库 (opens new window)
    • jsBridge 接口 (opens new window)
  • DolphinWeex

    • 快速上手 (opens new window)
    • UI 组件库 (opens new window)
    • jsBridge 接口 (opens new window)
  • 发布消息
  • 常见问题
  • 更新日志
  • 接口参考

    • UI 界面

    • 图形图像

    • 媒体

      • @ohos.multimedia.image (图片处理)
        • 导入模块
        • image.createPixelMap
        • image.createPixelMap
        • PixelMap
        • image.createImageSource
        • image.createImageSource
        • image.createImageSource
        • image.createImageSource
        • image.createImageSource
        • image.createImageSource
        • image.CreateIncrementalSource
        • image.CreateIncrementalSource
        • ImageSource
        • image.createImagePacker
        • ImagePacker
        • image.createImageReceiver
        • ImageReceiver
        • Image
        • PositionArea
        • ImageInfo
        • Size
        • PixelMapFormat
        • AlphaType
        • ScaleMode
        • SourceOptions
        • InitializationOptions
        • DecodingOptions
        • Region
        • PackingOption
        • GetImagePropertyOptions
        • PropertyKey
        • ImageFormat
        • ComponentType
        • Component
        • ResponseCode
    • 资源管理

    • 语言基础类库

    • 网络管理

    • 数据管理

    • 设备管理

    • 公共事件与通知

# @ohos.multimedia.image (图片处理)

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

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

# 导入模块

import image from "@ohos.multimedia.image";
1

# image.createPixelMap8+

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

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

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

参数:

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

返回值:

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

示例:

const color = new ArrayBuffer(96);
let bufferArr = new Uint8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } };
image
  .createPixelMap(color, opts)
  .then((pixelmap) => {
    console.log("Succeeded in creating pixelmap.");
  })
  .catch((error) => {
    console.log("Failed to create pixelmap.");
  });
1
2
3
4
5
6
7
8
9
10
11

# image.createPixelMap8+

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

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

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

参数:

名称 类型 必填 说明
colors ArrayBuffer 是 BGRA_8888 格式的颜色数组。
options InitializationOptions 是 属性。
callback AsyncCallback<PixelMap> 是 通过回调返回 PixelMap 对象。

示例:

const color = new ArrayBuffer(96);
let bufferArr = new Uint8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } };
image.createPixelMap(color, opts, (error, pixelmap) => {
  if (error) {
    console.log("Failed to create pixelmap.");
  } else {
    console.log("Succeeded in creating pixelmap.");
  }
});
1
2
3
4
5
6
7
8
9
10

# PixelMap7+

图像像素类,用于读取或写入图像数据以及获取图像信息。在调用 PixelMap 的方法前,需要先通过 createPixelMap 创建一个 PixelMap 实例。

# 属性

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

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

# readPixelsToBuffer7+

readPixelsToBuffer(dst: ArrayBuffer): Promise<void>

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

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

参数:

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

返回值:

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

示例:

const readBuffer = new ArrayBuffer(96);
pixelmap
  .readPixelsToBuffer(readBuffer)
  .then(() => {
    console.log("Succeeded in reading image pixel data."); //符合条件则进入
  })
  .catch((error) => {
    console.log("Failed to read image pixel data."); //不符合条件则进入
  });
1
2
3
4
5
6
7
8
9

# readPixelsToBuffer7+

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

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

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

参数:

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

示例:

const readBuffer = new ArrayBuffer(96);
pixelmap.readPixelsToBuffer(readBuffer, (err, res) => {
  if (err) {
    console.log("Failed to read image pixel data."); //不符合条件则进入
  } else {
    console.log("Succeeded in reading image pixel data."); //符合条件则进入
  }
});
1
2
3
4
5
6
7
8

# readPixels7+

readPixels(area: PositionArea): Promise<void>

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

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

参数:

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

返回值:

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

示例:

const area = {
  pixels: new ArrayBuffer(8),
  offset: 0,
  stride: 8,
  region: { size: { height: 1, width: 2 }, x: 0, y: 0 },
};
pixelmap
  .readPixels(area)
  .then(() => {
    console.log("Succeeded in reading the image data in the area."); //符合条件则进入
  })
  .catch((error) => {
    console.log("Failed to read the image data in the area."); //不符合条件则进入
  });
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# readPixels7+

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

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

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

参数:

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

示例:

const color = new ArrayBuffer(96);
let bufferArr = new Uint8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } };
image.createPixelMap(color, opts, (err, pixelmap) => {
  if (pixelmap == undefined) {
    console.info("createPixelMap failed.");
  } else {
    const area = {
      pixels: new ArrayBuffer(8),
      offset: 0,
      stride: 8,
      region: { size: { height: 1, width: 2 }, x: 0, y: 0 },
    };
    pixelmap.readPixels(area, () => {
      console.info("readPixels success");
    });
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# writePixels7+

writePixels(area: PositionArea): Promise<void>

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

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

参数:

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

返回值:

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

示例:

const color = new ArrayBuffer(96);
let bufferArr = new Uint8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } };
image
  .createPixelMap(color, opts)
  .then((pixelmap) => {
    if (pixelmap == undefined) {
      console.info("createPixelMap failed.");
    }
    const area = {
      pixels: new ArrayBuffer(8),
      offset: 0,
      stride: 8,
      region: { size: { height: 1, width: 2 }, x: 0, y: 0 },
    };
    let bufferArr = new Uint8Array(area.pixels);
    for (var i = 0; i < bufferArr.length; i++) {
      bufferArr[i] = i + 1;
    }

    pixelmap.writePixels(area).then(() => {
      console.info("Succeeded to write pixelmap into the specified area.");
    });
  })
  .catch((error) => {
    console.log("error: " + error);
  });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# writePixels7+

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

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

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

参数:

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

示例:

const area = {
  pixels: new ArrayBuffer(8),
  offset: 0,
  stride: 8,
  region: { size: { height: 1, width: 2 }, x: 0, y: 0 },
};
let bufferArr = new Uint8Array(area.pixels);
for (var i = 0; i < bufferArr.length; i++) {
  bufferArr[i] = i + 1;
}
pixelmap.writePixels(area, (error) => {
  if (error != undefined) {
    console.info("Failed to write pixelmap into the specified area.");
  } else {
    console.info("Succeeded to write pixelmap into the specified area.");
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# writeBufferToPixels7+

writeBufferToPixels(src: ArrayBuffer): Promise<void>

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

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

参数:

参数名 类型 必填 说明
src ArrayBuffer 是 图像像素数据。

返回值:

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

示例:

const color = new ArrayBuffer(96);
let bufferArr = new Uint8Array(color);
for (var 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((err) => {
    console.error("Failed to write data from a buffer to a PixelMap.");
  });
1
2
3
4
5
6
7
8
9
10
11
12
13

# writeBufferToPixels7+

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

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

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

参数:

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

示例:

const color = new ArrayBuffer(96);
let bufferArr = new Uint8Array(color);
for (var i = 0; i < bufferArr.length; i++) {
  bufferArr[i] = i + 1;
}
pixelmap.writeBufferToPixels(color, function (err) {
  if (err) {
    console.error("Failed to write data from a buffer to a PixelMap.");
    return;
  } else {
    console.log("Succeeded in writing data from a buffer to a PixelMap.");
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13

# getImageInfo7+

getImageInfo(): Promise<ImageInfo>

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

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

返回值:

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

示例:

const color = new ArrayBuffer(96);
let opts = { editable: true, pixelFormat: 2, size: { height: 6, width: 8 } };
image.createPixelMap(color, opts).then((pixelmap) => {
  if (pixelmap == undefined) {
    console.error("Failed to obtain the image pixel map information.");
  }
  pixelmap.getImageInfo().then((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.");
    }
  });
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# getImageInfo7+

getImageInfo(callback: AsyncCallback<ImageInfo>): void

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

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

参数:

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

示例:

const color = new ArrayBuffer(96);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } };
image.createPixelMap(color, opts, (err, pixelmap) => {
  if (pixelmap == undefined) {
    console.error("Failed to obtain the image pixel map information.");
  }
  pixelmap.getImageInfo((err, 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.");
    }
  });
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# getBytesNumberPerRow7+

getBytesNumberPerRow(): number

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

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

返回值:

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

示例:

const color = new ArrayBuffer(96);
let bufferArr = new Uint8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } };
image.createPixelMap(color, opts, (err, pixelmap) => {
  let rowCount = pixelmap.getBytesNumberPerRow();
});
1
2
3
4
5
6

# getPixelBytesNumber7+

getPixelBytesNumber(): number

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

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

返回值:

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

示例:

let pixelBytesNumber = pixelmap.getPixelBytesNumber();
1

# getDensity9+

getDensity():number

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

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

返回值:

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

示例:

let getDensity = pixelmap.getDensity();
1

# opacity9+

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

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

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

参数:

参数名 类型 必填 说明
rate number 是 透明比率的值,取值范围:0-1。
callback AsyncCallback<void> 是 获取回调,失败时返回错误信息。

示例:

var rate = 0.5;
pixelmap.opacity(rate, (err) => {
  if (err) {
    console.error("Failed to set opacity.");
    return;
  } else {
    console.log("Succeeded in setting opacity.");
  }
});
1
2
3
4
5
6
7
8
9

# opacity9+

opacity(rate: number): Promise<void>

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

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

参数:

参数名 类型 必填 说明
rate number 是 透明比率的值,取值范围:0-1。

返回值:

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

示例:

async function Demo() {
  await pixelmap.opacity(0.5);
}
1
2
3

# createAlphaPixelmap9+

createAlphaPixelmap(): Promise<PixelMap>

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

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

返回值:

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

示例:

async function Demo() {
  await pixelmap.createAlphaPixelmap();
}
1
2
3

# createAlphaPixelmap9+

createAlphaPixelmap(callback: AsyncCallback<PixelMap>): void

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

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

参数:

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

示例:

pixelmap.createAlphaPixelmap((err, alphaPixelMap) => {
  if (alphaPixelMap == undefined) {
    console.info("Failed to obtain new pixel map.");
  } else {
    console.info("Succeed in obtaining new pixel map.");
  }
});
1
2
3
4
5
6
7

# scale9+

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

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

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

参数:

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

示例:

async function Demo() {
  await pixelmap.scale(2.0, 1.0);
}
1
2
3

# scale9+

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

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

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

参数:

参数名 类型 必填 说明
x number 是 宽度的缩放值,其值为输入的倍数。
y number 是 高度的缩放值,其值为输入的倍数。

返回值:

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

示例:

async function Demo() {
  await pixelmap.scale(2.0, 1.0);
}
1
2
3

# translate9+

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

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

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

参数:

参数名 类型 必填 说明
x number 是 区域横坐标。
y number 是 区域纵坐标。
callback AsyncCallback<void> 是 获取回调,失败时返回错误信息。

示例:

async function Demo() {
  await pixelmap.translate(3.0, 1.0);
}
1
2
3

# translate9+

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

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

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

参数:

参数名 类型 必填 说明
x number 是 区域横坐标。
y number 是 区域纵坐标。

返回值:

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

示例:

async function Demo() {
  await pixelmap.translate(3.0, 1.0);
}
1
2
3

# rotate9+

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

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

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

参数:

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

示例:

async function Demo() {
  await pixelmap.rotate(90.0);
}
1
2
3

# rotate9+

rotate(angle: number): Promise<void>

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

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

参数:

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

返回值:

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

示例:

async function Demo() {
  await pixelmap.rotate(90.0);
}
1
2
3

# flip9+

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

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

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

参数:

参数名 类型 必填 说明
horizontal boolean 是 水平翻转。
vertical boolean 是 垂直翻转。
callback AsyncCallback<void> 是 获取回调,失败时返回错误信息。

示例:

async function Demo() {
  await pixelmap.flip(false, true);
}
1
2
3

# flip9+

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

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

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

参数:

参数名 类型 必填 说明
horizontal boolean 是 水平翻转。
vertical boolean 是 垂直翻转。

返回值:

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

示例:

async function Demo() {
  await pixelmap.flip(false, true);
}
1
2
3

# crop9+

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

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

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

参数:

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

示例:

async function Demo() {
  await pixelmap.crop({ x: 0, y: 0, size: { height: 100, width: 100 } });
}
1
2
3

# crop9+

crop(region: Region): Promise<void>

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

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

参数:

参数名 类型 必填 说明
region Region 是 裁剪的尺寸。

返回值:

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

示例:

async function Demo() {
  await pixelmap.crop({ x: 0, y: 0, size: { height: 100, width: 100 } });
}
1
2
3

# release7+

release():Promise<void>

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

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

返回值:

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

示例:

pixelmap
  .release()
  .then(() => {
    console.log("Succeeded in releasing pixelmap object.");
  })
  .catch((error) => {
    console.log("Failed to release pixelmap object.");
  });
1
2
3
4
5
6
7
8

# release7+

release(callback: AsyncCallback<void>): void

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

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

参数:

名称 类型 必填 说明
callback AsyncCallback<void> 是 异步返回释放结果。

示例:

pixelmap.release(() => {
  console.log("Succeeded in releasing pixelmap object.");
});
1
2
3

# image.createImageSource

createImageSource(uri: string): ImageSource

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

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

参数:

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

返回值:

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

示例:

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

# image.createImageSource9+

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

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

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

参数:

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

返回值:

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

示例:

var sourceOptions = { sourceDensity: 120 };
let imageSource = image.createImageSource("test.png", sourceOptions);
1
2

# image.createImageSource7+

createImageSource(fd: number): ImageSource

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

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

参数:

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

返回值:

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

示例:

const imageSourceApi = image.createImageSource(0);
1

# image.createImageSource9+

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

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

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

参数:

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

返回值:

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

示例:

var sourceOptions = { sourceDensity: 120 };
const imageSourceApi = image.createImageSource(0, sourceOptions);
1
2

# image.createImageSource9+

createImageSource(buf: ArrayBuffer): ImageSource

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

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

参数:

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

示例:

const buf = new ArrayBuffer(96);
const imageSourceApi = image.createImageSource(buf);
1
2

# image.createImageSource9+

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

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

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

参数:

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

返回值:

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

示例:

const data = new ArrayBuffer(112);
const imageSourceApi = image.createImageSource(data);
1
2

# image.CreateIncrementalSource9+

CreateIncrementalSource(buf: ArrayBuffer): ImageSource

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

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

参数:

参数名 类型 必填 说明
buf ArrayBuffer 是 增量数据。

返回值:

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

示例:

const buf = new ArrayBuffer(96);
const imageSourceIncrementalSApi = image.CreateIncrementalSource(buf);
1
2

# image.CreateIncrementalSource9+

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

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

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

参数:

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

返回值:

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

示例:

const buf = new ArrayBuffer(96);
const imageSourceIncrementalSApi = image.CreateIncrementalSource(buf);
1
2

# ImageSource

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

# 属性

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

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

# getImageInfo

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

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

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

参数:

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

示例:

imageSourceApi.getImageInfo(0, (error, imageInfo) => {
  if (error) {
    console.log("getImageInfo failed.");
  } else {
    console.log("getImageInfo succeeded.");
  }
});
1
2
3
4
5
6
7

# getImageInfo

getImageInfo(callback: AsyncCallback<ImageInfo>): void

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

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

参数:

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

示例:

imageSourceApi.getImageInfo((imageInfo) => {
  console.log("Succeeded in obtaining the image information.");
});
1
2
3

# getImageInfo

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

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

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

参数:

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

返回值:

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

示例:

imageSourceApi
  .getImageInfo(0)
  .then((imageInfo) => {
    console.log("Succeeded in obtaining the image information.");
  })
  .catch((error) => {
    console.log("Failed to obtain the image information.");
  });
1
2
3
4
5
6
7
8

# getImageProperty7+

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

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

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

参数:

名称 类型 必填 说明
key string 是 图片属性名。
options GetImagePropertyOptions 否 图片属性,包括图片序号与默认属性值。

返回值:

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

示例:

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

# getImageProperty7+

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

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

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

参数:

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

示例:

imageSourceApi.getImageProperty("BitsPerSample", (error, data) => {
  if (error) {
    console.log(
      "Failed to get the value of the specified attribute key of the image."
    );
  } else {
    console.log(
      "Succeeded in getting the value of the specified attribute key of the image."
    );
  }
});
1
2
3
4
5
6
7
8
9
10
11

# getImageProperty7+

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

获取图片指定属性键的值,callback 形式返回结果。

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

参数:

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

示例:

let property = { index: 0, defaultValue: "9999" };
imageSourceApi.getImageProperty("BitsPerSample", property, (error, data) => {
  if (error) {
    console.log(
      "Failed to get the value of the specified attribute key of the image."
    );
  } else {
    console.log(
      "Succeeded in getting the value of the specified attribute key of the image."
    );
  }
});
1
2
3
4
5
6
7
8
9
10
11
12

# modifyImageProperty9+

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

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

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

参数:

参数名 类型 必填 说明
key string 是 图片属性名。
value string 是 属性值。

返回值:

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

示例:

imageSourceApi.modifyImageProperty("ImageWidth", "120").then(() => {
  const w = imageSourceApi.getImageProperty("ImageWidth");
  console.info("w", w);
});
1
2
3
4

# modifyImageProperty9+

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

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

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

参数:

参数名 类型 必填 说明
key string 是 图片属性名。
value string 是 属性值。
callback AsyncCallback<void> 是 修改属性值,callback 返回结果。

示例:

imageSourceApi.modifyImageProperty("ImageWidth", "120", () => {});
1

# updateData9+

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

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

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

参数:

名称 类型 必填 说明
buf ArrayBuffer 是 增量数据。
isFinished boolean 是 是否更新完。
value number 否 偏移量。
length number 否 数组长。

返回值:

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

示例:

const array = new ArrayBuffer(100);
imageSourceApi.updateData(array, false, 0, 10).then((data) => {
  console.info("Succeeded in updating data.");
});
1
2
3
4

# updateData9+

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

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

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

参数:

名称 类型 必填 说明
buf ArrayBuffer 是 增量数据。
isFinished boolean 是 是否更新完。
value number 否 偏移量。
length number 否 数组长。
callback AsyncCallback<void> 是 回调表示成功或失败。

示例:

const array = new ArrayBuffer(100);
imageSourceApi.updateData(array, false, 0, 10, (error, data) => {
  if (data !== undefined) {
    console.info("Succeeded in updating data.");
  }
});
1
2
3
4
5
6

# createPixelMap7+

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

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

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

参数:

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

返回值:

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

示例:

imageSourceApi
  .createPixelMap()
  .then((pixelmap) => {
    console.log(
      "Succeeded in creating pixelmap object through image decoding parameters."
    );
  })
  .catch((error) => {
    console.log(
      "Failed to create pixelmap object through image decoding parameters."
    );
  });
1
2
3
4
5
6
7
8
9
10
11
12

# createPixelMap7+

createPixelMap(callback: AsyncCallback<PixelMap>): void

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

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

参数:

名称 类型 必填 说明
callback AsyncCallback<PixelMap> 是 通过回调返回 PixelMap 对象。

示例:

imageSourceApi.createPixelMap((err, pixelmap) => {
  console.info("Succeeded in creating pixelmap object.");
});
1
2
3

# createPixelMap7+

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

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

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

参数:

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

示例:

let 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, (pixelmap) => {
  console.log("Succeeded in creating pixelmap object.");
});
1
2
3
4
5
6
7
8
9
10
11
12

# release

release(callback: AsyncCallback<void>): void

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

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

参数:

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

示例:

imageSourceApi.release(() => {
  console.log("release succeeded.");
});
1
2
3

# release

release(): Promise<void>

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

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

返回值:

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

示例:

imageSourceApi
  .release()
  .then(() => {
    console.log("Succeeded in releasing the image source instance.");
  })
  .catch((error) => {
    console.log("Failed to release the image source instance.");
  });
1
2
3
4
5
6
7
8

# image.createImagePacker

createImagePacker(): ImagePacker

创建 ImagePacker 实例。

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

返回值:

类型 说明
ImagePacker 返回 ImagePacker 实例。

示例:

const imagePackerApi = image.createImagePacker();
1

# ImagePacker

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

# 属性

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

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

# packing

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

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

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

参数:

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

示例:

const imageSourceApi = image.createImageSource(0);
let packOpts = { format: "image/jpeg", quality: 98 };
imagePackerApi.packing(imageSourceApi, packOpts, (data) => {});
1
2
3

# packing

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

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

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

参数:

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

返回值:

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

示例:

const imageSourceApi = image.createImageSource(0);
let packOpts = { format: "image/jpeg", quality: 98 };
imagePackerApi
  .packing(imageSourceApi, packOpts)
  .then((data) => {
    console.log("packing succeeded.");
  })
  .catch((error) => {
    console.log("packing failed.");
  });
1
2
3
4
5
6
7
8
9
10

# packing8+

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

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

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

参数:

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

示例:

const color = new ArrayBuffer(96);
let bufferArr = new Uint8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } };
image.createPixelMap(color, opts).then((pixelmap) => {
  let packOpts = { format: "image/jpeg", quality: 98 };
  imagePackerApi.packing(pixelmap, packOpts, (data) => {
    console.log("Succeeded in packing the image.");
  });
});
1
2
3
4
5
6
7
8
9

# packing8+

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

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

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

参数:

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

返回值:

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

示例:

const color = new ArrayBuffer(96);
let bufferArr = new Uint8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } };
image.createPixelMap(color, opts).then((pixelmap) => {
  let packOpts = { format: "image/jpeg", quality: 98 };
  imagePackerApi
    .packing(pixelmap, packOpts)
    .then((data) => {
      console.log("Succeeded in packing the image.");
    })
    .catch((error) => {
      console.log("Failed to pack the image..");
    });
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# release

release(callback: AsyncCallback<void>): void

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

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

参数:

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

示例:

imagePackerApi.release(() => {
  console.log("Succeeded in releasing image packaging.");
});
1
2
3

# release

release(): Promise<void>

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

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

返回值:

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

示例:

imagePackerApi
  .release()
  .then(() => {
    console.log("Succeeded in releasing image packaging.");
  })
  .catch((error) => {
    console.log("Failed to release image packaging.");
  });
1
2
3
4
5
6
7
8

# image.createImageReceiver9+

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

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

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

参数:

名称 类型 必填 说明
width number 是 图像的默认宽度。
height number 是 图像的默认高度。
format number 是 图像格式。
capacity number 是 同时访问的最大图像数。

返回值:

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

示例:

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

# ImageReceiver9+

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

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

# 属性

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

名称 类型 可读 可写 说明
size Size 是 否 图片大小。
capacity number 是 否 同时访问的图像数。
format ImageFormat 是 否 图像格式。

# getReceivingSurfaceId9+

getReceivingSurfaceId(callback: AsyncCallback<string>): void

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

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

参数:

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

示例:

receiver.getReceivingSurfaceId((err, id) => {
  if (err) {
    console.log("getReceivingSurfaceId failed.");
  } else {
    console.log("getReceivingSurfaceId succeeded.");
  }
});
1
2
3
4
5
6
7

# getReceivingSurfaceId9+

getReceivingSurfaceId(): Promise<string>

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

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

返回值:

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

示例:

receiver
  .getReceivingSurfaceId()
  .then((id) => {
    console.log("getReceivingSurfaceId succeeded.");
  })
  .catch((error) => {
    console.log("getReceivingSurfaceId failed.");
  });
1
2
3
4
5
6
7
8

# readLatestImage9+

readLatestImage(callback: AsyncCallback<Image>): void

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

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

参数:

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

示例:

receiver.readLatestImage((err, img) => {
  if (err) {
    console.log("readLatestImage failed.");
  } else {
    console.log("readLatestImage succeeded.");
  }
});
1
2
3
4
5
6
7

# readLatestImage9+

readLatestImage(): Promise<Image>

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

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

返回值:

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

示例:

receiver
  .readLatestImage()
  .then((img) => {
    console.log("readLatestImage succeeded.");
  })
  .catch((error) => {
    console.log("readLatestImage failed.");
  });
1
2
3
4
5
6
7
8

# readNextImage9+

readNextImage(callback: AsyncCallback<Image>): void

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

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

参数:

名称 类型 必填 说明
callback AsyncCallback<Image> 是 回调函数,返回下一张图片。

示例:

receiver.readNextImage((err, img) => {
  if (err) {
    console.log("readNextImage failed.");
  } else {
    console.log("readNextImage succeeded.");
  }
});
1
2
3
4
5
6
7

# readNextImage9+

readNextImage(): Promise<Image>

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

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

返回值:

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

示例:

receiver
  .readNextImage()
  .then((img) => {
    console.log("readNextImage succeeded.");
  })
  .catch((error) => {
    console.log("readNextImage failed.");
  });
1
2
3
4
5
6
7
8

# on('imageArrival')9+

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

接收图片时注册回调。

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

参数:

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

示例:

receiver.on("imageArrival", () => {});
1

# release9+

release(callback: AsyncCallback<void>): void

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

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

参数:

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

示例:

receiver.release(() => {});
1

# release9+

release(): Promise<void>

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

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

返回值:

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

示例:

receiver
  .release()
  .then(() => {
    console.log("release succeeded.");
  })
  .catch((error) => {
    console.log("release failed.");
  });
1
2
3
4
5
6
7
8

# Image9+

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

# 属性

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

名称 类型 可读 可写 说明
clipRect Region 是 是 要裁剪的图像区域。
size Size 是 否 图像大小。
format number 是 否 图像格式,参考PixelMapFormat。

# getComponent9+

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

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

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

参数:

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

示例:

img.getComponent(4, (err, component) => {
  if (err) {
    console.log("getComponent failed.");
  } else {
    console.log("getComponent succeeded.");
  }
});
1
2
3
4
5
6
7

# getComponent9+

getComponent(componentType: ComponentType): Promise<Component>

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

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

参数:

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

返回值:

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

示例:

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

# release9+

release(callback: AsyncCallback<void>): void

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

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

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

参数:

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

示例:

img.release(() => {
  console.log("release succeeded.");
});
1
2
3

# release9+

release(): Promise<void>

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

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

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

返回值:

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

示例:

img
  .release()
  .then(() => {
    console.log("release succeeded.");
  })
  .catch((error) => {
    console.log("release failed.");
  });
1
2
3
4
5
6
7
8

# PositionArea7+

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

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

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

# ImageInfo

表示图片信息。

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

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

# Size

表示图片尺寸。

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

名称 类型 可读 可写 说明
height number 是 是 输出图片的高。
width number 是 是 输出图片的宽。

# PixelMapFormat7+

枚举,图片像素格式。

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

名称 默认值 描述
UNKNOWN 0 未知格式。
RGB_565 2 格式为 RGB_565
RGBA_8888 3 格式为 RGBA_8888
BGRA_88889+ 4 格式为 BGRA_8888

# AlphaType9+

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

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

名称 默认值 描述
UNKNOWN 0 未知透明度。
OPAQUE 1 没有 alpha 或图片全透明。
PREMUL 2 RGB 前乘 alpha
UNPREMUL 3 RGB 不前乘 alpha

# ScaleMode9+

枚举,图像的缩放模式。

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

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

# SourceOptions9+

ImageSource 的初始化选项。

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

名称 类型 可读 可写 说明
sourceDensity number 是 是 ImageSource 的密度。
sourcePixelFormat PixelMapFormat 是 是 图片像素格式。
sourceSize Size 是 是 图像像素大小。

# InitializationOptions8+

PixelMap 的初始化选项。

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

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

# DecodingOptions7+

图像解码设置选项。

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

名称 类型 可读 可写 说明
sampleSize number 是 是 缩略图采样大小。
rotate number 是 是 旋转角度。
editable boolean 是 是 是否可编辑。
desiredSize Size 是 是 期望输出大小。
desiredRegion Region 是 是 解码区域。
desiredPixelFormat PixelMapFormat 是 是 解码的像素格式。
index number 是 是 解码图片序号。

# Region7+

表示区域信息。

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

名称 类型 可读 可写 说明
size Size 是 是 区域大小。
x number 是 是 区域横坐标。
y number 是 是 区域纵坐标。

# PackingOption

表示图片打包选项。

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

名称 类型 可读 可写 说明
format string 是 是 目标格式。
当前支持格式有:jpeg webp
quality number 是 是 JPEG 编码中设定输出图片质量的参数,取值范围为 1-100

# GetImagePropertyOptions7+

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

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

名称 类型 可读 可写 说明
index number 是 是 图片序号。
defaultValue string 是 是 默认属性值。

# PropertyKey7+

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

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

名称 默认值 说明
BITS_PER_SAMPLE "BitsPerSample" 每个像素比特数。
ORIENTATION "Orientation" 图片方向。
IMAGE_LENGTH "ImageLength" 图片长度。
IMAGE_WIDTH "ImageWidth" 图片宽度。
GPS_LATITUDE "GPSLatitude" 图片纬度。
GPS_LONGITUDE "GPSLongitude" 图片经度。
GPS_LATITUDE_REF "GPSLatitudeRef" 纬度引用,例如 N 或 S。
GPS_LONGITUDE_REF "GPSLongitudeRef" 经度引用,例如 W 或 E。

# ImageFormat9+

枚举,图片格式。

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

名称 默认值 描述
YCBCR_422_SP 1000 YCBCR422 半平面格式。
JPEG 2000 JPEG 编码格式。

# ComponentType9+

枚举,图像的组件类型。

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

名称 默认值 描述
YUV_Y 1 亮度信息。
YUV_U 2 色度信息。
YUV_V 3 色度信息。
JPEG 4 JPEG 类型。

# Component9+

描述图像颜色分量。

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

名称 类型 可读 可写 说明
componentType ComponentType 是 否 组件类型。
rowStride number 是 否 行距。
pixelStride number 是 否 像素间距。
byteBuffer ArrayBuffer 是 否 组件缓冲区。

# ResponseCode

编译错误返回的响应码。

名称 值 说明
ERR_MEDIA_INVALID_VALUE -1 无效大小。
SUCCESS 0 操作成功。
ERROR 62980096 操作失败。
ERR_IPC 62980097 ipc 错误。
ERR_SHAMEM_NOT_EXIST 62980098 共享内存错误。
ERR_SHAMEM_DATA_ABNORMAL 62980099 共享内存错误。
ERR_IMAGE_DECODE_ABNORMAL 62980100 图像解码错误。
ERR_IMAGE_DATA_ABNORMAL 62980101 图像输入数据错误。
ERR_IMAGE_MALLOC_ABNORMAL 62980102 图像 malloc 错误。
ERR_IMAGE_DATA_UNSUPPORT 62980103 不支持图像类型。
ERR_IMAGE_INIT_ABNORMAL 62980104 图像初始化错误。
ERR_IMAGE_GET_DATA_ABNORMAL 62980105 图像获取数据错误。
ERR_IMAGE_TOO_LARGE 62980106 图像数据太大。
ERR_IMAGE_TRANSFORM 62980107 图像转换错误。
ERR_IMAGE_COLOR_CONVERT 62980108 图像颜色转换错误。
ERR_IMAGE_CROP 62980109 裁剪错误。
ERR_IMAGE_SOURCE_DATA 62980110 图像源数据错误。
ERR_IMAGE_SOURCE_DATA_INCOMPLETE 62980111 图像源数据不完整。
ERR_IMAGE_MISMATCHED_FORMAT 62980112 图像格式不匹配。
ERR_IMAGE_UNKNOWN_FORMAT 62980113 图像未知格式。
ERR_IMAGE_SOURCE_UNRESOLVED 62980114 图像源未解析。
ERR_IMAGE_INVALID_PARAMETER 62980115 图像无效参数。
ERR_IMAGE_DECODE_FAILED 62980116 解码失败。
ERR_IMAGE_PLUGIN_REGISTER_FAILED 62980117 注册插件失败。
ERR_IMAGE_PLUGIN_CREATE_FAILED 62980118 创建插件失败。
ERR_IMAGE_ENCODE_FAILED 62980119 图像编码失败。
ERR_IMAGE_ADD_PIXEL_MAP_FAILED 62980120 图像添加像素映射失败。
ERR_IMAGE_HW_DECODE_UNSUPPORT 62980121 不支持图像硬件解码。
ERR_IMAGE_DECODE_HEAD_ABNORMAL 62980122 图像解码头错误。
ERR_IMAGE_DECODE_EXIF_UNSUPPORT 62980123 图像解码 exif 取消支持。
ERR_IMAGE_PROPERTY_NOT_EXIST 62980124 图像属性不存在;错误代码被媒体占用,图像从 150 开始。
ERR_IMAGE_READ_PIXELMAP_FAILED 62980246 读取像素地图失败。
ERR_IMAGE_WRITE_PIXELMAP_FAILED 62980247 写入像素映射失败。
ERR_IMAGE_PIXELMAP_NOT_ALLOW_MODIFY 62980248 pixelmap 不允许修改。
ERR_IMAGE_CONFIG_FAILED 62980259 配置错误。