jquery - Convert image into base64string in visualstudio apache cordova -
i working on vs2015 cordova app .i have problem when image gallery , corverting base64string . got base64string when dispalayed black image . here code :
function onphotourisuccess(imageuri) { var largeimage = document.getelementbyid('smallimage'); largeimage.style.display = 'block'; largeimage.src = imageuri; basestrg = encodeimageuri(imageuri); } function getphoto(source) { navigator.camera.getpicture(onphotourisuccess, onfail, { destinationtype: camera.destinationtype.native_uri, mediatype: camera.mediatype.photo, sourcetype: source }); } function encodeimageuri(imageuri) { var c = document.createelement('canvas'); var ctx = c.getcontext("2d"); var img = new image(); img.onload = function () { c.width = this.width; c.height = this.height; ctx.drawimage(img, 0, 0); }; img.src = imageuri; var dataurl = c.todataurl("image/jpeg"); return dataurl; }
any ideas
i tried specific code working, saw same end result black box.
i think there's easier way achieve goal, though. instead of passing in native_uri
destination type, there's option called data_url
can use. data_url
option provide base64 encoded string may use in image element.
here's code worked me when placed in getphoto()
function. adapted cordova camera plugin documentation:
navigator.camera.getpicture(onsuccess, onfail, { destinationtype: camera.destinationtype.data_url, mediatype: camera.mediatype.picture, sourcetype: source }); function onsuccess(imagedata) { var image: = document.getelementbyid('mypicture'); image.src = "data:image/jpeg;base64," + imagedata; } function onfail(message) { alert('failed because: ' + message); }
Comments
Post a Comment