IONIC-I无法将“图像转换为base64”

时间:2019-04-07 01:16:40

标签: ionic-framework camera screen

我使用应用相机拍摄的照片显示为黑屏。我查看了其他问题并尝试了一些答案,但无法将图片转换为base64。你能帮忙吗?

您可以从链接中看到整个代码。 https://pastebin.ubuntu.com/p/4FwYdk5fvD/



  takePicture(sourceType: PictureSourceType)
  {
    var options: CameraOptions = {
      quality: 100,
      sourceType: sourceType,
      saveToPhotoAlbum: false,
      correctOrientation: true
    };

    this.camera.getPicture(options).then(imagePath => {
      if (this.plt.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY)
      {
        this.filePath.resolveNativePath(imagePath)
          .then(filePath => {
            let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
            let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
            this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
          });
      }
      else
      {
        var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
        var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
        this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
      }
    });

  }

1 个答案:

答案 0 :(得分:0)

我有点问题。我还想要名称(虽然不是路径),并且我还想要基数64 ...所以我做了这个:

takePicture(sourceType: PictureSourceType) {
var options: CameraOptions = {
    quality: 80,
    sourceType: sourceType,
    saveToPhotoAlbum: false,
    correctOrientation: true,
    destinationType: 1,
    targetWidth: 1240,
    targetHeight: 768,
};
this.camera.getPicture(options)
.then((imageData) => {
  this.imgService.convertFilePathToBlob(imageData).subscribe(blob => {
    this.image = blob;
  });

   let correctPath = imageData; // or subStr the parts you want
   let currentName = this.image[1]; // grab the name
   let currentImage = this.image[0]; // the image itself as base64


}).catch((err) => {
  console.warn("takePicture Error: " + err);
});}

和转换功能...记得从'@ ionic-native / file / ngx'导入File,FileEntry

convertFilePathToBlob(filePath:string) {
 return from(this.file.resolveLocalFilesystemUrl(filePath)).pipe(
  mergeMap((fileEntry: FileEntry) => {
      return Observable.create(observer => {
          fileEntry.file(file => {
              const reader = new FileReader();
              reader.onloadend = () => {
                  const imgBlob = new Blob([reader.result], { type: file.type });
                  observer.next([imgBlob, file.name]); // the name can be grabbed now
                  observer.complete();
              };
              reader.readAsArrayBuffer(file);
          }, error => {
              observer.error(error);
          });
      });
  }));}

对于我来说这是可行的,但是我发现它太疯狂了,我必须做所有这一切,并且进行了一些调试和谷歌搜索……我希望它可以以某种方式为您提供帮助...:)