How to convert img src to encode base64 binary?


#1

I am converting img src to encode base64 bit string. But it is coming with some addition: “data:image/jpeg;base64,iVBORw…”. I don’t want this extra added string. what should I do?

I am using below method.

convertToDataURLviaCanvas(url, outputFormat) {
return new Promise((resolve, reject) => {
let img = new Image();
img.crossOrigin = ‘Anonymous’;
img.onload = () => {
let canvas = document.createElement(‘CANVAS’),
ctx = canvas.getContext(‘2d’),
dataURL;
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
resolve(dataURL);
canvas = null;
};
img.src = url;
});
}
calling via this:-
this.convertToDataURLviaCanvas(this.pathForImage(this.lastImage), “image/jpeg”).then(base64 => {
alert(base64);
});


#2

I found another way to convert image in base64 bit which is quite easy.

Just make small change in cameraOption:
destinationType: this.camera.DestinationType.DATA_URL

Thanks