Reputation: 319
so I'm still pretty new to cordova and right now, I'm using it to make an app and I'm trying to take a picture and then save it. Right now, I can't take a picture. All I have is a button and in the corresponding javascript file, a function is called when the button is pressed. I started off by running 'cordova plugin add org.apache.cordova.camera'. This is what I tried:
document.addEventListener('deviceready', onDeviceReady);
function onDeviceReady() {
$('#picture').on('click',function() {
takePicture();
}
function takePicture() {
if (!navigator.camera) {
alert("Camera API not supported", "Error");
return;
}
navigator.camera.getPicture(function(imageURI) {
alert("test");
}, function(err) {
alert("Inside err");
}, cameraOption);
/*navigator.camera.getPicture(function(imagePath) {
alert("test");
document.getElementByID("photoImg").setAttribute("src", imagePath);
alert("Got picture");
}, function() {
alert("Photo canceled");
}, {
destinationType: navigator.camera.DestinatonType.FILE_URI
});*/
}
}
I got the function from http://learn.ionicframework.com/formulas/cordova-camera/ and I also tried the second from another place, but the camera doesn't come up in either one. Also, I inserted some alerts inside and none of them appear, so I'm a bit confused.
Does anyone have any suggestions how I can get the camera to show up?
Upvotes: 0
Views: 2195
Reputation: 7244
First make sure that you are calling your code inside the document ready callback, so:
$(document).ready(function () {
$('#picture').on('click',function() {
takePicture();
}
});
You are not showing your camera options, here is some code that works for me.
function onSuccess(imageURI) {
// This will show the photo in an <img> element with id="photo"
jQuery('#photo').attr('src', imageURI).show();
// Do more stuff here
}
var folder = true; // set to false to take with camera, true to select from library
navigator.camera.getPicture(onSuccess, onFail, {
quality: 25, // 25%
// takes a photo that will fit precisely within an iPhone 6 Plus screen
targetWidth: 1080,
targetHeight: 960,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: folder ? Camera.PictureSourceType.PHOTOLIBRARY : Camera.PictureSourceType.CAMERA
});
Upvotes: 1