圖片處理

2024-01-23 15:47 更新

本模塊提供圖片處理效果,包括通過屬性創(chuàng)建PixelMap、讀取圖像像素數(shù)據(jù)、讀取區(qū)域內(nèi)的圖片數(shù)據(jù)等。

說明

本模塊首批接口從API version 6開始支持。后續(xù)版本的新增接口,采用上角標單獨標記接口的起始版本。

導入模塊

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

image.createPixelMap8+

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

通過屬性創(chuàng)建PixelMap,默認采用BGRA_8888格式處理數(shù)據(jù),通過Promise返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

colors

ArrayBuffer

BGRA_8888格式的顏色數(shù)組。

options

InitializationOptions

創(chuàng)建像素的屬性,包括透明度,尺寸,縮略值,像素格式和是否可編輯。

返回值:

類型

說明

Promise<PixelMap>

返回Pixelmap。

當創(chuàng)建的pixelmap大小超過原圖大小時,返回原圖pixelmap大小。

示例:

  1. const color = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. let bufferArr = new Uint8Array(color);
  3. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
  4. image.createPixelMap(color, opts).then((pixelmap) => {
  5. console.log('Succeeded in creating pixelmap.');
  6. }).catch(error => {
  7. console.log('Failed to create pixelmap.');
  8. })

image.createPixelMap8+

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

通過屬性創(chuàng)建PixelMap,默認采用BGRA_8888格式處理數(shù)據(jù),通過回調(diào)函數(shù)返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

colors

ArrayBuffer

BGRA_8888格式的顏色數(shù)組。

options

InitializationOptions

屬性。

callback

AsyncCallback<PixelMap>

通過回調(diào)返回PixelMap對象。

示例:

  1. const color = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. let bufferArr = new Uint8Array(color);
  3. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
  4. image.createPixelMap(color, opts, (error, pixelmap) => {
  5. if(error) {
  6. console.log('Failed to create pixelmap.');
  7. } else {
  8. console.log('Succeeded in creating pixelmap.');
  9. }
  10. })

PixelMap7+

圖像像素類,用于讀取或?qū)懭雸D像數(shù)據(jù)以及獲取圖像信息。在調(diào)用PixelMap的方法前,需要先通過createPixelMap創(chuàng)建一個PixelMap實例。目前pixelmap序列化大小最大128MB,超過會送顯失敗。大小計算方式為(寬*高*每像素占用字節(jié)數(shù))。

屬性

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

類型

可讀

可寫

說明

isEditable

boolean

設定是否圖像像素可被編輯。

readPixelsToBuffer7+

readPixelsToBuffer(dst: ArrayBuffer): Promise<void>

讀取圖像像素數(shù)據(jù),結(jié)果寫入ArrayBuffer里,使用Promise形式返回。指定BGRA_8888格式創(chuàng)建pixelmap,讀取的像素數(shù)據(jù)與原數(shù)據(jù)保持一致。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

dst

ArrayBuffer

緩沖區(qū),函數(shù)執(zhí)行結(jié)束后獲取的圖像像素數(shù)據(jù)寫入到該內(nèi)存區(qū)域內(nèi)。緩沖區(qū)大小由getPixelBytesNumber接口獲取。

返回值:

類型

說明

Promise<void>

Promise實例,用于獲取結(jié)果,失敗時返回錯誤信息。

示例:

  1. const readBuffer = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. pixelmap.readPixelsToBuffer(readBuffer).then(() => {
  3. console.log('Succeeded in reading image pixel data.'); //符合條件則進入
  4. }).catch(error => {
  5. console.log('Failed to read image pixel data.'); //不符合條件則進入
  6. })

readPixelsToBuffer7+

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

讀取圖像像素數(shù)據(jù),結(jié)果寫入ArrayBuffer里,使用callback形式返回。指定BGRA_8888格式創(chuàng)建pixelmap,讀取的像素數(shù)據(jù)與原數(shù)據(jù)保持一致。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

dst

ArrayBuffer

緩沖區(qū),函數(shù)執(zhí)行結(jié)束后獲取的圖像像素數(shù)據(jù)寫入到該內(nèi)存區(qū)域內(nèi)。緩沖區(qū)大小由getPixelBytesNumber接口獲取。

callback

AsyncCallback<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

  1. const readBuffer = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. pixelmap.readPixelsToBuffer(readBuffer, (err, res) => {
  3. if(err) {
  4. console.log('Failed to read image pixel data.'); //不符合條件則進入
  5. } else {
  6. console.log('Succeeded in reading image pixel data.'); //符合條件則進入
  7. }
  8. })

readPixels7+

readPixels(area: PositionArea): Promise<void>

讀取區(qū)域內(nèi)的圖片數(shù)據(jù),使用Promise形式返回讀取結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

area

PositionArea

區(qū)域大小,根據(jù)區(qū)域讀取。

返回值:

類型

說明

Promise<void>

Promise實例,用于獲取讀取結(jié)果,失敗時返回錯誤信息。

示例:

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

readPixels7+

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

讀取區(qū)域內(nèi)的圖片數(shù)據(jù),使用callback形式返回讀取結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

area

PositionArea

區(qū)域大小,根據(jù)區(qū)域讀取。

callback

AsyncCallback<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

  1. const color = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. let bufferArr = new Uint8Array(color);
  3. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
  4. image.createPixelMap(color, opts, (err, pixelmap) => {
  5. if(pixelmap == undefined){
  6. console.info('createPixelMap failed.');
  7. } else {
  8. const area = { pixels: new ArrayBuffer(8),
  9. offset: 0,
  10. stride: 8,
  11. region: { size: { height: 1, width: 2 }, x: 0, y: 0 }};
  12. pixelmap.readPixels(area, () => {
  13. console.info('readPixels success');
  14. })
  15. }
  16. })

writePixels7+

writePixels(area: PositionArea): Promise<void>

將PixelMap寫入指定區(qū)域內(nèi),使用Promise形式返回寫入結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

area

PositionArea

區(qū)域,根據(jù)區(qū)域?qū)懭搿?/p>

返回值:

類型

說明

Promise<void>

Promise實例,用于獲取寫入結(jié)果,失敗時返回錯誤信息。

示例:

  1. const color = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. let bufferArr = new Uint8Array(color);
  3. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
  4. image.createPixelMap(color, opts)
  5. .then( pixelmap => {
  6. if (pixelmap == undefined) {
  7. console.info('createPixelMap failed.');
  8. }
  9. const area = { pixels: new ArrayBuffer(8),
  10. offset: 0,
  11. stride: 8,
  12. region: { size: { height: 1, width: 2 }, x: 0, y: 0 }
  13. }
  14. let bufferArr = new Uint8Array(area.pixels);
  15. for (var i = 0; i < bufferArr.length; i++) {
  16. bufferArr[i] = i + 1;
  17. }
  18. pixelmap.writePixels(area).then(() => {
  19. console.info('Succeeded to write pixelmap into the specified area.');
  20. })
  21. }).catch(error => {
  22. console.log('error: ' + error);
  23. })

writePixels7+

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

將PixelMap寫入指定區(qū)域內(nèi),使用callback形式返回寫入結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

area

PositionArea

區(qū)域,根據(jù)區(qū)域?qū)懭搿?/p>

callback

AsyncCallback<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

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

writeBufferToPixels7+

writeBufferToPixels(src: ArrayBuffer): Promise<void>

讀取緩沖區(qū)中的圖片數(shù)據(jù),結(jié)果寫入PixelMap中,使用Promise形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

src

ArrayBuffer

圖像像素數(shù)據(jù)。

返回值:

類型

說明

Promise<void>

Promise實例,用于獲取結(jié)果,失敗時返回錯誤信息。

示例:

  1. const color = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. let bufferArr = new Uint8Array(color);
  3. for (var i = 0; i < bufferArr.length; i++) {
  4. bufferArr[i] = i + 1;
  5. }
  6. pixelmap.writeBufferToPixels(color).then(() => {
  7. console.log("Succeeded in writing data from a buffer to a PixelMap.");
  8. }).catch((err) => {
  9. console.error("Failed to write data from a buffer to a PixelMap.");
  10. })

writeBufferToPixels7+

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

讀取緩沖區(qū)中的圖片數(shù)據(jù),結(jié)果寫入PixelMap中,使用callback形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

src

ArrayBuffer

圖像像素數(shù)據(jù)。

callback

AsyncCallback<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

  1. const color = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. let bufferArr = new Uint8Array(color);
  3. for (var i = 0; i < bufferArr.length; i++) {
  4. bufferArr[i] = i + 1;
  5. }
  6. pixelmap.writeBufferToPixels(color, function(err) {
  7. if (err) {
  8. console.error("Failed to write data from a buffer to a PixelMap.");
  9. return;
  10. } else {
  11. console.log("Succeeded in writing data from a buffer to a PixelMap.");
  12. }
  13. });

getImageInfo7+

getImageInfo(): Promise<ImageInfo>

獲取圖像像素信息,使用Promise形式返回獲取的圖像像素信息。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

返回值:

類型

說明

Promise<ImageInfo>

Promise實例,用于異步獲取圖像像素信息,失敗時返回錯誤信息。

示例:

  1. const color = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. let opts = { editable: true, pixelFormat: 2, size: { height: 6, width: 8 } }
  3. image.createPixelMap(color, opts).then(pixelmap => {
  4. if (pixelmap == undefined) {
  5. console.error("Failed to obtain the image pixel map information.");
  6. }
  7. pixelmap.getImageInfo().then(imageInfo => {
  8. if (imageInfo == undefined) {
  9. console.error("Failed to obtain the image pixel map information.");
  10. }
  11. if (imageInfo.size.height == 4 && imageInfo.size.width == 6) {
  12. console.log("Succeeded in obtaining the image pixel map information.");
  13. }
  14. })
  15. })

getImageInfo7+

getImageInfo(callback: AsyncCallback<ImageInfo>): void

獲取圖像像素信息,使用callback形式返回獲取的圖像像素信息。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<ImageInfo>

獲取圖像像素信息回調(diào),異步返回圖像像素信息,失敗時返回錯誤信息。

示例:

  1. const color = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
  3. image.createPixelMap(color, opts, (err, pixelmap) => {
  4. if (pixelmap == undefined) {
  5. console.error("Failed to obtain the image pixel map information.");
  6. }
  7. pixelmap.getImageInfo((err, imageInfo) => {
  8. if (imageInfo == undefined) {
  9. console.error("Failed to obtain the image pixel map information.");
  10. }
  11. if (imageInfo.size.height == 4 && imageInfo.size.width == 6) {
  12. console.log("Succeeded in obtaining the image pixel map information.");
  13. }
  14. })
  15. })

getBytesNumberPerRow7+

getBytesNumberPerRow(): number

獲取圖像像素每行字節(jié)數(shù)。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

返回值:

類型

說明

number

圖像像素的行字節(jié)數(shù)。

示例:

  1. const color = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. let bufferArr = new Uint8Array(color);
  3. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
  4. image.createPixelMap(color, opts, (err,pixelmap) => {
  5. let rowCount = pixelmap.getBytesNumberPerRow();
  6. })

getPixelBytesNumber7+

getPixelBytesNumber(): number

獲取圖像像素的總字節(jié)數(shù)。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

返回值:

類型

說明

number

圖像像素的總字節(jié)數(shù)。

示例:

  1. let pixelBytesNumber = pixelmap.getPixelBytesNumber();

getDensity9+

getDensity():number

獲取當前圖像像素的密度。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

返回值:

類型

說明

number

圖像像素的密度。

示例:

  1. let getDensity = pixelmap.getDensity();

opacity9+

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

通過設置透明比率來讓PixelMap達到對應的透明效果,使用callback形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

rate

number

透明比率的值,取值范圍:0-1。

callback

AsyncCallback<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

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

opacity9+

opacity(rate: number): Promise<void>

通過設置透明比率來讓PixelMap達到對應的透明效果,使用Promise形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

rate

number

透明比率的值,取值范圍:0-1。

返回值:

類型

說明

Promise<void>

Promise實例,用于獲取結(jié)果,失敗時返回錯誤信息。

示例:

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

createAlphaPixelmap9+

createAlphaPixelmap(): Promise<PixelMap>

根據(jù)Alpha通道的信息,來生成一個僅包含Alpha通道信息的pixelmap,可用于陰影效果,使用Promise形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

返回值:

類型

說明

Promise<PixelMap>

Promise實例,返回pixelmap。

示例:

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

createAlphaPixelmap9+

createAlphaPixelmap(callback: AsyncCallback<PixelMap>): void

根據(jù)Alpha通道的信息,來生成一個僅包含Alpha通道信息的pixelmap,可用于陰影效果,使用callback形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<PixelMap>

獲取回調(diào),異步返回結(jié)果。

示例:

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

scale9+

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

根據(jù)輸入的寬高對圖片進行縮放,使用callback形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

x

number

寬度的縮放值,其值為輸入的倍數(shù)。

y

number

高度的縮放值,其值為輸入的倍數(shù)。

callback

AsyncCallback<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

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

scale9+

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

根據(jù)輸入的寬高對圖片進行縮放,使用Promise形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

x

number

寬度的縮放值,其值為輸入的倍數(shù)。

y

number

高度的縮放值,其值為輸入的倍數(shù)。

返回值:

類型

說明

Promise<void>

Promise實例,異步返回結(jié)果。

示例:

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

translate9+

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

根據(jù)輸入的坐標對圖片進行位置變換,使用callback形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

x

number

區(qū)域橫坐標。

y

number

區(qū)域縱坐標。

callback

AsyncCallback<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

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

translate9+

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

根據(jù)輸入的坐標對圖片進行位置變換,使用Promise形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

x

number

區(qū)域橫坐標。

y

number

區(qū)域縱坐標。

返回值:

類型

說明

Promise<void>

Promise實例,異步返回結(jié)果。

示例:

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

rotate9+

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

根據(jù)輸入的角度對圖片進行旋轉(zhuǎn),使用callback形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

angle

number

圖片旋轉(zhuǎn)的角度。

callback

AsyncCallback<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

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

rotate9+

rotate(angle: number): Promise<void>

根據(jù)輸入的角度對圖片進行旋轉(zhuǎn),使用Promise形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

angle

number

圖片旋轉(zhuǎn)的角度。

返回值:

類型

說明

Promise<void>

Promise實例,異步返回結(jié)果。

示例:

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

flip9+

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

根據(jù)輸入的條件對圖片進行翻轉(zhuǎn),使用callback形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

horizontal

boolean

水平翻轉(zhuǎn)。

vertical

boolean

垂直翻轉(zhuǎn)。

callback

AsyncCallback<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

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

flip9+

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

根據(jù)輸入的條件對圖片進行翻轉(zhuǎn),使用Promise形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

horizontal

boolean

水平翻轉(zhuǎn)。

vertical

boolean

垂直翻轉(zhuǎn)。

返回值:

類型

說明

Promise<void>

Promise實例,異步返回結(jié)果。

示例:

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

crop9+

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

根據(jù)輸入的尺寸對圖片進行裁剪,使用callback形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

region

Region

裁剪的尺寸。

callback

AsyncCallback<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

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

crop9+

crop(region: Region): Promise<void>

根據(jù)輸入的尺寸對圖片進行裁剪,使用Promise形式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

region

Region

裁剪的尺寸。

返回值:

類型

說明

Promise<void>

Promise實例,異步返回結(jié)果。

示例:

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

release7+

release():Promise<void>

釋放PixelMap對象,使用Promise形式返回釋放結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

返回值:

類型

說明

Promise<void>

Promise實例,異步返回釋放結(jié)果。

示例:

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

release7+

release(callback: AsyncCallback<void>): void

釋放PixelMap對象,使用callback形式返回釋放結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<void>

異步返回釋放結(jié)果。

示例:

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

image.createImageSource

createImageSource(uri: string): ImageSource

通過傳入的uri創(chuàng)建圖片源實例。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

uri

string

圖片路徑,當前僅支持應用沙箱路徑。

當前支持格式有:.jpg .png .gif .bmp .webp RAW。

返回值:

類型

說明

ImageSource

返回ImageSource類實例,失敗時返回undefined。

示例:

  1. //Stage模型
  2. const context = getContext(this);
  3. const path = context.cacheDir + "/test.jpg";
  4. const imageSourceApi = image.createImageSource(path);
  1. //FA模型
  2. import featureAbility from '@ohos.ability.featureAbility';
  3. const context = featureAbility.getContext();
  4. const path = context.getCacheDir() + "/test.jpg";
  5. const imageSourceApi = image.createImageSource(path);

image.createImageSource9+

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

通過傳入的uri創(chuàng)建圖片源實例。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

uri

string

圖片路徑,當前僅支持應用沙箱路徑。

當前支持格式有:.jpg .png .gif .bmp .webp RAW。

options

SourceOptions

圖片屬性,包括圖片序號與默認屬性值。

返回值:

類型

說明

ImageSource

返回ImageSource類實例,失敗時返回undefined。

示例:

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

image.createImageSource7+

createImageSource(fd: number): ImageSource

通過傳入文件描述符來創(chuàng)建圖片源實例。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

文件描述符fd。

返回值:

類型

說明

ImageSource

返回ImageSource類實例,失敗時返回undefined。

示例:

  1. const imageSourceApi = image.createImageSource(0);

image.createImageSource9+

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

通過傳入文件描述符來創(chuàng)建圖片源實例。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

文件描述符fd。

options

SourceOptions

圖片屬性,包括圖片序號與默認屬性值。

返回值:

類型

說明

ImageSource

返回ImageSource類實例,失敗時返回undefined。

示例:

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

image.createImageSource9+

createImageSource(buf: ArrayBuffer): ImageSource

通過緩沖區(qū)創(chuàng)建圖片源實例。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

buf

ArrayBuffer

圖像緩沖區(qū)數(shù)組。

示例:

  1. const buf = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. const imageSourceApi = image.createImageSource(buf);

image.createImageSource9+

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

通過緩沖區(qū)創(chuàng)建圖片源實例。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

buf

ArrayBuffer

圖像緩沖區(qū)數(shù)組。

options

SourceOptions

圖片屬性,包括圖片序號與默認屬性值。

返回值:

類型

說明

ImageSource

返回ImageSource類實例,失敗時返回undefined。

示例:

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

image.CreateIncrementalSource9+

CreateIncrementalSource(buf: ArrayBuffer): ImageSource

通過緩沖區(qū)以增量的方式創(chuàng)建圖片源實例。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

buf

ArrayBuffer

增量數(shù)據(jù)。

返回值:

類型

說明

ImageSource

返回圖片源,失敗時返回undefined。

示例:

  1. const buf = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. const imageSourceIncrementalSApi = image.CreateIncrementalSource(buf);

image.CreateIncrementalSource9+

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

通過緩沖區(qū)以增量的方式創(chuàng)建圖片源實例。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

buf

ArrayBuffer

增量數(shù)據(jù)。

options

SourceOptions

圖片屬性,包括圖片序號與默認屬性值。

返回值:

類型

說明

ImageSource

返回圖片源,失敗時返回undefined。

示例:

  1. const buf = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. const imageSourceIncrementalSApi = image.CreateIncrementalSource(buf);

ImageSource

圖片源類,用于獲取圖片相關信息。在調(diào)用ImageSource的方法前,需要先通過createImageSource構(gòu)建一個ImageSource實例。

屬性

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

名稱

類型

可讀

可寫

說明

supportedFormats

Array<string>

支持的圖片格式,包括:png,jpeg,bmp,gif,webp,RAW。

getImageInfo

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

獲取指定序號的圖片信息,使用callback形式返回圖片信息。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

index

number

創(chuàng)建圖片源時的序號。

callback

AsyncCallback<ImageInfo>

獲取圖片信息回調(diào),異步返回圖片信息對象。

示例:

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

getImageInfo

getImageInfo(callback: AsyncCallback<ImageInfo>): void

獲取圖片信息,使用callback形式返回圖片信息。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<ImageInfo>

獲取圖片信息回調(diào),異步返回圖片信息對象。

示例:

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

getImageInfo

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

獲取圖片信息,使用Promise形式返回圖片信息。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

index

number

創(chuàng)建圖片源時的序號,不選擇時默認為0。

返回值:

類型

說明

Promise<ImageInfo>

返回獲取到的圖片信息。

示例:

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

getImageProperty7+

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

獲取圖片中給定索引處圖像的指定屬性鍵的值,用Promise形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

key

string

圖片屬性名。

options

GetImagePropertyOptions

圖片屬性,包括圖片序號與默認屬性值。

返回值:

類型

說明

Promise<string>

Promise實例,用于異步獲取圖片屬性值,如獲取失敗則返回屬性默認值。

示例:

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

getImageProperty7+

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

獲取圖片中給定索引處圖像的指定屬性鍵的值,用callback形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

key

string

圖片屬性名。

callback

AsyncCallback<string>

獲取圖片屬性回調(diào),返回圖片屬性值,如獲取失敗則返回屬性默認值。

示例:

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

getImageProperty7+

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

獲取圖片指定屬性鍵的值,callback形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

key

string

圖片屬性名。

options

GetImagePropertyOptions

圖片屬性,包括圖片序號與默認屬性值。

callback

AsyncCallback<string>

獲取圖片屬性回調(diào),返回圖片屬性值,如獲取失敗則返回屬性默認值。

示例:

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

modifyImageProperty9+

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

通過指定的鍵修改圖片屬性的值,使用Promise形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

key

string

圖片屬性名。

value

string

屬性值。

返回值:

類型

說明

Promise<void>

Promise實例,異步返回結(jié)果。

示例:

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

modifyImageProperty9+

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

通過指定的鍵修改圖片屬性的值,callback形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

key

string

圖片屬性名。

value

string

屬性值。

callback

AsyncCallback<void>

修改屬性值,callback返回結(jié)果。

示例:

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

updateData9+

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

更新增量數(shù)據(jù),使用Promise形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

buf

ArrayBuffer

增量數(shù)據(jù)。

isFinished

boolean

是否更新完。

value

number

偏移量。

length

number

數(shù)組長。

返回值:

類型

說明

Promise<void>

Promise實例,異步返回結(jié)果。

示例:

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

updateData9+

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

更新增量數(shù)據(jù),callback形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

buf

ArrayBuffer

增量數(shù)據(jù)。

isFinished

boolean

是否更新完。

value

number

偏移量。

length

number

數(shù)組長。

callback

AsyncCallback<void>

回調(diào)表示成功或失敗。

示例:

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

createPixelMap7+

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

通過圖片解碼參數(shù)創(chuàng)建PixelMap對象。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

options

DecodingOptions

解碼參數(shù)。

返回值:

類型

說明

Promise<PixelMap>

異步返回Promise對象。

示例:

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

createPixelMap7+

createPixelMap(callback: AsyncCallback<PixelMap>): void

通過默認參數(shù)創(chuàng)建PixelMap對象,使用callback形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<PixelMap>

通過回調(diào)返回PixelMap對象。

示例:

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

createPixelMap7+

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

通過圖片解碼參數(shù)創(chuàng)建PixelMap對象。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

options

DecodingOptions

解碼參數(shù)。

callback

AsyncCallback<PixelMap>

通過回調(diào)返回PixelMap對象。

示例:

  1. let decodingOptions = {
  2. sampleSize: 1,
  3. editable: true,
  4. desiredSize: { width: 1, height: 2 },
  5. rotate: 10,
  6. desiredPixelFormat: 3,
  7. desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 0 },
  8. index: 0
  9. };
  10. imageSourceApi.createPixelMap(decodingOptions, pixelmap => {
  11. console.log('Succeeded in creating pixelmap object.');
  12. })

release

release(callback: AsyncCallback<void>): void

釋放圖片源實例,使用callback形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<void>

資源釋放回調(diào),失敗時返回錯誤信息。

示例:

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

release

release(): Promise<void>

釋放圖片源實例,使用Promise形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

返回值:

類型

說明

Promise<void>

Promise實例,異步返回結(jié)果。

示例:

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

image.createImagePacker

createImagePacker(): ImagePacker

創(chuàng)建ImagePacker實例。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImagePacker

返回值:

類型

說明

ImagePacker

返回ImagePacker實例。

示例:

  1. const imagePackerApi = image.createImagePacker();

ImagePacker

圖片打包器類,用于圖片壓縮和打包。在調(diào)用ImagePacker的方法前,需要先通過createImagePacker構(gòu)建一個ImagePacker實例,當前支持格式有:jpeg webp。

屬性

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImagePacker

名稱

類型

可讀

可寫

說明

supportedFormats

Array<string>

圖片打包支持的格式,jpeg。

packing

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

圖片壓縮或重新打包,使用callback形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImagePacker

參數(shù):

參數(shù)名

類型

必填

說明

source

ImageSource

打包的圖片源。

option

PackingOption

設置打包參數(shù)。

callback

AsyncCallback<ArrayBuffer>

獲取圖片打包回調(diào),返回打包后數(shù)據(jù)。

示例:

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

packing

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

圖片壓縮或重新打包,使用Promise形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImagePacker

參數(shù):

參數(shù)名

類型

必填

說明

source

ImageSource

打包的圖片源。

option

PackingOption

設置打包參數(shù)。

返回值:

類型

說明

Promise<ArrayBuffer>

Promise實例,用于異步獲取壓縮或打包后的數(shù)據(jù)。

示例:

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

packing8+

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

圖片壓縮或重新打包,使用callback形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImagePacker

參數(shù):

參數(shù)名

類型

必填

說明

source

PixelMap

打包的PixelMap資源。

option

PackingOption

設置打包參數(shù)。

callback

AsyncCallback<ArrayBuffer>

獲取圖片打包回調(diào),返回打包后數(shù)據(jù)。

示例:

  1. const color = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. let bufferArr = new Uint8Array(color);
  3. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
  4. image.createPixelMap(color, opts).then((pixelmap) => {
  5. let packOpts = { format:"image/jpeg", quality:98 }
  6. imagePackerApi.packing(pixelmap, packOpts, data => {
  7. console.log('Succeeded in packing the image.');
  8. })
  9. })

packing8+

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

圖片壓縮或重新打包,使用Promise形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImagePacker

參數(shù):

參數(shù)名

類型

必填

說明

source

PixelMap

打包的PixelMap源。

option

PackingOption

設置打包參數(shù)。

返回值:

類型

說明

Promise<ArrayBuffer>

Promise實例,用于異步獲取壓縮或打包后的數(shù)據(jù)。

示例:

  1. const color = new ArrayBuffer(96); //96為需要創(chuàng)建的像素buffer大小,取值為:height * width *4
  2. let bufferArr = new Uint8Array(color);
  3. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
  4. image.createPixelMap(color, opts).then((pixelmap) => {
  5. let packOpts = { format:"image/jpeg", quality:98 }
  6. imagePackerApi.packing(pixelmap, packOpts)
  7. .then( data => {
  8. console.log('Succeeded in packing the image.');
  9. }).catch(error => {
  10. console.log('Failed to pack the image..');
  11. })
  12. })

release

release(callback: AsyncCallback<void>): void

釋放圖片打包實例,使用callback形式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImagePacker

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<void>

釋放回調(diào),失敗時返回錯誤信息。

示例:

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

release

release(): Promise<void>

釋放圖片打包實例,使用Promise形式返回釋放結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImagePacker

返回值:

類型

說明

Promise<void>

Promise實例,用于異步獲取釋放結(jié)果,失敗時返回錯誤信息。

示例:

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

image.createImageReceiver9+

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

通過寬、高、圖片格式、容量創(chuàng)建ImageReceiver實例。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageReceiver

參數(shù):

參數(shù)名

類型

必填

說明

width

number

圖像的默認寬度。

height

number

圖像的默認高度。

format

number

圖像格式,取值為ImageFormat常量(目前僅支持 ImageFormat:JPEG 和 4)。

capacity

number

同時訪問的最大圖像數(shù)。

返回值:

類型

說明

ImageReceiver

如果操作成功,則返回ImageReceiver實例。

示例:

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

ImageReceiver9+

圖像接收類,用于獲取組件surface id,接收最新的圖片和讀取下一張圖片,以及釋放ImageReceiver實例。

在調(diào)用以下方法前需要先創(chuàng)建ImageReceiver實例。

屬性

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageReceiver

名稱

類型

可讀

可寫

說明

size

Size

圖片大小。

capacity

number

同時訪問的圖像數(shù)。

format

ImageFormat

圖像格式。

getReceivingSurfaceId9+

getReceivingSurfaceId(callback: AsyncCallback<string>): void

用于獲取一個surface id供Camera或其他組件使用。使用callback返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageReceiver

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<string>

回調(diào)函數(shù),返回surface id。

示例:

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

getReceivingSurfaceId9+

getReceivingSurfaceId(): Promise<string>

用于獲取一個surface id供Camera或其他組件使用。使用promise返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageReceiver

返回值:

類型

說明

Promise<string>

異步返回surface id。

示例:

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

readLatestImage9+

readLatestImage(callback: AsyncCallback<Image>): void

從ImageReceiver讀取最新的圖片,并使用callback返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageReceiver

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<Image>

回調(diào)函數(shù),返回最新圖像。

示例:

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

readLatestImage9+

readLatestImage(): Promise<Image>

從ImageReceiver讀取最新的圖片,并使用promise返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageReceiver

返回值:

類型

說明

Promise<Image>

異步返回最新圖片。

示例:

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

readNextImage9+

readNextImage(callback: AsyncCallback<Image>): void

從ImageReceiver讀取下一張圖片,并使用callback返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageReceiver

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<Image>

回調(diào)函數(shù),返回下一張圖片。

示例:

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

readNextImage9+

readNextImage(): Promise<Image>

從ImageReceiver讀取下一張圖片,并使用promise返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageReceiver

返回值:

類型

說明

Promise<Image>

異步返回下一張圖片。

示例:

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

on9+

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

接收圖片時注冊回調(diào)。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageReceiver

參數(shù):

參數(shù)名

類型

必填

說明

type

string

注冊事件的類型,固定為'imageArrival',接收圖片時觸發(fā)。

callback

AsyncCallback<void>

注冊的事件回調(diào)。

示例:

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

release9+

release(callback: AsyncCallback<void>): void

釋放ImageReceiver實例并使用回調(diào)返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageReceiver

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<void>

回調(diào)函數(shù),返回操作結(jié)果。

示例:

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

release9+

release(): Promise<void>

釋放ImageReceiver實例并使用promise返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageReceiver

返回值:

類型

說明

Promise<void>

異步返回操作結(jié)果。

示例:

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

image.createImageCreator9+

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

通過寬、高、圖片格式、容量創(chuàng)建ImageCreator實例。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageCreator

參數(shù):

參數(shù)名

類型

必填

說明

width

number

圖像的默認寬度。

height

number

圖像的默認高度。

format

number

圖像格式,如YCBCR_422_SP,JPEG。

capacity

number

同時訪問的最大圖像數(shù)。

返回值:

類型

說明

ImageCreator

如果操作成功,則返回ImageCreator實例。

示例:

  1. var creator = image.createImageCreator(8192, 8, 4, 8);

ImageCreator9+

圖像創(chuàng)建模塊,用于請求圖像原生數(shù)據(jù)區(qū)域,并開放給應用編譯原生圖像數(shù)據(jù)的能力。

在調(diào)用以下方法前需要先創(chuàng)建ImageCreator實例。

屬性

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageCreator

名稱

類型

可讀

可寫

說明

capacity

number

同時訪問的圖像數(shù)。

format

ImageFormat

圖像格式。

dequeueImage9+

dequeueImage(callback: AsyncCallback<Image>): void

從空閑隊列中獲取buffer圖片,用于繪制UI內(nèi)容,并使用callback返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageCreator

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<Image>

回調(diào)函數(shù),返回最新圖片。

示例:

  1. creator.dequeueImage((err, img) => {
  2. if (err) {
  3. console.info('dequeueImage failded.');
  4. }
  5. console.info('dequeueImage succeeded.');
  6. });

dequeueImage9+

dequeueImage(): Promise<Image>

從空閑隊列中獲取buffer圖片,用于繪制UI內(nèi)容,并使用promise返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageCreator

返回值:

類型

說明

Promise<Image>

返回繪制的圖像。

示例:

  1. creator.dequeueImage().then(img => {
  2. console.info('dequeueImage succeeded.');
  3. }).catch(error => {
  4. console.log('dequeueImage failed: ' + error);
  5. })

queueImage9+

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

將繪制好的圖片放入Dirty隊列,并使用callback返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageCreator

參數(shù):

參數(shù)名

類型

必填

說明

interface

Image

繪制好的buffer圖像。

callback

AsyncCallback<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

  1. creator.dequeueImage().then(img => {
  2. //繪制圖片
  3. img.getComponent(4).then(component => {
  4. var bufferArr = new Uint8Array(component.byteBuffer);
  5. for (var i = 0; i < bufferArr.length; i += 4) {
  6. bufferArr[i] = 0; //B
  7. bufferArr[i + 1] = 0; //G
  8. bufferArr[i + 2] = 255; //R
  9. bufferArr[i + 3] = 255; //A
  10. }
  11. })
  12. creator.queueImage(img, (err) => {
  13. if (err) {
  14. console.info('queueImage failed: ' + err);
  15. }
  16. console.info('queueImage succeeded');
  17. })
  18. })

queueImage9+

queueImage(interface: Image): Promise<void>

將繪制好的圖片放入Dirty隊列,并使用promise返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageCreator

參數(shù):

參數(shù)名

類型

必填

說明

interface

Image

繪制好的buffer圖像。

返回值:

類型

說明

Promise<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

  1. creator.dequeueImage().then(img => {
  2. //繪制圖片
  3. img.getComponent(4).then(component => {
  4. var bufferArr = new Uint8Array(component.byteBuffer);
  5. for (var i = 0; i < bufferArr.length; i += 4) {
  6. bufferArr[i] = 0; //B
  7. bufferArr[i + 1] = 0; //G
  8. bufferArr[i + 2] = 255; //R
  9. bufferArr[i + 3] = 255; //A
  10. }
  11. })
  12. creator.queueImage(img).then(() => {
  13. console.info('queueImage succeeded.');
  14. }).catch(error => {
  15. console.info('queueImage failed: ' + error);
  16. })
  17. })

on9+

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

監(jiān)聽imageRelease事件,并使用callback返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageCreator

參數(shù):

參數(shù)名

類型

必填

說明

type

string

監(jiān)聽事件類型,如'imageRelease'。

callback

AsyncCallback<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

  1. creator.on('imageRelease', (err) => {
  2. if (err) {
  3. console.info('on faild' + err);
  4. }
  5. console.info('on succeeded');
  6. })

release9+

release(callback: AsyncCallback<void>): void

釋放當前圖像,并使用callback返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageCreator

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

  1. creator.release((err) => {
  2. if (err) {
  3. console.info('release failed: ' + err);
  4. }
  5. console.info('release succeeded');
  6. });

release9+

release(): Promise<void>

釋放當前圖像,并使用promise返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageCreator

返回值:

類型

說明

Promise<void>

獲取回調(diào),失敗時返回錯誤信息。

示例:

  1. creator.release().then(() => {
  2. console.info('release succeeded');
  3. }).catch(error => {
  4. console.info('release failed');
  5. })

Image9+

提供基本的圖像操作,包括獲取圖像信息、讀寫圖像數(shù)據(jù)。調(diào)用readNextImagereadLatestImage接口時會返回image。

屬性

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

類型

可讀

可寫

說明

clipRect

Region

要裁剪的圖像區(qū)域。

size

Size

圖像大小。

format

number

圖像格式,參考PixelMapFormat

getComponent9+

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

根據(jù)圖像的組件類型從圖像中獲取組件緩存并使用callback返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

componentType

ComponentType

圖像的組件類型。

callback

AsyncCallback<Component>

用于返回組件緩沖區(qū)。

示例:

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

getComponent9+

getComponent(componentType: ComponentType): Promise<Component>

根據(jù)圖像的組件類型從圖像中獲取組件緩存并使用Promise方式返回結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

componentType

ComponentType

圖像的組件類型。

返回值:

類型

說明

Promise<Component>

用于返回組件緩沖區(qū)的promise實例。

示例:

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

release9+

release(callback: AsyncCallback<void>): void

釋放當前圖像并使用callback返回結(jié)果。

在接收另一個圖像前必須先釋放對應資源。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<void>

返回操作結(jié)果。

示例:

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

release9+

release(): Promise<void>

釋放當前圖像并使用Promise方式返回結(jié)果。

在接收另一個圖像前必須先釋放對應資源。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

返回值:

類型

說明

Promise<void>

promise返回操作結(jié)果。

示例:

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

PositionArea7+

表示圖片指定區(qū)域內(nèi)的數(shù)據(jù)。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

類型

可讀

可寫

說明

pixels

ArrayBuffer

像素。

offset

number

偏移量。

stride

number

像素間距,stride >= region.size.width*4。

region

Region

區(qū)域,按照區(qū)域讀寫。寫入的區(qū)域?qū)挾燃覺坐標不能大于原圖的寬度,寫入的區(qū)域高度加Y坐標不能大于原圖的高度。

ImageInfo

表示圖片信息。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

類型

可讀

可寫

說明

size

Size

圖片大小。

density9+

number

像素密度,單位為ppi。

Size

表示圖片尺寸。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

類型

可讀

可寫

說明

height

number

輸出圖片的高。

width

number

輸出圖片的寬。

PixelMapFormat7+

枚舉,圖片像素格式。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

說明

UNKNOWN

0

未知格式。

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+

枚舉,圖像的透明度類型。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

說明

UNKNOWN

0

未知透明度。

OPAQUE

1

沒有alpha或圖片全透明。

PREMUL

2

RGB前乘alpha。

UNPREMUL

3

RGB不前乘alpha。

ScaleMode9+

枚舉,圖像的縮放模式。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

說明

CENTER_CROP

1

縮放圖像以填充目標圖像區(qū)域并居中裁剪區(qū)域外的效果。

FIT_TARGET_SIZE

0

圖像適合目標尺寸的效果。

SourceOptions9+

ImageSource的初始化選項。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

類型

可讀

可寫

說明

sourceDensity

number

ImageSource的密度。

sourcePixelFormat

PixelMapFormat

圖片像素格式。

sourceSize

Size

圖像像素大小。

InitializationOptions8+

PixelMap的初始化選項。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

類型

可讀

可寫

說明

alphaType9+

AlphaType

透明度。

editable

boolean

是否可編輯。

pixelFormat

PixelMapFormat

像素格式。

scaleMode9+

ScaleMode

縮略值。

size

Size

創(chuàng)建圖片大小。

DecodingOptions7+

圖像解碼設置選項。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

名稱

類型

可讀

可寫

說明

sampleSize

number

縮略圖采樣大小。

rotate

number

旋轉(zhuǎn)角度。

editable

boolean

是否可編輯。

desiredSize

Size

期望輸出大小。

desiredRegion

Region

解碼區(qū)域。

desiredPixelFormat

PixelMapFormat

解碼的像素格式。

index

number

解碼圖片序號。

fitDensity9+

number

圖像像素密度,單位為ppi。

Region7+

表示區(qū)域信息。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

類型

可讀

可寫

說明

size

Size

區(qū)域大小。

x

number

區(qū)域橫坐標。

y

number

區(qū)域縱坐標。

PackingOption

表示圖片打包選項。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImagePacker

名稱

類型

可讀

可寫

說明

format

string

目標格式。

當前只支持jpg和webp。

quality

number

JPEG編碼中設定輸出圖片質(zhì)量的參數(shù),取值范圍為1-100。

bufferSize9+

number

用于設置圖片大小,默認為10M

GetImagePropertyOptions7+

表示查詢圖片屬性的索引。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageSource

名稱

類型

可讀

可寫

說明

index

number

圖片序號。

defaultValue

string

默認屬性值。

PropertyKey7+

枚舉,Exif(Exchangeable image file format)圖片信息。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

說明

BITS_PER_SAMPLE

"BitsPerSample"

每個像素比特數(shù)。

ORIENTATION

"Orientation"

圖片方向。

IMAGE_LENGTH

"ImageLength"

圖片長度。

IMAGE_WIDTH

"ImageWidth"

圖片寬度。

GPS_LATITUDE

"GPSLatitude"

圖片緯度。

GPS_LONGITUDE

"GPSLongitude"

圖片經(jīng)度。

GPS_LATITUDE_REF

"GPSLatitudeRef"

緯度引用,例如N或S。

GPS_LONGITUDE_REF

"GPSLongitudeRef"

經(jīng)度引用,例如W或E。

DATE_TIME_ORIGINAL9+

"DateTimeOriginal"

拍攝時間,例如2022:09:06 15:48:00

EXPOSURE_TIME9+

"ExposureTime"

曝光時間,例如1/33 sec.

SCENE_TYPE9+

"SceneType"

拍攝場景模式,例如人像、風光、運動、夜景等。

ISO_SPEED_RATINGS9+

"ISOSpeedRatings"

ISO感光度,例如400

F_NUMBER9+

"FNumber"

光圈值,例如f/1.8

ImageFormat9+

枚舉,圖片格式。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

說明

YCBCR_422_SP

1000

YCBCR422半平面格式。

JPEG

2000

JPEG編碼格式。

ComponentType9+

枚舉,圖像的組件類型。

系統(tǒng)能力: SystemCapability.Multimedia.Image.ImageReceiver

名稱

說明

YUV_Y

1

亮度信息。

YUV_U

2

色度信息。

YUV_V

3

色度信息。

JPEG

4

JPEG 類型。

Component9+

描述圖像顏色分量。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

類型

可讀

可寫

說明

componentType

ComponentType

組件類型。

rowStride

number

行距。

pixelStride

number

像素間距。

byteBuffer

ArrayBuffer

組件緩沖區(qū)。

ResponseCode

編譯錯誤返回的響應碼。

名稱

說明

ERR_MEDIA_INVALID_VALUE

-1

無效大小。

SUCCESS

0

操作成功。

ERROR

62980096

操作失敗。

ERR_IPC

62980097

ipc錯誤。

ERR_SHAMEM_NOT_EXIST

62980098

共享內(nèi)存錯誤。

ERR_SHAMEM_DATA_ABNORMAL

62980099

共享內(nèi)存錯誤。

ERR_IMAGE_DECODE_ABNORMAL

62980100

圖像解碼錯誤。

ERR_IMAGE_DATA_ABNORMAL

62980101

圖像輸入數(shù)據(jù)錯誤。

ERR_IMAGE_MALLOC_ABNORMAL

62980102

圖像malloc錯誤。

ERR_IMAGE_DATA_UNSUPPORT

62980103

不支持圖像類型。

ERR_IMAGE_INIT_ABNORMAL

62980104

圖像初始化錯誤。

ERR_IMAGE_GET_DATA_ABNORMAL

62980105

圖像獲取數(shù)據(jù)錯誤。

ERR_IMAGE_TOO_LARGE

62980106

圖像數(shù)據(jù)太大。

ERR_IMAGE_TRANSFORM

62980107

圖像轉(zhuǎn)換錯誤。

ERR_IMAGE_COLOR_CONVERT

62980108

圖像顏色轉(zhuǎn)換錯誤。

ERR_IMAGE_CROP

62980109

裁剪錯誤。

ERR_IMAGE_SOURCE_DATA

62980110

圖像源數(shù)據(jù)錯誤。

ERR_IMAGE_SOURCE_DATA_INCOMPLETE

62980111

圖像源數(shù)據(jù)不完整。

ERR_IMAGE_MISMATCHED_FORMAT

62980112

圖像格式不匹配。

ERR_IMAGE_UNKNOWN_FORMAT

62980113

圖像未知格式。

ERR_IMAGE_SOURCE_UNRESOLVED

62980114

圖像源未解析。

ERR_IMAGE_INVALID_PARAMETER

62980115

圖像無效參數(shù)。

ERR_IMAGE_DECODE_FAILED

62980116

解碼失敗。

ERR_IMAGE_PLUGIN_REGISTER_FAILED

62980117

注冊插件失敗。

ERR_IMAGE_PLUGIN_CREATE_FAILED

62980118

創(chuàng)建插件失敗。

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

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號