目录

visionImageAnalyzer (AI Image Recognition Control)

AI image recognition provides scenario-based text recognition, subject segmentation, and image search functions by aggregating AI capabilities such as Optical Character Recognition (OCR), subject segmentation, entity recognition, and multi-object recognition.

Exceptions need to be captured when an API is called.

Start version: 5.0.0(12)

Module Import

import { visionImageAnalyzer } from '@kit.VisionKit';

Menu

AI image recognition menu.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Name

Type

Read-Only

Optional

Description

value

ResourceStr

Yes

No

Menu content.

action

Callback<string | Subject[]>

Yes

No

Menu result callback, including the text selection result and cropping result.

Rect

Rectangular data structure.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Name

Type

Read-Only

Optional

Description

left

number

Yes

Yes

Left position of a rectangle.

top

number

Yes

Yes

Top position of a rectangle.

right

number

Yes

Yes

Right position of a rectangle.

bottom

number

Yes

Yes

Bottom position of a rectangle.

Subject

Subject recognition result.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Name

Type

Read-Only

Optional

Description

id

number

Yes

No

Subject ID.

image

PixelMap

Yes

No

Image from which a subject is recognized.

boundingBox

Rect

Yes

No

Bounding box of a recognized subject.

SelectedStatus

Enumerated values of the object selection status during image recognition.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Name

Value

Description

SELECTED

0

Selected.

UNSELECTED

1

Unselected.

ImageAnalyzerVisibility

Enumerated values of the AI image recognition control visibility.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Name

Value

Description

SHOWN

0

Visible.

HIDDEN

1

Invisible.

AIButtonStatus

Enumerated values of the AIButton status.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Name

Value

Description

SELECTED

0

Selected.

UNSELECTED

1

Unselected.

HIDDEN

2

Hidden.

VisionImageAnalyzerController

Visual image controller used for controlling interactions. It inherits from the ImageAnalyzerController class.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

[h2]setImageAnalyzerVisibility

setImageAnalyzerVisibility(visibility: ImageAnalyzerVisibility): void

Sets the visibility of the AI image recognition control.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

visibility

ImageAnalyzerVisibility

Yes

Visibility of the AI image recognition control.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear() {
    this.visionImageAnalyzerController.setImageAnalyzerVisibility(visionImageAnalyzer.ImageAnalyzerVisibility.HIDDEN)
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
          types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
          aiController: this.visionImageAnalyzerController
        })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]setAIButtonPosition

setAIButtonPosition(position: Rect): void

Sets the position of AIButton.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

position

Rect

Yes

Position of AIButton relative to the four sides of the AI image recognition control, in vp. By default, it is displayed in the lower right corner.

If some parameters in Rect are passed, use the passed parameters preferentially for position matching. If the position parameter is abnormal, the default position will be used for display.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear() {
    let position: visionImageAnalyzer.Rect = {
      bottom: 100
    }
    this.visionImageAnalyzerController.setAIButtonPosition(position)
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
          types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
          aiController: this.visionImageAnalyzerController
        })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]setAIButtonVisibility

setAIButtonVisibility(visible: boolean): void

Sets the visibility of AIButton.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

visible

boolean

Yes

Indicates whether AIButton is visible. The value true indicates yes, and the value false indicates no.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear() {
    this.visionImageAnalyzerController.setAIButtonVisibility(true)
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
          types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
          aiController: this.visionImageAnalyzerController
        })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]setCustomTextMenuItems

setCustomTextMenuItems(menus: Menu[]): void

Sets customized menu items for text analysis.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

menus

Menu[]

Yes

Customized menu items to be added for selected text. The callback contains the selected text.

A maximum of three menu items can be displayed. If there are more than three menu items, the first three are displayed.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear() {
    this.visionImageAnalyzerController.setAIButtonVisibility(true)
    this.visionImageAnalyzerController.setCustomTextMenuItems([
      {
        value: "menu2",
        action: (param: string | visionImageAnalyzer.Subject[]) => {
          console.info("DEMO_TAG", "text menu clicked")
        }
      }
    ]
    )
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]startSubjectAnalyzer

startSubjectAnalyzer(): void

Enables subject recognition.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear() {
    this.visionImageAnalyzerController.startSubjectAnalyzer()
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]setCustomSubjectMenuItems

setCustomSubjectMenuItems(menus: Menu[]): void

Sets customized menu items for subject analysis.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

menus

Menu[]

Yes

Customized menu items to be added for selected subjects. The callback contains the selected subjects.

A maximum of three menu items can be displayed. If there are more than three menu items, the first three are displayed.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear() {
    this.visionImageAnalyzerController.setCustomSubjectMenuItems([
      {
        value: "menu2",
        action: (param: string | visionImageAnalyzer.Subject[]) => {
          console.info("DEMO_TAG", "subject menu clicked")
        }
      }
    ]
    )
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]setSelectedSubjects

setSelectedSubjects(subjectIds: number[]): void

Sets selected subjects based on the subject ID list.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

subjectIds

number[]

Yes

ID list of subjects to be selected. Only the subject IDs contained in the image recognition result are valid.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct SingleImageTest2 {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear() {
    this.visionImageAnalyzerController.on('subjectAnalysis', (subjects: visionImageAnalyzer.Subject[]) => {
      console.info("DEMO_TAG", `subjectAnalysis result: ${JSON.stringify(subjects)}`)
      if(subjects.length > 0) {
        this.visionImageAnalyzerController.setSelectedSubjects([subjects[0].id])
      }
    })
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]getSelectedSubjects

getSelectedSubjects(): Promise<Subject[] | null>

Obtains the selected subjects. This API returns the result asynchronously through a promise.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Returns:

Type

Description

Promise<Subject[] | null>

Selected subjects, which are returned through a promise.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  getSelectedSubjects() {
    this.visionImageAnalyzerController.getSelectedSubjects().then((subjects: visionImageAnalyzer.Subject[] | null) => {
      console.info("DEMO_TAG", `getSelectedSubjects result: ${JSON.stringify(subjects)}`)
    })
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]getSubject

getSubject(point: visionBase.Point): Promise<Subject | null>

Obtains a subject corresponding to a point position. This API returns the result asynchronously through a promise.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

point

Point

Yes

Point position relative to the upper left corner of an image, in px.

Returns:

Type

Description

Promise<Subject | null>

Subject corresponding to a point position, which is returned through a promise.

Example:

import visionBase from '@hms.ai.vision.visionBase'
import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController = new visionImageAnalyzer.VisionImageAnalyzerController()
  getSelectedSubjects() {
    let searchPoint: visionBase.Point = { x: 100, y: 100 }
    this.visionImageAnalyzerController.getSubject(searchPoint).then((subjects: visionImageAnalyzer.Subject | null) => {
      console.info("DEMO_TAG", `getSubject result: ${JSON.stringify(subjects)}`)
    })
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]getSubjectsImage

getSubjectsImage(subjectIds: number[]): Promise<PixelMap | null>

Obtains an image assembled based on subjects corresponding to specified subject IDs. This API returns the result asynchronously through a promise.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

subjectIds

number[]

Yes

Subject ID list.

Returns:

Type

Description

Promise<PixelMap | null>

Image assembled based on subjects corresponding to specified subject IDs, which is returned through a promise.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  getSubjectsImage() {
    this.visionImageAnalyzerController.on('subjectAnalysis', (subjects: visionImageAnalyzer.Subject[]) => {
      console.info("DEMO_TAG", `subjectAnalysis result: ${JSON.stringify(subjects)}`)
      if(subjects.length > 0) {
        let ids: number[] = [subjects[0].id]
        this.visionImageAnalyzerController.getSubjectsImage(ids).then((image: PixelMap | null) => {
          console.info('Image data obtained successfully: ', image);
        }).catch((error: Error) => {
          console.error('Failed to obtain image data: ', error);
        })
      }
    })
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]startObjectSearch

startObjectSearch(): Promise<boolean>

Enables visual search. This API returns the result asynchronously through a promise.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Returns:

Type

Description

Promise<boolean>

Indicates whether visual search is enabled, which is returned through a promise. The value true indicates yes, and the value false indicates no. The returned value is false if the API is called during text selection or subject recognition.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  startObjectSearch() {
    this.visionImageAnalyzerController.startObjectSearch()
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]stopObjectSearch

stopObjectSearch(): void

Disables visual search. The function cannot be disabled if the visual search interaction is in progress.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  stopObjectSearch() {
    this.visionImageAnalyzerController.stopObjectSearch()
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]on(type: 'aiButtonStatusChange')

on(type: 'aiButtonStatusChange', callback: Callback<AIButtonStatus>): void

Listens to the AIButton display status.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

type

string

Yes

The value is aiButtonStatusChange, indicating listening to the AIButton display status.

callback

Callback<AIButtonStatus>

Yes

Callback function.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear(): void {
    this.visionImageAnalyzerController?.setAIButtonVisibility(true)
    this.visionImageAnalyzerController.on('aiButtonStatusChange', (aiButtonState: visionImageAnalyzer.AIButtonStatus) => {
      console.info("DEMO_TAG", `aiButtonStatusChange result: ${JSON.stringify(aiButtonState)}`)
    })
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]off(type: 'aiButtonStatusChange')

off(type: 'aiButtonStatusChange', callback?: Callback<AIButtonStatus>): void

Cancels the listening to the AIButton display status.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

type

string

Yes

The value is aiButtonStatusChange, indicating canceling the listening to the AIButton display status.

callback

Callback<AIButtonStatus>

No

Callback to be canceled. If this parameter is not specified, all callbacks will be canceled.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear(): void {
    this.visionImageAnalyzerController?.setAIButtonVisibility(true)
    this.visionImageAnalyzerController.on('aiButtonStatusChange', (aiButtonState: visionImageAnalyzer.AIButtonStatus) => {
      console.info("DEMO_TAG", `aiButtonStatusChange result: ${JSON.stringify(aiButtonState)}`)
    })
  }
  aboutToDisappear(): void {
    this.visionImageAnalyzerController.off('aiButtonStatusChange')
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]on(type: 'imageAnalyzerVisibilityChange')

on(type: 'imageAnalyzerVisibilityChange', callback: Callback<ImageAnalyzerVisibility>): void

Listens to the visibility of the AI image recognition control.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

type

string

Yes

The value is imageAnalyzerVisibilityChange, indicating listening to the visibility of the AI image recognition control.

callback

Callback<ImageAnalyzerVisibility>

Yes

Callback function.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear(): void {
    this.visionImageAnalyzerController?.setAIButtonVisibility(true)
    this.visionImageAnalyzerController.on('imageAnalyzerVisibilityChange', (visibility: visionImageAnalyzer.ImageAnalyzerVisibility) => {
      console.info("DEMO_TAG", `imageAnalyzerVisibilityChange result: ${JSON.stringify(visibility)}`)
    })
  }
  aboutToDisappear(): void {
    this.visionImageAnalyzerController.off('imageAnalyzerVisibilityChange')
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]off(type: 'imageAnalyzerVisibilityChange')

off(type: 'imageAnalyzerVisibilityChange', callback?: Callback<ImageAnalyzerVisibility>): void

Cancels the listening to the visibility of the AI image recognition control.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

type

string

Yes

The value is imageAnalyzerVisibilityChange, indicating canceling the listening to the visibility of the AI image recognition control.

callback

Callback<ImageAnalyzerVisibility>

No

Callback to be canceled. If this parameter is not specified, all callbacks will be canceled.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear(): void {
    this.visionImageAnalyzerController?.setAIButtonVisibility(true)
    this.visionImageAnalyzerController.on('imageAnalyzerVisibilityChange', (visibility: visionImageAnalyzer.ImageAnalyzerVisibility) => {
      console.info("DEMO_TAG", `imageAnalyzerVisibilityChange result: ${JSON.stringify(visibility)}`)
    })
  }
  aboutToDisappear(): void {
    this.visionImageAnalyzerController.off('imageAnalyzerVisibilityChange')
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]on(type: 'textAnalysis')

on(type: 'textAnalysis', callback: Callback<string>): void

Listens to the text analysis result.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

type

string

Yes

The value is textAnalysis, indicating listening to the text analysis result.

callback

Callback<string>

Yes

Callback function.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear(): void {
    this.visionImageAnalyzerController.on('textAnalysis', (text: string) => {
      console.info("DEMO_TAG", `textAnalysis result: ${JSON.stringify(text)}`)
    })
  }
  aboutToDisappear(): void {
    this.visionImageAnalyzerController.off('textAnalysis')
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]off(type: 'textAnalysis')

off(type: 'textAnalysis', callback?: Callback<string>): void

Cancels the listening to the text analysis result.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

type

string

Yes

The value is textAnalysis, indicating canceling the listening to the text analysis result.

callback

Callback<string>

No

Callback to be canceled. If this parameter is not specified, all callbacks will be canceled.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear(): void {
    this.visionImageAnalyzerController.on('textAnalysis', (text: string) => {
      console.info("DEMO_TAG", `textAnalysis result: ${JSON.stringify(text)}`)
    })
  }
  aboutToDisappear(): void {
    this.visionImageAnalyzerController.off('textAnalysis')
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]on(type: 'selectedTextChange')

on(type: 'selectedTextChange', callback: Callback<string>): void

Listens to the text selection result.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

type

string

Yes

The value is selectedTextChange, indicating listening to the text selection result.

callback

Callback<string>

Yes

Callback function.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear(): void {
    this.visionImageAnalyzerController.on('selectedTextChange', (selectedText: string) => {
      console.info("DEMO_TAG", `selectedTextChange result: ${JSON.stringify(selectedText)}`)
    })
  }
  aboutToDisappear(): void {
    this.visionImageAnalyzerController.off('selectedTextChange')
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]off(type: 'selectedTextChange')

off(type: 'selectedTextChange', callback?: Callback<string>): void

Cancels the listening to the text selection result.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

type

string

Yes

The value is selectedTextChange, indicating canceling the listening to the text selection result.

callback

Callback<string>

No

Callback to be canceled. If this parameter is not specified, all callbacks will be canceled.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear(): void {
    this.visionImageAnalyzerController.on('selectedTextChange', (selectedText: string) => {
      console.info("DEMO_TAG", `selectedTextChange result: ${JSON.stringify(selectedText)}`)
    })
  }
  aboutToDisappear(): void {
    this.visionImageAnalyzerController.off('selectedTextChange')
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]on(type: 'subjectAnalysis')

on(type: 'subjectAnalysis', callback: Callback<Subject[]>): void

Listens to the subject analysis result and returns all subject information.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

type

string

Yes

The value is subjectAnalysis, indicating listening to the subject analysis result and returning all subject information.

callback

Callback<Subject[]>

Yes

Callback function.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear(): void {
    this.visionImageAnalyzerController.on('subjectAnalysis', (subjects: visionImageAnalyzer.Subject[]) => {
      console.info("DEMO_TAG", `subjectAnalysis result: ${JSON.stringify(subjects)}`)
    })
  }
  aboutToDisappear(): void {
    this.visionImageAnalyzerController.off('subjectAnalysis')
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]off(type: 'subjectAnalysis')

off(type: 'subjectAnalysis', callback?: Callback<Subject[]>): void

Cancels the listening to the subject analysis result.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

type

string

Yes

The value is subjectAnalysis, indicating canceling the listening to the subject analysis result.

callback

Callback<Subject[]>

No

Callback to be canceled. If this parameter is not specified, all callbacks will be canceled.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear(): void {
    this.visionImageAnalyzerController.on('subjectAnalysis', (subjects: visionImageAnalyzer.Subject[]) => {
      console.info("DEMO_TAG", `subjectAnalysis result: ${JSON.stringify(subjects)}`)
    })
  }
  aboutToDisappear(): void {
    this.visionImageAnalyzerController.off('subjectAnalysis')
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]on(type: 'selectedSubjectsChange')

on(type: 'selectedSubjectsChange', callback: Callback<Subject[]>): void

Listens to selected subjects.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

type

string

Yes

The value is selectedSubjectsChange, indicating listening to selected subjects.

callback

Callback<Subject[]>

Yes

Callback function.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear(): void {
    this.visionImageAnalyzerController.on('selectedSubjectsChange', (subjects: visionImageAnalyzer.Subject[]) => {
      console.info("DEMO_TAG", `selectedSubjectsChange result: ${JSON.stringify(subjects)}`)
    })
  }
  aboutToDisappear(): void {
    this.visionImageAnalyzerController.off('selectedSubjectsChange')
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]off(type: 'selectedSubjectsChange')

off(type: 'selectedSubjectsChange', callback?: Callback<Subject[]>): void

Cancels the listening to selected subjects.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

type

string

Yes

The value is selectedSubjectsChange, indicating canceling the listening to selected subjects.

callback

Callback<Subject[]>

No

Callback to be canceled. If this parameter is not specified, all callbacks will be canceled.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear(): void {
    this.visionImageAnalyzerController.on('selectedSubjectsChange', (subjects: visionImageAnalyzer.Subject[]) => {
      console.info("DEMO_TAG", `selectedSubjectsChange result: ${JSON.stringify(subjects)}`)
    })
  }
  aboutToDisappear(): void {
    this.visionImageAnalyzerController.off('selectedSubjectsChange')
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]on(type: 'analyzerFailed')

on(type: 'analyzerFailed', callback: ErrorCallback): void

Listens to abnormal scenarios of AI image recognition.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

type

string

Yes

The value is analyzerFailed, indicating listening to abnormal scenarios of AI image recognition.

callback

ErrorCallback

Yes

Callback function.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

Error Code

Error Message

401

If the input parameter is not valid parameter.

1013700002

The service is abnormal.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear(): void {
    this.visionImageAnalyzerController.on('analyzerFailed', (error: BusinessError) => {
      console.info("DEMO_TAG", `analyzerFailed result: ${JSON.stringify(error)}`)
    })
  }
  aboutToDisappear(): void {
    this.visionImageAnalyzerController.off('analyzerFailed')
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}

[h2]off(type: 'analyzerFailed')

off(type: 'analyzerFailed', callback?: ErrorCallback): void

Cancels the listening to abnormal scenarios of AI image recognition.

System capability: SystemCapability.AI.VisionImageAnalyzer

Start version: 5.0.0(12)

Parameters:

Name

Type

Mandatory

Description

type

string

Yes

The value is analyzerFailed, indicating canceling the listening to abnormal scenarios of AI image recognition.

callback

ErrorCallback

No

Callback to be canceled. If this parameter is not specified, all callbacks will be canceled.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

Error Code

Error Message

401

If the input parameter is not valid parameter.

1013700002

The service is abnormal.

Example:

import { visionImageAnalyzer } from '@kit.VisionKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
export struct ImageDemo {
  private visionImageAnalyzerController: visionImageAnalyzer.VisionImageAnalyzerController =
    new visionImageAnalyzer.VisionImageAnalyzerController()
  aboutToAppear(): void {
    this.visionImageAnalyzerController.on('analyzerFailed', (error: BusinessError) => {
      console.info("DEMO_TAG", `analyzerFailed result: ${JSON.stringify(error)}`)
    })
  }
  aboutToDisappear(): void {
    this.visionImageAnalyzerController.off('analyzerFailed')
  }
  build() {
    Stack() {
      // Set the image separately and add it to the src/main/resources/base/media directory.
      Image($r('app.media.img'), {
        types: [ImageAnalyzerType.TEXT, ImageAnalyzerType.SUBJECT, ImageAnalyzerType.OBJECT_LOOKUP],
        aiController: this.visionImageAnalyzerController
      })
        .width('100%')
        .height('100%')
        .enableAnalyzer(true)
        .objectFit(ImageFit.Contain)
    }.width('100%').height('100%')
  }
}