Created
January 9, 2012 23:40
-
-
Save pamelafox/1585701 to your computer and use it in GitHub Desktop.
Phonegap Mock Camera API getPicture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var navigator = window.navigator || {}; | |
navigator.camera = (function() { | |
function resizeImage(img, maxHeight, maxWidth) { | |
var ratio = maxHeight/maxWidth; | |
if (img.height/img.width > ratio){ | |
// height is the problem | |
if (img.height > maxHeight){ | |
img.width = Math.round(img.width*(maxHeight/img.height)); | |
img.height = maxHeight; | |
} | |
} else { | |
// width is the problem | |
if (img.width > maxHeight){ | |
img.height = Math.round(img.height*(maxWidth/img.width)); | |
img.width = maxWidth; | |
} | |
} | |
} | |
// Always succeeds. Returns data URL for a local static photo | |
function getPicture(onSuccess, onFail, options) { | |
// We're just going to always succeed. | |
var img = document.createElement('img'); | |
img.src = '/img/mockmeal.jpg'; | |
img.style.position = 'absolute'; | |
img.style.left = '-999em'; | |
img.onload = function() { | |
if (options.targetHeight && options.targetWidth) { | |
resizeImage(img, options.targetHeight, options.targetWidth); | |
} | |
var canvas = document.createElement("canvas"); | |
canvas.width = img.width; | |
canvas.height = img.height; | |
var ctx = canvas.getContext("2d"); | |
ctx.drawImage(img, 0, 0); | |
var dataURL = canvas.toDataURL("image/jpeg"); | |
onSuccess(dataURL.replace('data:image/jpeg;base64,', '')); | |
}; | |
document.body.appendChild(img); | |
} | |
return { | |
getPicture: getPicture | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what doy you think about to use display none instead of position?