Skip to content

Instantly share code, notes, and snippets.

@siteslave
Created January 3, 2014 04:04
Show Gist options
  • Select an option

  • Save siteslave/8232519 to your computer and use it in GitHub Desktop.

Select an option

Save siteslave/8232519 to your computer and use it in GitHub Desktop.
test.js
var pictureSource;
var destinationType;
var camera;
var app = {
init: function ()
{
document.addEventListener(
"deviceready",
this.onDeviceReady,
false
);
},
onDeviceReady: function ()
{
camera = navigator.camera;
pictureSource = camera.PictureSourceType;
destinationType = camera.DestinationType;
sourceType = camera.PictureSourceType;
encodingType = camera.EncodingType;
},
onPhotoDataSuccess: function (imageData)
{
$('#my_img').prop(
'src',
'data:image/jpeg;base64,' + imageData);
},
onPhotoURISuccess: function (imageURI)
{
$('#my_img').prop('src', imageURI);
},
capturePhoto: function ()
{
var options = {
quality: 70,
destinationType: destinationType.DATA_URL,
sourceType: sourceType.CAMERA,
encodingType: encodingType.JPEG,
};
camera.getPicture(
this.onPhotoDataSuccess,
this.onFail,
options
);
},
capturePhotoWithFile: function ()
{
var options = {
quality: 50,
destinationType: destinationType.FILE_URI,
saveToPhotoAlbum: true
};
camera.getPicture(
this.onPhotoFileSuccess,
this.onFail,
options
);
},
onPhotoFileSuccess: function (imageData)
{
$('#my_img')
.prop('src', imageData);
},
getPhoto: function (source)
{
var options = {
quality: 70,
destinationType: destinationType.FILE_URI,
sourceType: source
};
camera.getPicture(
this.onPhotoURISuccess,
this.onFail,
options
);
},
onFail: function (message)
{
alert('Failed because: ' + message);
}
};
$(function ()
{
//initial App
app.init();
$('#btn_capture').on('click',
function ()
{
app.capturePhoto();
});
$('#btn_open').on('click',
function ()
{
app.getPhoto(pictureSource.PHOTOLIBRARY);
});
$('#btn_capture_file').on('click',
function ()
{
app.capturePhotoWithFile();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment