android - base 64 png string to base 64 jpg using java or javascript -


i'm working on project developed in android phonegap , need draw items on screen , turn data pdf.

to draw using html5 canvas element.

to write pdf using library "jspdf."

the problem that, on android, method canvas.todataurl ('image / jpeg') returns string of "image/png" type jspdf library reads images in base64-jpg format.

i thought of 2 solutions:

1) use sort of "javascript encoder", found on internet, not find active link, transform canvas in base64-jpg format string.

2) create plugin "translate" base64-png string base64-jpg format.

so....is there way in javascript or java make "translation"? or know way realize have explained?

try one:

http://web.archive.org/web/20120830003356/http://www.bytestrom.eu/blog/2009/1120a_jpeg_encoder_for_javascript

after download jpegencoder insert code:

 var encoder = new jpegencoder();  var imagedata = encoder.encode(canvas.getcontext('2d').getimagedata(0, 0, canvas.width, canvas.height), 100); 

or

try if have problem backgroud color :

http://www.mikechambers.com/blog/2011/01/31/setting-the-background-color-when-generating-images-from-canvas-todataurl/?utm_source=feedburner&utm_medium=feed&utm_campaign=feed%3a+mikechambers+%28mike+chambers%29

function canvastoimage(canvas, backgroundcolor) {     //cache height , width             var w = canvas.width;     var h = canvas.height;     var context = canvas.getcontext('2d');     var data;             if(backgroundcolor)     {         //get current imagedata canvas.         data = context.getimagedata(0, 0, w, h);          //store current globalcompositeoperation         var compositeoperation = context.globalcompositeoperation;          //set draw behind current content         context.globalcompositeoperation = "destination-over";          //set background color         context.fillstyle = backgroundcolor;          //draw background / rect on entire canvas         context.fillrect(0,0,w,h);     }      //get image data canvas     var imagedata = canvas.todataurl("image/png");      if(backgroundcolor)     {         //clear canvas         context.clearrect (0,0,w,h);          //restore original / cached imagedata         context.putimagedata(data, 0,0);                  //reset globalcompositeoperation         context.globalcompositeoperation = compositeoperation;     }      //return base64 encoded data url string     return imagedata; } 
  • backgroundcolor parameter : 'rgba(255,255,255,0.5)'

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -