1

I am trying to capture images using cordovaCamera and imagePicker then remove the EXIF data and create a copy of the image that I get after the metadata is removed.

In the following code I get image file in Blob format which I pass to resolveLocalFileSystemURL but unable to proceed as resolveLocalFileSystemURL does not accept blob or base64 data so is it possible to convert Blob/Base64 to fileURI format. Or is there any alternative to solve this.

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

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

I get the following error for the above code "Uncaught TypeError: Wrong type for parameter "uri" of resolveLocalFileSystemURI: Expected String, but got Blob."

georgeawg
  • 48,608
  • 13
  • 72
  • 95
virja
  • 21
  • 5

0 Answers0