将 photo.uri 转换为 base64 文件

transforming photo.uri to base64 file

我无法将图像转换为 base64 字符串,我该怎么做?我尝试了 filereader、canvas 等。请帮忙。

  async snap() {
if (this.camera) {
  let photo = await this.camera.takePictureAsync();

  app.models.predict(Clarifai.GENERAL_MODEL, photo.uri)
  .then(
      function(response) {
        console.log(response);
      },
      function(err) {
        console.log(err);
      }
    );

}
}

无需测试您的代码,FileReader 就可以解决问题。由于 snap 是一个异步函数:

async snap() {
  return await this.camera.takePictureAsync();
}

this.snap().then((photo) => {
  app.models.predict(Clarifai.GENERAL_MODEL, photo.uri)
    .then(
      function(response) {
        console.log(response);
      },
      function(err) {
        console.log(err);
      }
    );

  let reader = new FileReader()
  reader.onload = (item) => {
    console.log(item.target.result)
  }
  reader.readAsDataURL(photo.uri)

})

只需添加一个标志base64: true:

 if (this.camera) {
    this.camera.takePictureAsync({
      base64: true,
    }).then(data => {
            console.log(data.base64);
             });
      }
    };