离子本机camera.getPicture(options)始终返回字符串

时间:2019-01-02 20:15:03

标签: ionic-framework ionic3 cordova-plugins ionic-native

我正在使用@ ionic-native / camera的getPicture函数来获取图像的文件URI。我有cordova相机插件,并且所有软件包都已更新。根据{{​​3}},默认目标类型选项为File_URI。但是,即使我使用默认目标类型显式指定我的选项作为File_URI,它也会返回base64字符串。

下面给出了源代码,我缺少什么吗?任何帮助表示赞赏。

import { Camera, CameraOptions } from '@ionic-native/camera';

    openGallery(){
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.FILE_URI,
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
    }

    this.camera.getPicture(options).then((imageURI) => {
     // imageData is either a base64 encoded string or a file URI
     // If it's base64 (DATA_URL):

     this.image_loc = imageURI;

     console.log("The image location is as follows: " + this.image_loc);

    }, (err) => {
     // Handle error
    }); 
}

控制台中的输出:

documentation

2 个答案:

答案 0 :(得分:1)

尝试此代码:

 Widget _buildItem(BuildContext context, int index) {
    final remind = reminders[index];

    return CheckboxListTile(
      value: remind.isDone,
      title: Text(remind.title + "-" + remind.message),
      secondary: const Icon(Icons.edit),
      controlAffinity: ListTileControlAffinity.leading,
      onChanged: (bool isChecked) {
        onRemindToggle(remind, isChecked);
      },
    );
  }

答案 1 :(得分:1)

尝试

 base64Image:any;

      optionsCamera: CameraOptions = {
        quality: 100,
        destinationType: this.camera.DestinationType.FILE_URI,
        encodingType: this.camera.EncodingType.PNG,
        cameraDirection: this.camera.Direction.BACK,
        targetHeight:400,
        targetWidth: 400,
        correctOrientation: true,
        allowEdit: true

      }


    this.camera.getPicture(options).then((imageData) => {

          this.base64Image = imageData; 
          this.convertToUrl(imageData);

        }, (err) => {
          // console.log(err);

        });


    convertToUrl(newImage){
        function toDataURL(ctx, callback) {
          var url = ctx.base64Image;

          var xhr = new XMLHttpRequest();
          xhr.onload = function() {
            var reader = new FileReader();
            reader.onloadend = function() {
              ctx.base64Image = reader.result;
              callback(reader.result);       
            }
            reader.readAsDataURL(xhr.response);

          };
          xhr.open('GET', url);
          xhr.responseType = 'blob';
          xhr.send();
        }

        toDataURL(this, function(dataUrl) {
         console.log(dataUrl)
        })


      }