Skip to content

Instantly share code, notes, and snippets.

@dsifry
Created August 2, 2010 20:28
Show Gist options
  • Save dsifry/505254 to your computer and use it in GitHub Desktop.
Save dsifry/505254 to your computer and use it in GitHub Desktop.
function show_pic()
{
var viewport = document.getElementById('viewport');
viewport.style.display = "";
var backdrop = document.getElementById('backdrop');
backdrop.style.display = "none";
//
// This brings up the Camera App and gets the picture, and calls dump_pic()
// with the picture data as a Base64-encoded string
//
navigator.camera.getPicture(dump_pic, fail, { quality: 50 });
}
function dump_pic(data)
{
// push the data into a file: This should work, but it doesn't.
// navigator.file.write('/sdcard/test_img.jpg', data, fail, fail);
//
// In actuality, the picture gets sent to /sdcard/Pic.jpg, but as far
// as I can tell, this is not supported functionality cross-platform.
// document.getElementById("test_img").src = "file:///sdcard/Pic.jpg";
//
//
// This actually pushes the base64-encoded data into an HTML <img> tag with
// id=test_img.
document.getElementById("test_img").src = "data:image/jpeg;base64," + data;
//
// This pushes the actual JPG data to HTML5 localstorage.
localStorage.setItem("image01", decode64(data));
}
// This decodes base64 data
function decode64(input) {
var output = new StringMaker();
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var i = 0;
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output.append(String.fromCharCode(chr1));
if (enc3 != 64) {
output.append(String.fromCharCode(chr2));
}
if (enc4 != 64) {
output.append(String.fromCharCode(chr3));
}
}
return output.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment