Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap Cordova 3.0.0 navigator.camera is undefined

I'm trying to use the Cordova native plugins for the first time. I started out with the camera and the sample code provided in the documentation. This is failing however and the navigator.camera is undefined.

I've included the code below.

<div data-role="page" id="CameraPage">
<script type="text/javascript" charset="utf-8">

var pictureSource;   // picture source
var destinationType; // sets the format of returned value

// Wait for device API libraries to load
//
document.addEventListener("deviceready",onDeviceReady,false);

// device APIs are available
//
function onDeviceReady() {
    pictureSource=navigator.camera.PictureSourceType;
    destinationType=navigator.camera.DestinationType;
}

// Called when a photo is successfully retrieved
//
function onPhotoDataSuccess(imageData) {
  alert("Photo Data Success");
  // Uncomment to view the base64-encoded image data
  // console.log(imageData);

  // Get image handle
  //
  var smallImage = document.getElementById('smallImage');

  // Unhide image elements
  //
  smallImage.style.display = 'block';

  // Show the captured photo
  // The inline CSS rules are used to resize the image
  //
  smallImage.src = "data:image/jpeg;base64," + imageData;
}

// A button will call this function
//
function capturePhoto() {
  alert("function is called"); 
  if(_.isUndefined(navigator.camera)){
    alert("Camera is not defined");
  }else{
    alert("WTF?!");
  }
  // Take picture using device camera and retrieve image as base64-encoded string
  navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
    destinationType: destinationType.DATA_URL });
}

// Called if something bad happens.
//
function onFail(message) {
  alert('Failed because: ' + message);
}

</script>

<button onclick="capturePhoto();">Capture Photo</button> <br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />

I installed the camera plugin according to the CLI directions

cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git

I also added the cordova.js files.

<script type="text/javascript" charset="utf-8" src="js/cordova-js/lib/cordova.js"></script>
like image 539
bigtunacan Avatar asked Sep 09 '13 01:09

bigtunacan


1 Answers

Where are you testing your code?

I dont know if you still need the anwser. But I think it can help someone. I run your code and it works 100%. I have tested in Android's emulator with a emulated Camera. I think that you forgot the uses permissions.

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

I made an example here https://github.com/paulorbpacheco/CameraTest-Cordova.git

I hope this can help you...

Regards

like image 117
Paulo Pacheco Avatar answered Nov 09 '22 02:11

Paulo Pacheco