# OCR detection

VisionKit from the base library Version 2.27.0 begins to provide OCR capabilities.

# Definition of method

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 picture detection

adopt [VKSession.runOCR interface](https://developers.weixin.qq.com/miniprogram/dev/api/ai/Visionkit /VKSession.runOCR.html) Input an image, the algorithm detects the text in the image, and then passes [VKSession.on interface](https://developers.weixin.qq.com/miniprogram/dev/api/ai/Visionkit /VKSession.on.html) Output the obtained text content.

Sample code:

const session = wx.createVKSession({
  track: {
    OCR: { mode: 2 } // mode: 1 - Use the camera2 - Manual input image
  },
})

// In static image detection mode, each tone runOCR The interface will trigger once updateAnchors event
session.on('updateAnchors', anchors => {
  console.log('anchors.text', ".concat" (anchors.map(anchor=>anchor.text)))
})

// Need to call once start To initiate
session.start(errno => {
  if (errno) {
    // If it fails, it will return. errno
  } else {
    // Otherwise, return null, indicating success
    session.runOCR({
      frameBuffer, // picture ArrayBuffer Data. Pixel data of the image to be detected, each of four terms representing a RGBA
      width, // Image width
      height, // Image height
    })
  }
})

# 2. Real-time detection via camera

Algorithm to detect the text content in the camera in real time by [VKSession.on interface](https://developers.weixin.qq.com/miniprogram/dev/api/ai/Visionkit /VKSession.on.html) Text output in real time.

Sample code:

const session = wx.createVKSession({
  track: {
    OCR: { mode: 1 } // mode: 1 - Use the camera2 - Manual input image
  },
})

// When text is detected, updateAnchors Events are triggered continuously (Triggered once per frame)
session.on('updateAnchors', anchors => {
  console.log('anchors.text',"".concat(anchors.map(anchor=>anchor.text)))
})

// When the text area is removed from the camera, it triggers removeAnchors event
session.on('removeAnchors',  () => {
  console.log('removeAnchors')
})

// Need to call once start To initiate
session.start(errno => {
  if (errno) {
    // If it fails, it will return. errno
  } else {
    // Otherwise, return null, indicating success
  }
})

# Application Scenario Examples

  1. Text detection.
  2. License plate recognition.
  3. Document text recognition.

# Program Examples

  1. [Real-Time Imager OCR Detection Capability Using Reference](https://github.com/WeChat mini-program /miniprogram-demo/tree/master/miniprogram/packageAPI/pages/ar/ocr-detect)
  2. [Use of static image OCR capabilities](https://github.com/WeChat mini-program /miniprogram-demo/tree/master/miniprogram/packageAPI/pages/ar/photo-ocr-detect)