# OCR detection

VisionKit provides OCR capabilities starting with version 2.27.0 of the base library.

# Method Definition

There are two ways to use OCR detection, one is to input a static picture for detection, the other is through the camera real-time detection.

# 1. Static Image Detection

An image is entered via the VKSession.runOCR interface . The algorithm detects the text in the image and then outputs the obtained text content via the VK Session.on interface .

Example code:

const session = wx.createVKSession({
  track: {
    OCR: { mode: 2 } // mode: 1 - 使用摄像头;2 - 手动传入图像
  },
})

// In static image detection mode, every time the runOCR interface is tuned, an updateAnchors event is triggered.
session.on('updateAnchors', anchors => {
  console.log('anchors.text', "".concat(anchors.map(anchor=>anchor.text)))
})

// You need to call start once to start
session.start(errno => {
  if (errno) {
    // 如果失败,将返回 errno
  } else {
    // 否则,返回null,表示成功
    session.runOCR({
      frameBuffer, // 图片 ArrayBuffer 数据。待检测图像的像素点数据,每四项表示一个像素点的 RGBA
      width, // 图像宽度
      height, // 图像高度
    })
  }
})

# 2. Detection via camera in real time

The algorithm detects the text in the camera in real time and outputs the text in real time through the VKSession.on interface .

Example code:

const session = wx.createVKSession({
  track: {
    OCR: { mode: 1 } // mode: 1 - 使用摄像头;2 - 手动传入图像
  },
})

// The updateAnchors event is triggered continuously (once per frame) when text is detected in the camera's real-time detection mode.
session.on('updateAnchors', anchors => {
  console.log('anchors.text',"".concat(anchors.map(anchor=>anchor.text)))
})

// The removeAnchors event is triggered when the text area leaves the camera
session.on('removeAnchors', () => {
  console.log('removeAnchors')
})

// You need to call start once to start
session.start(errno => {
  if (errno) {
    // 如果失败,将返回 errno
  } else {
    // 否则,返回null,表示成功
  }
})

# Example of application scenarios

  1. Text detection.
  2. The license plate identification.
  3. Document text identification.

# Program Examples

  1. Real-Time Imager OCR Detection Capability Using Reference
  2. Use of static image OCR capabilities