cordova-plugin-camera插件

控制器注入$scope,$ionicActionSheet,$timeout,$ionicLoading

/*************************************************************************************************/

 $scope.choosePicMenu = function() {
                    var type = 'gallery';
                    
                    var hideSheet = $ionicActionSheet.show({
                        buttons: [
                            { text: '拍照' },
                            { text: '从相册选择' }
                        ],
                        titleText: '选择照片',
                        cancelText: '取消',
                        cancel: function() {
                        },
                        buttonClicked: function(index) {
                            if(index == 0){
                                //type = 'camera';
                                var srcType = Camera.PictureSourceType.CAMERA;
                            }else if(index == 1){
                                //type = 'gallery';
                                var srcType = Camera.PictureSourceType.SAVEDPHOTOALBUM;
                            }
                            var options = setOptions(srcType);
                            //var func = createNewFileEntry;
                            
                            options.targetHeight = 400;//图片压缩后的高
                            options.targetWidth = 400;
                            navigator.camera.getPicture(function cameraSuccess(imageUri) {
                                 console.log(imageUri);
                                 $scope.temp_image = imageUri;//取到的本地图片地址,可在页面预览
                                 //$scope.head_logo = imageUri;
                                 $scope.uoload_img();//访问API,保存图片
                                 
                                 //弹出缓冲提示
                                 $scope.showLoadingToast();
                                 //这里使用定时器是为了缓存一下加载过程,防止加载过快
                                 $timeout(function () {
                                   //停止缓冲提示
                                   $scope.hideLoadingToast();
                                 }, 1000);
                                 

                            }, function cameraError(error) {
                                console.debug("Unable to obtain picture: " + error, "app");
                                
                                 //显示等待
                                //弹出缓冲提示
                                $scope.showLoadingToast();
                                //这里使用定时器是为了缓存一下加载过程,防止加载过快
                                $timeout(function () {
                                  //停止缓冲提示
                                  $scope.hideLoadingToast();
                                }, 2500);
                            }, options);
                            
                         }
                    });
                    $timeout(function() {
                         hideSheet();
                     }, 3000);
                   
                };
                
                  $scope.showLoadingToast = function () {
                    // Setup the loader
                    $ionicLoading.show({
                      template: '请稍后...',
                      content: 'Loading',
                      animation: 'fade-in',
                      showBackdrop: true,
                      maxWidth: 200,
                      showDelay: 0
                    });
                  }
                  $scope.hideLoadingToast = function () {
                    $ionicLoading.hide();
                  }



            //请求服务器保存图片地址

            $scope.uoload_img = function(){
                    
                    $ionicLoading.show({///小蓝圈缓冲
                        template:'<ion-spinner icon="ios-small" class="spinner-assertive"></ion-spinner>',
                        duration:3000
                    });
                    
                         //新建文件上传选项,并设置文件key,name,type
                         console.log($scope.temp_image);
                         var options = new FileUploadOptions();
                         
                         options.fileKey = "upfile";
                    options.fileName = $scope.temp_image.substr($scope.temp_image.lastIndexOf('/')+1);
                         options.mimeType = "image/jpeg";
                         //用params保存其他参数,例如昵称,年龄之类
                         var params = {};
                        
                         params['userid'] = login_obj.userid;
                         params['sellerid'] = sellerid;
                         params['checkstr'] = login_obj.checkstr;
                         params['fileName'] = options.fileName;
                         //把params添加到options的params中
                         options.params = params;
 
        var ft = new FileTransfer();
       var upload_url = ****;//接口地址
                         //上传文件
                         ft.upload(
                             $scope.temp_image,
                             encodeURI(upload_url),
                             uploadSuccess,
                             uploadError,
                             options);
                         //upload成功的话
                         function uploadSuccess(r) {
                             console.log(r);
                             var resp = JSON.parse(r.response);
                           
                              //$ionicLoading.hide();
                             }
                             
                         }
                         //upload失败的话
                         function uploadError(error) {
                             $ionicLoading.show({
                                 template:"头像上传失败",
                                 duration:2000
                             });
                            
                         }
                     
                 }

/*************************************************************************************************/



//单独的方法,供camera插件调用(配置)
    function setOptions(srcType) {
    var options = {
      // Some common settings are 20, 50, and 100
      quality: 50,
      destinationType: Camera.DestinationType.FILE_URI,
      // In this app, dynamically set the picture source, Camera or photo gallery
      sourceType: srcType,
      encodingType: Camera.EncodingType.JPEG,
      mediaType: Camera.MediaType.PICTURE,
      allowEdit: true,
      correctOrientation: true  //Corrects Android orientation quirks
    }
    return options;
  }

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值