0

我玩弄文件系统API。此时我可以使用ngx.webcam 拍照,接下来我将 Base64 转换为 blob,然后使用 filesystemAPI 保存 blob:

const imageName = 'imgName';
const blobImage = this.convert(this.webcamImage.imageAsBase64, imageName);

this.windowRef.nativeWindow.navigator.webkitPersistentStorage.requestQuota(1024 * 1024, (grantedBytes) => {
      this.windowRef.nativeWindow.webkitRequestFileSystem(this.windowRef.nativeWindow.PERSISTENT, 
       grantedBytes, (fs) => {
          ((f) => {
            this.filePath(fs.root, this.path.split('/'), blobImage, f, fs);
          })(blobImage);
      });
});
filePath(rootDirEntry, folders, blobImage, f, fs) {
    // Throw out './' or '/' and move on to prevent something like '/foo/.//bar'.
    if (folders[0] === '.' || folders[0] === '') {
      folders = folders.slice(1);
    }
    rootDirEntry.getDirectory(folders[0], { create: true }, (dirEntry) => {
      // Recursively add the new subfolder (if we still have another to create).
      if (folders.length) {
        this.filePath(dirEntry, folders.slice(1), blobImage, f, fs);
      }
      if (!folders.length) {
        rootDirEntry.getFile(blobImage.name, { create: true, exclusive: true }, (fileEntry) => {
          fileEntry.fullPath = '/' + this.path + blobImage.name;
          fileEntry.createWriter((fileWriter) => {
            fileWriter.write(f); // Note: write() can take a File or Blob object.
          });
        });
      }
    });
}
convert(base64, imageName) {
    const imageBlob = this.dataURItoBlob(base64);
    const imageFile = new File([imageBlob], imageName, { type: 'image/jpeg' });
    return imageFile;
}

  dataURItoBlob(dataURI) {
    const byteString = window.atob(dataURI);
    const arrayBuffer = new ArrayBuffer(byteString.length);
    const int8Array = new Uint8Array(arrayBuffer);
    for (let i = 0; i < byteString.length; i++) {
      int8Array[i] = byteString.charCodeAt(i);
    }
    const blob = new Blob([int8Array], { type: 'image/jpeg' });
    return blob;
}

现在,如果我浏览我的目录,它会显示我的 blob(我的 blob 的名称)。

然而,我想从我的目录中取出我的 blob 并查看我已经标记的图像。我可以显示的 .txt 文件:

text() {
    this.windowRef.nativeWindow.navigator.webkitPersistentStorage.requestQuota(1024 * 1024, 
      (grantedBytes) => {
        this.windowRef.nativeWindow.webkitRequestFileSystem(this.windowRef.nativeWindow.PERSISTENT, 
         grantedBytes, (fs) => {
           this.onInitFs(fs);
        });
    });
}

onInitFs(fs) {
   fs.root.getFile('testFileSystemAPI.txt', {}, (fileEntry) => {

      // Get a File object representing the file,
      // then use FileReader to read its contents.
      fileEntry.file((file) => {
        const reader = new FileReader();

        reader.onloadend = function(e) {
          const txtArea = document.createElement('textarea');
          txtArea.value = this.result as string;
          document.body.appendChild(txtArea);
        };

        reader.readAsText(file);
      });
});

但是如果我尝试将其转移到我的问题上,它就不起作用。我的最后一次尝试:

bilderAnzeigen() {
    this.windowRef.nativeWindow.navigator.webkitPersistentStorage.requestQuota(1024 * 1024, 
      (grantedBytes) => {
         this.windowRef.nativeWindow.webkitRequestFileSystem(this.windowRef.nativeWindow.PERSISTENT, 
          grantedBytes, (fs) => {
            this.bA2(fs.root, this.path.split('/'));
      });
    });
}

bA2(rootDirEntry, folders) {
    const dirReader = rootDirEntry.createReader();
    let entries = [];

    if (folders.length - 2 && folders.length - 3) {

      // Call the reader.readEntries() until no more results are returned.
      const readEntries = () => {
        dirReader.readEntries((results) => {
          if (!results.length) {
            this.listResults(entries);
          } else {
            entries = entries.concat(this.toArray(results));
            this.bilderArray = this.bilderArray.concat(this.toArray(results));
            readEntries();
            rootDirEntry.getFile(entries[0].name, {}, (fileEntry) => {
              fileEntry.file((file) => {
                const reader = new FileReader();

                this.imgsrc = reader.result;

                reader.onload = (e) => {
                  this.imgsrc = e.target.result;
                };
              });
            });
          }
        });
      };
      readEntries(); // Start reading dirs.

    }
    // Throw out './' or '/' and move on to prevent something like '/foo/.//bar'.
    if (folders[0] === '.' || folders[0] === '') {
      folders = folders.slice(1);
    }
    rootDirEntry.getDirectory(folders[0], { create: true }, (dirEntry) => {
      // Recursively add the new subfolder (if we still have another to create).
      if (folders.length) {
        this.bA2(dirEntry, folders.slice(1));
      }
    });
  }
toArray(list) {
    return Array.prototype.slice.call(list || [], 0);
  }

  listResults(entries) {
    // Document fragments can improve performance since they're only appended
    // to the DOM once. Only one browser reflow occurs.
    const fragment = document.createDocumentFragment();

    // this.bilderArray.forEach((bild, i) => { console.log(bild.name); });

    entries.forEach((entry, i) => {
      // console.log(entry);
      const img = entry.isDirectory ? '<img src="folder-icon.gif">' :
        '<img src="file-icon.gif">';
      const li = document.createElement('li');
      li.innerHTML = [img, '<span>', entry.name, '</span>'].join('');
      fragment.appendChild(li);

      // this.getbase64(entry, i);


    });

    document.querySelector('#filelist').appendChild(fragment);
  }

我的大脑被卡住了。有谁知道,我怎样才能把我的形象出来?

4

1 回答 1

0

我找到了解决方案,

TS:

  showImg() {
    this.windowRef.nativeWindow.navigator.webkitPersistentStorage.requestQuota(1024 * 1024, (grantedBytes) => {
      this.windowRef.nativeWindow.webkitRequestFileSystem(this.windowRef.nativeWindow.PERSISTENT, grantedBytes, (fs) => {
        this.onInitFsShowImg(fs);
      });
    });
  }

  onInitFsShowImg(fs) {

    fs.root.getFile('myImg.png', {}, (fileEntry) => {
      fileEntry.file((file) => {
        const reader = new FileReader();

        reader.readAsDataURL(file);
        reader.onload = (event) => {
          this.imgURL = reader.result;
        };
      });
    });
  }

HTML:

<img [src]="imgURL">
于 2020-02-26T13:12:12.813 回答