IONIC拍照及图片上传

详情请参考官网:https://ionicframework.com/docs/native/camera/

1.添加插件

$ ionic cordova plugin add cordova-plugin-camera
$ npm install --save @ionic-native/camera

 2.拍照和从相册获取图片只有一个参数的区别

  //拍照或者从图库获取图片
  getPictureWay(numPhotoType) {
    const options: CameraOptions = {
      quality: 50,
      saveToPhotoAlbum: false,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      allowEdit: false,
      correctOrientation: false
    }

    //判断是拍照还是从图库中获取
    if (numPhotoType === 1) {
      //为 1 才从图库获取
      options.sourceType = this.camera.PictureSourceType.PHOTOLIBRARY
    } else {
      //其它情况默认从相机获取
      options.sourceType = this.camera.PictureSourceType.CAMERA
    }

    let loading = this.loadingCtrl.create({
      content: this.translate.instant('pdfloading')
    });
    loading.present();

    this.camera.getPicture(options).then((imageData) => {
          
      //imageData为拍照或从图库选择的图片的文件流,格式为Base64


     loading.dismiss();          
    }, (err) => {
      loading.dismiss();   
    });
  }

猜你喜欢

转载自blog.csdn.net/qq_41868796/article/details/83582975