$cordovaCamera.getPicture与navigator.camera.getPicture

本文介绍了使用Cordova Camera插件获取和处理图片的具体方法,包括设置参数、处理图片数据等,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


作者:LoveEmperor_王子様


多看API,多做实验;

本人最后还是用的$cordovaCamera.getPicture方法;界面显示图片更流畅,

$cordovaCamera.getPicture方法;

var options = {
  quality: 80,
  destinationType: Camera.DestinationType.DATA_URL,
  sourceType: Camera.PictureSourceType.CAMERA,
  encodingType: Camera.EncodingType.JPEG,
  targetWidth: 600,
  targetHeight: 600,
  popoverOptions: CameraPopoverOptions,
  saveToPhotoAlbum: false,
  correctOrientation:true
};

$cordovaCamera.getPicture(options).then(function(imageData) {

    $scope.img = "data:image/jpeg;base64," +  imageData;
    //alert(imageData);
    $scope.images_list.push($scope.img);
  $scope.allFile.push(imageData);

}, function(err) {
  // error
});

navigator.camera.getPicture方法;

function onDeviceReady() {        pictureSource=navigator.camera.PictureSourceType;      destinationType=navigator.camera.DestinationType;    }

   function onPhotoDataSuccess(imageData) {
     var smallImage = document.getElementById('smallImage');
     $scope.listShow = 1;
     //$scope.img = "data:image/jpeg;base64," +imageData;
     $scope.img = imageData;
     alert(imageData);
     $scope.images_list.push($scope.img);
     $scope.allFile.push(imageData);


       window.plugins.Base64.encodeFile(imageData, function (base64) {
         //$scope.myFileImage22   =(base64.slice(34));
         $scope.img = "data:image/jpeg;base64," +  (base64.slice(34));
         $scope.images_list.push($scope.img);
       
       });

     lrz(imageData)
       .then(function (rst) {
         $scope.myFileImage2 = (rst.base64.slice(23));
         $scope.allFile.push($scope.myFileImage2);
         //$scope.img = "data:image/jpeg;base64," +$scope.myFileImage2;
         //$scope.images_list.push($scope.img);
       })
       .catch(function (err){
       })
       .always(function () {
       });

   }

   function onPhotoURISuccess(imageURI) {
     var largeImage = document.getElementById('largeImage');
     largeImage.style.display = 'block';
     largeImage.src =imageURI;
   
   }

   var  self = this;

   $scope. capturePhotoEdit111 = function() {
     navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,       destinationType: destinationType.DATA_URL });    };
   $scope. capturePhotoEdit =function() {
     navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 40,    saveToPhotoAlbum: false ,     destinationType: destinationType.FILE_URI    });
     $scope.imageBorder = 1;
     $scope.clock = new Date();
     var updateClock = function() {
       $scope.clock = new Date();
     };
     //setInterval(function() {
     //  $scope.$apply(updateClock);
     //}, 1000);
     updateClock();
     var toTime = new Date($scope.clock.toString());
     $scope.dt2 = $filter("date")(toTime, "yyyyMMddHHmmss");
     $scope.cameraImageNum = 1;

   };
    $scope.getPhoto=function(source) {
     navigator.camera.getPicture(onPhotoURISuccess, onFail, { maximumImagesCount: 9, quality: 50,
       destinationType: destinationType.FILE_URI,
       sourceType:
         navigator.camera.PictureSourceType.PHOTOLIBRARY });
   };

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