# ID testing

VisionKit has been supported since version3.3.0of the base library.

Thecapability is used as a parallel capability interface toother VisionKit capabilities.

This capability is generally used by users for the development of functions such asidentity identificationoridentification cropping.

# Method Definition

ID detection is currently only supported throughvisual mode, i.e., input a static picture for ID identification and return the ID corresponding information.

You can configuregetAffineImg,Determines whether theBody Certification Region Clipping Matrixis returned for each identification.

# Enter ID Photo Requirements

Enter the body image, as far as possible tonormal angle shot.The perspective effect of different angles will affect the accuracy of recognition.

# Visual Mode Interface

You first need to create a configuration for VKSession , and then start the VKSession instance byVKSession.start.

Open VKSession, and add the return of the corresponding identity card information listening event, sample code:

// VKSession configuration
const session = wx.createVKSession({
    track: {
        IDCard: {
            mode: 2 // 照片模式
        }
    },
    version: 'v1',
})

// VKSession start
session.start(err => {

  // 静态图片估计模式下,每调一次 detectIDCard 接口就会触发一次 updateAnchors 事件
  session.on('updateAnchors', anchors => {
      // 处理返回的身份证信息
      if (anchors && anchors[0]) {
          // 存在数组,证明存在身份证信息
          const anchor = anchors[0];

          // 识别信息
          const isComplete = anchor.isComplete; // 身份证是否完整
          const label = anchor.label; // 身份证面信息(0 照片面 / 1 国徽面 )
          const orientation = anchor.orientation; // 身份证朝向 (0 朝上 1 朝下 2 朝下 3 朝左)
          const box = anchor.box; // 身份证坐标框点数组 (0 左上点 1 右上点 2 右下点 3 左下点)

          // 裁剪信息,接口 getAffineImg 为 true 时会返回。
          const affineImgWidth = anchor.affineImgWidth;
          const affineImgHeight = anchor.affineImgHeight;
          const affineMat = anchor.affineMat;

          // 存在裁剪信息,可以结合原图获取裁剪后的身份证图片
          if (affineImgWidth && affineImgHeight && affineMat) {
            /*
              * affineMat 3x3仿射变换矩阵,行主序
              *  [0 1 2
              *   3 4 5
              *   6 7 8]
              */
            /*
              * canvas 2d setTransform
              * setTransform(a, b, c, d, e, f)
              *  [a c e
              *   b d f
              *   0 0 1]
              */
             // 可以利用离屏的Canvas2D,结合原图与裁剪矩阵,进行具体的身份证图片裁剪。
          }
      }

  })
  // 图片没有识别到身份证,会触发一次 removeAnchors
  session.on('removeAnchors', anchors => {
      console.log("没有识别到身份证")
  })
});

Call ID identification. Example code:


// Call a specific ID photo identification interface
session.detectIDCard({
    // 识别身份证图片的 ArrayBuffer,Uint8ClampedArray,RGBA
    // 比如可以通过 canvas(2D)的 context.getImageData 获取
    frameBuffer: imgDataBuffer,
    // 传入识别图片的原始宽度
    width: imgOriginWidth,
    // 传入识别图片的原始高度
    height: imgOriginHeight,
    // 是否获取裁剪图片信息
    getAffineImg: true,
})
// After the call, recognition is completed.
// Successful identification triggers an updateAnchors callback, failure to process triggers a removeAnchors callback

# Output instructions

AnchorInformation

struct anchor
{
  isComplete,  // 身份证是否完整
  label,       // 身份证面信息(0 照片面 / 1 国徽面 )
  orientation, // 身份证朝向 (0 朝上 1 朝下 2 朝下 3 朝左)
  box,         // 身份证坐标框点数组 (0 左上点 1 右上点 2 右下点 3 左下点)
  /* 身份证裁剪信息
   * getAffineImg 为 true 时返回 */
  affineImgWidth,   // 身份证裁剪宽度
  affineImgHeight,  // 身份证裁剪区域高度
  affineMat,        // 身份证裁剪矩阵
}

# Body Certificate Coordinate Box Points Group Box

An array of 4 in length indicates that the ID card is located in the original picture and the coordinates of the box are located.

Array<Point>(8) box

Each array element is structured as:

struct Point { x, y }

# Body certificate trimming matrix affineMat

An array of length 9 representing the 3 x 3 affine transformation matrix of the main order of rows. Can be combined with Canvas (2D) and the original picture for specific body photo cutting.

# Program Examples

# Examples of ID Photo Identification

Weixin Mini Program Sampleinterface-VisionKitVisual ability-Photo identification

Open source address: photo body identification