将photo.uri转换为base64文件

时间:2019-11-10 05:45:45

标签: javascript react-native base64 expo

我无法将图像转换为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);
      }
    );

}
}

2 个答案:

答案 0 :(得分:0)

无需测试您的代码,无论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)

})

答案 1 :(得分:0)

只需添加一个标记base64: true

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