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:
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 :
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
Post a Comment