[SOLVED] Camera is not defined when using $cordovaCamera

We are using ngCordova and I have $cordovaCamera injected into my method. I get errors when using the Camera object in my camera options:

    // select an existing picture
    $scope.selectPicture = function($cordovaCamera) {
            var options = { 
                    //quality : 75, 
                    destinationType : Camera.DestinationType.DATA_URL, 
                    sourceType : Camera.PictureSourceType.PHOTOLIBRARY, 
                    allowEdit : true,
                    encodingType: Camera.EncodingType.JPEG,
                    //targetWidth: 100,
                    //targetHeight: 100,
                    popoverOptions: CameraPopoverOptions,
                            saveToPhotoAlbum: false
            };
        $cordovaCamera.getPicture(options).then(function(imageData) {
                    // success image data is here
                    }, function(err) {
                            // An error occured. Show a message to the user
            });
    };

The error Im seeing is:

ReferenceError: Camera is not defined
at Scope.$scope.takePicture (file:///android_asset/www/js/controllers.js:272:22)
at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:18471:21
at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:43026:9
at Scope.$eval (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:20326:28)
at Scope.$apply (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:20424:23)
at HTMLAnchorElement. (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:43025:13)
at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:10478:10
at forEach (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:7950:18)
at HTMLAnchorElement.eventHandler (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:10477:5)
at triggerMouseEvent (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:2648:7)

Line 272 is the first line of options in the code fragment up top.

Hi @eno,

Make sure you try to access the camera only after the $ionicPlatform.ready() or onDeviceReady() has triggered. Many plugins are not loaded instantly which believe youā€™re suffering from.

Let me know if this works.

Regards,

The problem was that the actual plugin never made it into the actual Android build even though it had been installed and existed in my plugins folder. Furthermore, telling cordova to install again doesnā€™t work. I had to remove my platforms, remove the plugin, reinstall the plugin, re-add my platform (I have a hooks script that fires after platform add that installs all the plugins we use). Only after all, my Android build actually worked.

2 Likes

I had a similar problem with code similar to yours. The problem was solved only after I updated my phone. I was disappointed for not getting the method getPicture() working and I give up on trying to fix this bug for like 1 week. Then, my cellphone (android) was updated and right after the update process I checked the app and it was working.

I have to mention that I almost throw my cellphone away --ā€™

Iā€™m still having a problem with wp8.1, firtst i got this error to,but now the camera shows up when i tab to make a picture i hear a clicking sound like the picture is taken but after that nothing happens and the only thing i can do is cancel the camera :confounded:

Rikkie,

I want to make sure that that you tried this. Iā€™ve just faced the 'Camera is not defined ā€™ again:

ā€œrefreshā€ the camera plugin by typing (android/ios - up to you)

cordova plugin rm cordova-plugin-camera
ionic platform rm android
ionic platform add android
cordova plugin add cordova-plugin-camera

I am running the app in a real device, I am using chrome console to debug and type ā€˜window.cordovaā€™ or even ā€˜Cameraā€™ on chromeā€™s console and the result was undefined which means that cordova wasnā€™t loaded correctly.

Then Iā€™ve noticed that in my index.html I removed the call to:

<script src="cordova.js"></script> 

Iā€™ve just added the piece of code above and now my code is like this:

<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>
<script src="js/app.js"></script>

Things are working properly.

Please let us know if you have succeded!

1 Like

Iā€™ve tried to do what was advices, and this didnā€™t help me at all. When testing in a browser, Iā€™m still getting the error: ā€œReferenceError: Camera is not definedā€, and when testing on a device (iOS) the camera opens, takes a photo and doesnā€™t add it to the App nor Firebase. I also donā€™t know how to debug it from that point onwards. So I canā€™t tell weather the problem is with Cordova or with the rest of my code that interfaces with Firebase. Any advice on how to debug?

Iā€™ve found same problem. Iā€™ve solved by changing Camera object in the ā€œoptionsā€ definition by $cordovaCamera.

However, I canā€™t take a picture on my device. Iā€™m using the creator app. Do you know if it should work from there?

    var op    tions = {
          quality: 50,
          destinationType: $cordovaCamera.DestinationType.DATA_URL,
          sourceType: $cordovaCamera.PictureSourceType.CAMERA,
          allowEdit: true,
          encodingType: $cordovaCamera.EncodingType.JPEG,
          targetWidth: 100,
          targetHeight: 100,
          popoverOptions: $cordovaCamera.PopoverArrowDirection.ARROW_UP,
          saveToPhotoAlbum: false,
    	  correctOrientation:true
        };

Hi! Iā€™ve same problem. Browserā€™s console show me same error and i thought this was due by service, it mean, i create a service that contain ā€œCameraā€ (Here code):
.service(ā€˜Cameraā€™, function($q) {

return {
getPicture: function(options) {
var q = $q.defer();

     $cordovaCamera.getPicture(function(result) {
        q.resolve(result);
     }, function(err) {
        q.reject(err);
     }, options);

     return q.promise;

But now show another error ā€œError: Camera.DestinationType is undefinedā€. I need help with that :grin:

Hey,

I had similar problem. However i solved problem with following this solution.

plugins - Camera.getPicture is not a function in ionic 3 - Stack Overflow