【问题标题】:Ionic native camera.getPicture(options) always returns a string离子原生 camera.getPicture(options) 总是返回一个字符串
【发布时间】:2019-05-29 11:16:31
【问题描述】:

我正在使用来自@ionic-native/camera 的getPicture 函数来获取图像的文件URI。我有科尔多瓦相机插件,所有的包都更新了。根据documentation,默认目标类型选项是 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
    }); 
}

控制台输出:

【问题讨论】:

  • 现在遇到同样的问题

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


【解决方案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)
        })


      }

【讨论】:

  • 非常感谢!几周来我一直在努力解决这个问题!
【解决方案2】:

试试这个代码:

  const options: CameraOptions = {
          quality: 80,
          destinationType: this.camera.DestinationType.FILE_URI,
          encodingType: this.camera.EncodingType.JPEG,
          mediaType: this.camera.MediaType.PICTURE,
          sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
        }
 this.camera.getPicture(options).then((imageData) => {
         // imageData is either a base64 encoded string or a file URI
         // If it's base64 (DATA_URL):
         console.log(imageData);
        }, (err) => {
         // Handle error
        });

【讨论】:

  • 您可以根据需要将PHOTOLIBRARY更改为CAMERA
  • 谢谢 Najam,但我看不出我的代码与您提供的代码有什么区别。你看,在你的情况下 imageData 应该提供一个 URI。这是我对 imageURI 变量的期望,但是,无论目标类型是什么,它似乎总是返回一个 base64 字符串。
猜你喜欢
  • 2018-11-18
  • 1970-01-01
  • 2022-01-22
  • 2013-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-16
相关资源
最近更新 更多