16-HarmonyOS5-VisionKit-VisionImageAnalyzer-Case

2 points by zhousg 9 hours ago

Case Study of Vision Image Analyzer in VisionKit on HarmonyOS 5.0 Abstract This article introduces how to use visionImageAnalyzer from @kit.VisionKit to implement image text analysis in HarmonyOS 5.0. By creating the VisionKitVisionImageAnalyzer component, users can perform text analysis on specified images and display all the text and the selected text.

import { visionImageAnalyzer } from '@kit.VisionKit'

@Entry @Component struct VisionKitVisionImageAnalyzer { aiController = new visionImageAnalyzer.VisionImageAnalyzerController() @State text: string = '' @State selectedText: string = ''

  aboutToAppear(): void { 
    this.aiController.on('textAnalysis', (text) => { 
      this.text = text 
    }) 
    this.aiController.on('selectedTextChange', (selectedText) => { 
      this.selectedText = selectedText 
    }) 
  } 

  build() { 
    Column({ space: 15 }) { 
      Image(' `https://inews.gtimg.com/om_ls/O3W2Lv10CTyLNHOjw4k_Co1Kkb2-c42GHWvifzD-ka5OYAA_294195/0` ', { 
        types: [ImageAnalyzerType.TEXT], 
        aiController: this.aiController 
      }) 
        .enableAnalyzer(true) 
        .objectFit(ImageFit.Contain) 
        .width(300) 
        .height(300) 
      Text('All text: ' + this.text) 
      Text('Selected text: ' + this.selectedText) 
    } 
    .alignItems(HorizontalAlign.Start) 
    .padding(15) 
    .height('100%') 
    .width('100%') 
  } 
}