How can I convert the URL of a base64 image (IONIC 2)

3
this.camera.getPicture({
  sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
  destinationType: this.camera.DestinationType.FILE_URI,
  quality: 100,
  encodingType: this.camera.EncodingType.PNG,
}).then(imageData => {
  console.log('ESTA ES LA URI '+imageData);
  //como obtengo la imagen y la convierto en base64 ?
}, error => {
  this.error = JSON.stringify(error);
});
    
asked by Antonio Sierra Hurtado 10.08.2017 в 19:13
source

3 answers

2

In the Ionic Framework Docs ( cordova-plugin-camera ):

this.camera.getPicture(options).then((imageData) => {
 // imageData is either a base64 encoded string or a file URI
 // If it's base64:
 let base64Image = 'data:image/jpeg;base64,' + imageData;
}, (err) => {
 // Handle error
});

So, to create a base64 image:

  • You insert the string 'data:image/jpeg;base64,' + the imageData
  • let base64Image = 'data:image/jpeg;base64,' + imageData;

    Solution:

    console.log('data:image/png;base64,' + imageData);
    

    Edit : - You have to monitor the coding of the image:

    In your case: encodingType: this.camera.EncodingType.PNG Therefore:

    data:image/png;base64,
    

    General:

    data:image/[tipo de imagen];base64,
    

    Where:

    [image type] = jpeg, png, gif ...

        
    answered by 10.08.2017 в 21:35
    0

    You can use the file plugin link

    And use the method this.file.readAsBinaryString (path, file);

    this method returns a base 64

        
    answered by 23.09.2017 в 18:18
    -1
    this.camera.getPicture({
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
      destinationType: this.camera.DestinationType.DATA_URL, //convertir en base64
      quality: 100,
    encodingType: this.camera.EncodingType.PNG,
    }).then(imageData => {
      let image='data:image/png;base64,'+imageData;
      //sacando image en un img ya se mostrará 
    }, error => {
      this.error = JSON.stringify(error);
    });
    
        
    answered by 15.11.2017 в 11:50