Javascript - Cordova - Android - Return base64 encoded canvas drawn with local image file -
I filesystem, I write a new HTML file to a string using using Cordova 2.4.0 Need for . Because I just as a HTML) require that HTML, contains some images loaded from a local folder I change the images encode and (background images without a file (png / jpg images with this implement them through CSS I am trying to address the same string, the future is embedded in the HTML file) well, the way I do these modifications string I am using that function returns. For example: Function "agregaImagenLocal (pathToLocalImage)": I was doing this without the "download" incident, so it returned an empty image. But with this function inside "Onload" I do not know how to return the encoded image to the task before base64.
padding-top: 5%; Background Image: URL ('+ agregaImagenLocal (' ../ img / ESTELAR.png ') +'); Background color: white;
function agregaImagenLocal (pathToLocalFile) {var canvas = document.getElementById ( 'canvasOculto'); Var spec = new image (150,100); Canvas.wind = imagine Wide; Canvas Het = imagination height; Var contextoCanvas = canvas.getContext ('2d'); Imagen.onload = function () {contextoCanvas.drawImage (spec, 0, 0); UrlImagenLocal = canvas.toDataURL (); } Imagen.src = pathToLocalFile; Return urlImagenLocal // ??????? }
This function (file path) to Base64 to change an image:
function convertImgToBase64 (url, callback, outputFormat) {var canvas = document.createElement ( 'canvas'); Var ctx = canvas.getContext ('2d'); Var img = new image; Img.crossOrigin = 'Anonymous'; Img.onload = function () {canvas.height = img.height; Canvas.width = img.width; Ctx.drawImage (img, 0,0); Var dataURL = canvas.toDataURL (Output format || 'image / PNG'); Callback.call (this, dataURL); // Clear Canvas = Faucet; }; Img.src = url; } After conversion, you can use jQuery to set background CSS using Base64 code.
var imageUrl = ""; // Your local image path here Convert imagebase 64 (image URL, function (base 64 error) {$ ('output'). CSS ("background-image", base 64 IMG);} This will be set Source of an element with class 'output' on base 64 image.
Comments
Post a Comment