How to take pictures and crop them using ionic framework and cordovaCamera-plugin?

纵饮孤独 提交于 2019-12-06 10:16:26

I had troubles with this plugin too and after looking up on the Internet i found out that the camera plugin property

allowEdit: true

Does not work properly, not returning the cropped image in the promise, but the original (hence not modified) image, check this forum thread

I solved this problem using this plugin alongside with the cordova camera plugin, a pretty neat soluton!

you have to use $cordovaCamera.getPicture(options) to work for taking a picture or from a library.Here am posting a sample code for camera which worked for me In your controller write the camera code as

$scope.userPic = function(){
            console.log("userPic function got called");
            $ionicPopup.show({
              template: '<p>Take picture or use from library</p>',
              title: 'Choose',
              buttons: [
                {
                  text: '<b>Camera</b>',
                  onTap: function(e) {
                    return "camera";
                  }
                },
                {
                  text: '<b>Library</b>',
                  type: 'button-positive',
                  onTap: function(e) {
                    return "library";
                  }
                },
              ]
            }).then(function(resp) {
              $scope.takePicture(resp);
              console.log('resp', resp);
            });
        }

        $scope.takePicture = function(resp){
            console.log("takePicture function got called");
            console.log(resp);
            var source;
            if (resp == "camera") {
              source = Camera.PictureSourceType.CAMERA;
            }else{
              source = Camera.PictureSourceType.PHOTOLIBRARY;
            };
            var options = {
              quality : 75,
              destinationType : Camera.DestinationType.FILE_URI,
              sourceType : source,
              allowEdit : true,
              encodingType: Camera.EncodingType.JPEG,
              targetWidth: 300,
              targetHeight: 300,
              popoverOptions: CameraPopoverOptions,
              saveToPhotoAlbum: false
            };
             $cordovaCamera.getPicture(options).then(function(imageData) {
                console.log(imageData);
            }, function(err) {
                console.log(err);
                // error
            });
             }


In your HTML write the camera button code as

<button class="button button-bar button-balanced" ng-click="userPic()">
      Camera
    </button>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!