在Cordova中使用带有离子和角度的相机

时间:2015-01-29 18:31:11

标签: angularjs cordova ionic

抱歉我的英文。

        chill.controller('dialogsController', function($scope, $http, $rootScope, $location, $ionicPopup, $timeout) {

        $http.get('http://site/api/v1/contacts/index/id_user/'+$rootScope.userData.id_user)
        .success(function(data)
        {
            $scope.dialogs = data;
        });

        $scope.sentPhoto = function(id_contact) {
            navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
                destinationType: Camera.DestinationType.DATA_URL
            });

            function onSuccess(imageData) {
                var image = document.getElementById('myImage');
                image.src = imageData;
                alert('Success');
            }

            function onFail(message) {
                alert('Failed because: ' + message);
            }
        };
    });

的index.html

<!-- ionic/angularjs js -->
<script src="template/lib/ionic/js/ionic.bundle.js"></script>
<script src="template/js/ng-cordova.min.js"></script>
<script src="template/js/google-maps.js"></script>
<script src="template/js/ng-map.min.js"></script>

<!-- your app's js -->
<script src="template/js/app.js"></script>

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

相机启动。拍摄静态照片。然后什么都没发生onSuccess或onSuccess不起作用。

请帮帮我。

2 个答案:

答案 0 :(得分:0)

由于您在onSuccess中定义了onFail$scope.sentPhoto,因此无法在回调中访问它们。

试试这个:

    $scope.sentPhoto = function(id_contact) {
        navigator.camera.getPicture($scope.onSuccess, $scope.onFail, { quality: 50,
            destinationType: Camera.DestinationType.DATA_URL
        });
    };

    $scope.onSuccess(imageData) {
        var image = document.getElementById('myImage');
        image.src = imageData;
        alert('Success');
    }

    $scope.onFail(message) {
        alert('Failed because: ' + message);
    }

或者(而不是干净)你可以将这些内联作为匿名函数放置:

    $scope.sentPhoto = function(id_contact) {
        navigator.camera.getPicture(
            function(imageData) {
                var image = document.getElementById('myImage');
                image.src = imageData;
                alert('Success');
            },
            function(message) {
                alert('Failed because: ' + message);
            }, 
            { quality: 50,
            destinationType: Camera.DestinationType.DATA_URL
        });
    };

答案 1 :(得分:0)

使用成功&amp; 错误功能如下,

 $scope.sentPhoto  = function() {
    navigator.camera.getPicture(function(imageData)
    {
       image.src = imageData;
       alert('Success');      

    }, function(err) {
       alert("Error"+err);
    }, {quality: 50,
      destinationType: Camera.DestinationType.FILE_URI,
      encodingType: Camera.EncodingType.JPEG,
      targetWidth: 300,
      targetHeight: 300});
 }