Ionic原生相机.getPicture(选项)始终返回一个字符串。

4
我正在使用@ionic-native/camera中的getPicture函数来获取图像的文件URI。我已经安装了cordova相机插件并更新了所有软件包。根据文档,默认的目标类型选项是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
    }); 
}

控制台输出:

在此输入图片描述


目前正在遇到相同的问题。 - ntgCleaner
2个回答

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)
        })


      }

1
非常感谢!我已经试图解决这个问题好几周了! - Gustavo

0

尝试这段代码:

  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 Us Saqib
谢谢你,Najam。但我没有看到我的代码和你提供的代码之间的区别。你看,在你的情况下,imageData应该提供一个URI。这就是我对我的imageURI变量的期望,然而,无论目标类型如何,它似乎总是返回一个base64字符串。 - Ghost

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接