嗨,可以使用javascript / angularjs和ionic1将Blob / Base64转换为fileURI格式。

时间:2018-12-18 03:53:56

标签: javascript angularjs ionic-v1

我试图使用cordovaCamera和imagePicker捕获图像,然后删除EXIF数据并创建删除元数据后得到的图像副本。

在下面的代码中,我获得了Blob格式的图像文件,该图像文件传递给resolveLocalFileSystemURL,但无法继续进行,因为resolveLocalFileSystemURL不接受blob或base64数据,因此可以将Blob / Base64转换为fileURI格式。还是有其他解决方案。

在html中: <input id="erd" type="file"/>

在controllers.js中:

var input = document.querySelector('#erd');
input.addEventListener('change', load);
function load(){
    var fr = new FileReader();
    fr.onload = process;
    fr.readAsArrayBuffer(this.files[0]);
    console.log(" onload "+URL.createObjectURL(this.files[0]));
}

function process(){
var dv = new DataView(this.result);
var offset = 0, recess = 0;
var pieces = [];
var i = 0;
if (dv.getUint16(offset) == 0xffd8){
    offset += 2;
    var app1 = dv.getUint16(offset);
    offset += 2;
    while (offset < dv.byteLength){
        if (app1 == 0xffe1){

            pieces[i] = {recess:recess,offset:offset-2};
            recess = offset + dv.getUint16(offset);
            i++;
        }
        else if (app1 == 0xffda){
            break;
        }
        offset += dv.getUint16(offset);
        var app1 = dv.getUint16(offset);
        offset += 2;
    }
    if (pieces.length > 0){
        var newPieces = [];
        pieces.forEach(function(v){
            newPieces.push(this.result.slice(v.recess, v.offset));
        }, this);
        newPieces.push(this.result.slice(recess));
        var br = new Blob(newPieces, {type: 'image/jpeg'});
        console.log(" pieces.length "+URL.createObjectURL(br));
        $scope.strimg=URL.createObjectURL(br).toString();
        //var file = new File(URL.createObjectURL(br), "uploaded_file.jpg", { type: "image/jpeg", lastModified: Date.now() });

        var myFile = blobToFile(br, "my-image.jpg"); 
        console.log('myFile'+JSON.stringify(myFile));
        $scope.strimg = myFile;
        createFileEntry($scope.strimg);
        }
    }
}
        function blobToFile(theBlob,fileName){ 
        console.log('theBlob'+theBlob+fileName);
        var b = theBlob; 
        b.lastModifiedDate = new Date(); 
        b.name = fileName; //Cast to a File() type 
        return theBlob; 
        }



    function createFileEntry(fileURI) {
        window.resolveLocalFileSystemURL(fileURI, copyFile, fail);
    }

    // 5
    function copyFile(fileEntry) {
    console.log(" copyFile "+fileEntry);
        var name = fileEntry.fullPath.substr(fileEntry.fullPath.lastIndexOf('/') + 1);
        var newName = name;

        $scope.filepathnew="file:///storage/emulated/0/Android/data/com.offermanagement.system/files/";

        window.resolveLocalFileSystemURL($scope.filepathnew, function(fileSystem2) {
                                        fileEntry.copyTo(
                                        fileSystem2,
                                        newName,
                                        onCopySuccess,
                                        fail
                                    );
                            },
                            fail);
    }

    function onCopySuccess(entry) {
        $scope.$apply(function () {
            $rootScope.profilepic=entry.nativeURL;
            $rootScope.images.push(entry.nativeURL);
            $rootScope.items.push({src:entry.nativeURL,sub:'' });

        });
    }

    function fail(error) {
        console.log("fail: " + error.code);
    }
  

以上代码出现以下错误   “未捕获的TypeError:resolveLocalFileSystemURI的参数“ uri”的类型错误:期望的字符串,但是有Blob。”

0 个答案:

没有答案