Send base64 string picture using Ionic


#1

Hi,

Currently I’m using cordova-camera-preview to take a picture from the phone in order to send a request to the server. I’m sending a base64 picture to the server in JSON format, but the server cannot read the picture.

I tried the server loading pictures in base64 pictures and reads them correctly, but when trying the application, it does not work.

The code related is:

getInfo(picture): Promise<CarInfo> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    console.log(picture);
    let jsonInfo = { 'img': picture };

    return this.http.post('http://api-url.com', jsonInfo, options).toPromise()
      .then(this.extractData)
      .catch(this.handleErrorPromise);
  }

Also the picture demonstrating that the base64 string works


#2

So what is the problem?
Did you look at what is sent to the server to see if it is valid?
If your backend is broken - or expects a different format, there is not much we can do…


#4

I think there is a problem with the encoding between Java and Python

if the cordova plugin parses the picture into base64 using java, first converts into utf-8 and then to base64

I will try to decode by basee64 and then decode by uft-8 to see if works

Regards


#5

im sorry… is this the correct way to get base 64 using ionic 3?
printMyImage() {
var image = new Image();
image.onload = function() {
var canvas = document.createElement(‘canvas’);
canvas.height = 100;
canvas.width = 100;
var context = canvas.getContext(‘2d’);
context.drawImage(image, 0, 0);
var imageData = canvas.toDataURL(‘image/jpeg’).replace(/^data:image/(png|jpg|jpeg);base64,/, “”); //remove mimetype
window.DatecsPrinter.printImage(
imageData, //base64
canvas.width,
canvas.height,
1,
function() {
printMyBarcode();
},
function(error) {
alert(JSON.stringify(error));
}
)
};
image.src = ‘img/some_image.jpg’;
}