преобразование photo.uri в файл base64 - PullRequest
0 голосов
/ 10 ноября 2019

Я не могу преобразовать изображение в строку base64, как мне это сделать? Я пробовал файл-ридер, канву и т. Д. Пожалуйста, помогите.

  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 голосов
/ 12 ноября 2019

Просто добавьте флаг base64: true:

 if (this.camera) {
    this.camera.takePictureAsync({
      base64: true,
    }).then(data => {
            console.log(data.base64);
             });
      }
    };
0 голосов
/ 10 ноября 2019

Без тестирования вашего кода, независимо от того, что FileReader должен сделать. Поскольку привязка является асинхронной функцией:

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)

})
...