使用$ cordovaCamera获取base64字符串不起作用。总是返回URI?

时间:2015-09-03 11:14:57

标签: angularjs base64 ionic-framework firebase ngcordova

使用$cordovaCamera的以下设置,我收到Camera.DestinationType.DATA_URLCamera.PictureSourceType.CAMERACamera.EncodingType.JPEG未定义的错误,但当我使用整数值0时, 1和0我可以通过错误;但是,图像不会返回为base64字符串而是图像的URI。

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 
};

我试图将图片保存到Firebase,但我可以获得的只是图像的临时文件位置,这几乎毫无价值。

我的职能:

var getPhoto = function() {
    Camera.getPicture().then(function(imageData) {
      console.log(imageData);
      $scope.lastPhoto = "data:image/jpeg;base64," + imageData;
    }, function(err) {
      console.err(err);
    }, {
      quality : 75,
      destinationType : 0,
      sourceType : 1,
      allowEdit : true,
      encodingType: 0,
      popoverOptions: CameraPopoverOptions,
      targetWidth: 500,
      targetHeight: 500,
      saveToPhotoAlbum: false
    });
};

其中Camera是我定义的服务:

.factory('Camera', ['$q', function($q) {

  return {
    getPicture: function(options) {
        console.log('this CAMERA service is getting called')
      var q = $q.defer();

      navigator.camera.getPicture(function(result) {
        // Do any magic you need
        console.log('RESULT in SERVICE', result);
        q.resolve(result);
      }, function(err) {
        q.reject(err);
      }, options);

      return q.promise;
    }
  }
}]);

我错过了什么,我的结果会返回data:image/jpeg;base64,file://somethingSomething

如何获取所拍摄图像的base64值,以便我可以有效地将其保存到Firebase?

1 个答案:

答案 0 :(得分:0)

试试这个

.factory('CamService', ['$q', function ($q) {

    return {
        GetPicture: function (options) {
            var cameraOptions = {
                quality: 40,
                destinationType: Camera.DestinationType.DATA_URL,
                sourceType: Camera.PictureSourceType.CAMERA,
                encodingType: Camera.EncodingType.JPEG,
                correctOrientation: true,
                saveToPhotoAlbum: false,
                targetHeight: 720
            };
            var q = $q.defer();

            navigator.camera.getPicture(function (result) {
                // Do any magic you need
                q.resolve(result);
            }, function (err) {
                q.reject(err);
            }, cameraOptions);

            return q.promise;
        }
    }
}])

在您的控制器中

CamService.GetPicture().then(function (data) {
            console.log(data);
            console.log("data:image/jpeg;base64," + data);

        }, function (err) {
            console.err(err);
        });

调用服务

时,不会考虑选项