toBase64String doesn't work


#1

When I try to convert a camera picture to base64 it does nothing. When I try to set a image source it doesn’t show anything and when I try to log the string to the console i get a “undefined”.

My code:

exports.takePicture = function(){
    camera.requestPermissions();
    camera.takePicture({
        width: 95,
        height: 95,
        keepAspectRatio: true
    }).then(function(picture) {
        thumbnail.src = picture.toBase64String("jpeg");
    });
}

This doesn’t work either:

exports.takePicture = function(){
    camera.requestPermissions();
    camera.takePicture({
        width: 95,
        height: 95,
        keepAspectRatio: true
    }).then(function(picture) {
       console.log(picture.toBase64String("jpeg"));
    });
}

#2

Since it’s JPEG, you have to specify the compression quality as well (100 being highest):

picture.toBase64String("jpeg", 100)


#3

I changed it to this:

exports.takePicture = function(){
    camera.requestPermissions();
    camera.takePicture({
        width: 95,
        height: 95,
        keepAspectRatio: true
    }).then(function(picture) {
        thumbnail.src = picture.toBase64String("jpeg", 100);
        console.log(String(picture.toBase64String("jpeg", 100)));
    });
}

But this still doesn’t work, the thumbnail stays the same and it doesn’t log anything.

This does change the thumbnail so it has to do with the converting to base64:

exports.takePicture = function(){
    camera.requestPermissions();
    camera.takePicture({
        width: 95,
        height: 95,
        keepAspectRatio: true
    }).then(function(picture) {
        thumbnail.src = picture;
    });
}

#4

Is picture an instance of ImageSource or a native image? What if you log this:

console.log(picture.typeName, picture.toBase64String)

What does it say?


#5

You can try following way

 camera.takePicture()
        .then(imageAsset => {
            // Result is an image asset instance
            img.src = imageAsset;
            // convert ImageAsset to ImageSource
            fromAsset(imageAsset).then(res => {
                myImageSource = res;
                var base64 = myImageSource.toBase64String("jpeg", 100);
                console.log(base64);
            })
        }).catch(function (err) {
            console.log("Error -> " + err.message);
        });

Note:
Camera returns ImageAsset and you required ImageSource to convert Base64 String. Therefore convert ImageAsset to ImageSource and convert into base64String.

Hope this helps


I can't convert the image to Base64 on iOS
#6

Thanks I got it working, I changed it to this:

camera.takePicture()
        .then(imageAsset => {
            // convert ImageAsset to ImageSource
            imageSource.fromAsset(imageAsset).then(res => {
                myImageSource = res;
                var base64 = myImageSource.toBase64String("jpeg", 100);
                thumbnail.src = "data:image/png;base64," + base64;
                console.log(base64);
            })
        }).catch(function (err) {
            console.log("Error -> " + err.message);
        });