【问题标题】:Get Base64 from imageURI with PhoneGap使用 PhoneGap 从 imageURI 获取 Base64
【发布时间】:2012-03-23 21:52:39
【问题描述】:

我正在尝试从手机相册中选取的图像中获取 base64,但我无法使其工作:

我试过了:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
    console.log("0");
    fileSystem.root.getFile(imageURI, null, function(fileEntry) {
        console.log("1");
        fileEntry.file(function(file) {
            console.log("2");
            var reader = new FileReader();
            reader.onloadend = function(evt) {
                console.log("Read complete!");
                image64.value = Base64.encode(evt.target.result);
            };
            reader.readAsText(file);
        }, failFile);
    }, failFile);
}, failSystem);

虽然图像显示正确..我收到此函数的错误:

fileSystem.root.getFile(imageURI, null, function(fileEntry)

错误是:FileError.ENCODING_ERR

我知道代码看起来不漂亮。但我不知道如何从 imageURI 中获取 Base64 编码。

【问题讨论】:

  • 嗨,它已经很容易给出例子了,只是参考...这个链接..here

标签: android ios blackberry cordova base64


【解决方案1】:

SERPRO 的上述方法有效......但我必须改变

reader.readAsText(file);
to
reader.readAsDataURL(file);

因此,行

image64.value = Base64.encode(evt.target.result);

可以去掉,直接提取base64结果为

image64.value = evt.target.result;

【讨论】:

    【解决方案2】:

    我在Google groups 中找到了解决方案。我稍微修改了一下,结果如下:

    var gotFileEntry = function(fileEntry) { 
        console.log("got image file entry: " +  fileEntry.fullPath); 
        fileEntry.file( function(file) {
            var reader = new FileReader();
            reader.onloadend = function(evt) {
                console.log("Read complete!");
                image64.value = Base64.encode(evt.target.result);
            };
            reader.readAsText(file);
        }, failFile);
    };
    
    window.resolveLocalFileSystemURI(imageURI, gotFileEntry, failSystem); 
    

    注意:读取一张普通的 5MPixel 图像大约需要 20 秒,然后再进行 10-15 到 Base64 编码。

    【讨论】:

    【解决方案3】:

    cordova-plugin-file 实现HTML5 File API,并使用回调API。我更喜欢 Promise,所以我使用 $q 库重写了该方法:

    function getContentAsBase64(fileUrl) {
    
      //Using $q to change the API so that getContentAsBase64 returns a promise
      var deferred = $q.defer();
    
      //function to call when either resolve or retrieval fails
      var fail = function (error) {
        errorHandler(error);
        deferred.reject(error);
      }
    
      //function to call when resolve file succeeded
      //we have a FileEntry - get the file, 
      var fileResolved = function (fileEntry) {
        fileEntry.file(fileSuccess, fail);
      }
    
      //function to call when file successfully retrieved
      //convert to base64 string
      var fileSuccess = function (file) {
    
        var reader = new FileReader();
        reader.onloadend = function (evt) {
          //promise is resolved with the base64 string
          deferred.resolve(evt.target.result);
        };
        reader.readAsDataURL(file);
      };
    
      window.resolveLocalFileSystemURL(fileUrl, fileResolved, fail);
      return deferred.promise;
    }        
    

    readAsDataURL 方法用于读取 指定的 Blob 或文件。当读操作完成后, readyState 变为 DONE,并触发 loadend。此时, result 属性包含作为 URL 的数据,表示 文件的数据作为 base64 编码的字符串。

    用法:

    var imageData;
    getContentAsBase64(aq.FileUri).then(function (content) {
       imageData= content;
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多