Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use camera for both native and web app with Ionic/AngularJS and Cordova

I'm trying to use the Camera, and I would like to know if you have any exemple on how to make it work on both web / native.

I have this piece of code, borrowed from the ng-cordova doc :

    $scope.takePicture = function() {
        var options = {
            quality: 75,
            destinationType: Camera.DestinationType.DATA_URL,
            sourceType: Camera.PictureSourceType.CAMERA,
            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
        });
    }

When I use it, it works well with my device, but catch an error with the web version

ReferenceError: Camera is not defined

That's why I ask if you have any good way to do that. I could simulate a click on an hidden input, but it doesn't look pretty. If you have any idea :)

like image 804
Kai23 Avatar asked Jun 25 '14 15:06

Kai23


1 Answers

You could provide an implementation for the camera access for both Cordova and standard browser. The standard browser implementation could be realized using this

http://www.html5rocks.com/en/tutorials/getusermedia/intro/

W3 draft

and to check, whether you're running in an app or in browser, you could use

ionic.Platform.platform()

documented here

like image 118
Clawish Avatar answered Nov 15 '22 13:11

Clawish