WeChat applet calls camera

To call the camera in the WeChat applet, you need to use the API of the WeChat applet. Specific steps are as follows:

  1. In the mini program's  app.json file, add the following permission configuration to obtain permission to use the camera:
"permission": {
  "scope.camera": {
    "desc": "用于拍摄照片"
  }
}
  1. In the file corresponding to the page where the camera needs to be called  .js , use  wx.createCameraContext the method to create a camera context object:
const cameraContext = wx.createCameraContext();
  1. In the file of the page  .wxml , add a  <camera> tag to display the camera picture:
<camera id="camera" mode="normal" bindtakephoto="takePhoto"></camera>
  1. In the page's  .js file, write  takePhoto the method for taking the photo:
Page({
  takePhoto: function () {
    const ctx = wx.createCameraContext();
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        console.log(res.tempImagePath);
      }
    })
  }
})
  1. Call  takePhoto the method to trigger the photo taking operation, and upon success, the temporary file path of the photo will be returned  res.tempImagePath.

Please note that the user needs to authorize access to the camera before calling it. You can  wx.authorize request user authorization using the method:

wx.authorize({
  scope: 'scope.camera',
  success: () => {
    // 用户已授权
  },
  fail: () => {
    // 用户未授权
  }
});

The above are the steps to call the camera in the WeChat applet. Using these steps, you can implement the camera function in the mini program.

Guess you like

Origin blog.csdn.net/qq_32134891/article/details/131413772