VisionKit provides OCR capabilities starting from Base Library version 2.27.0.
Method Definition
OCR detection has two usage methods: one is to input a static image for detection, and the other is to perform real-time detection through the camera.
1. Static Image Detection
Input an image via the VKSession.runOCR interface. The algorithm detects text within the image, and the recognized text content is then output via the VKSession.on interface.
Example code:
const session = wx.createVKSession({
track: {
OCR: { mode: 2 } // mode: 1 - use camera; 2 - manually input image
},
})
// In static image detection mode, calling the runOCR interface once triggers the updateAnchors event once
session.on('updateAnchors', anchors => {
console.log('anchors.text', "".concat(anchors.map(anchor=>anchor.text)))
})
// start needs to be called once to initiate
session.start(errno => {
if (errno) {
// If failed, errno will be returned
} else {
// Otherwise, null is returned, indicating success
session.runOCR({
frameBuffer, // Image ArrayBuffer data. Pixel data of the image to be detected; every four items represent the RGBA of one pixel
width, // Image width
height, // Image height
})
}
})
2. Real-time Detection via Camera
The algorithm detects text content in the camera feed in real time and outputs the text in real time via the VKSession.on interface.
Example code:
const session = wx.createVKSession({
track: {
OCR: { mode: 1 } // mode: 1 - use camera; 2 - manually pass image
},
})
// In camera real-time detection mode, when text is detected, the updateAnchors event is triggered continuously (triggered once per frame)
session.on('updateAnchors', anchors => {
console.log('anchors.text',"".concat(anchors.map(anchor=>anchor.text)))
})
// When the text area leaves the camera frame, the removeAnchors event is triggered
session.on('removeAnchors', () => {
console.log('removeAnchors')
})
// Call start once to initiate
session.start(errno => {
if (errno) {
// If failed, errno is returned
} else {
// Otherwise, null is returned, indicating success
}
})
Application Scenarios
- Text Detection.
- License Plate Recognition.
- ID Document Text Recognition.