arnaucube
arnaucube/objectImageIdentifierAIPublic Self-hosted repository
objectImageIdentifierAI/smartphoneApp/www/js/main.js
smartphoneApp/www/js/main.jsRaw
  1angular.module('app.main', [])
  2
  3.controller('MainCtrl', function($scope, $http, fileReader) {
  4    $scope.response = "";
  5    $scope.imageSrc="";
  6    $scope.uploadFile = function() {
  7        var blob = dataURItoBlob(selectedImg);
  8        $scope.img_file= blob;
  9
 10        console.log("$scope.img_file");
 11        console.log($scope.img_file);
 12        var fd = new FormData();
 13        //Take the first selected file
 14        fd.append("file", $scope.img_file);
 15        console.log(fd);
 16        $http({
 17                url: urlapi + 'predict',
 18                method: "POST",
 19                headers: {
 20                    "Content-Type": undefined
 21                },
 22                data: fd
 23            })
 24            .then(function(data) {
 25                    console.log("response: ");
 26                    console.log(data.data);
 27                    // response reaction
 28                    $scope.response= data.data.result;
 29                },
 30                function(response) { // optional
 31                    // failed
 32                    console.log(response);
 33                });
 34    };
 35
 36    function dataURItoBlob(dataURI) {
 37        // convert base64/URLEncoded data component to raw binary data held in a string
 38        var byteString;
 39        if (dataURI.split(',')[0].indexOf('base64') >= 0)
 40            byteString = atob(dataURI.split(',')[1]);
 41        else
 42            byteString = unescape(dataURI.split(',')[1]);
 43
 44        // separate out the mime component
 45        var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
 46
 47        // write the bytes of the string to a typed array
 48        var ia = new Uint8Array(byteString.length);
 49        for (var i = 0; i < byteString.length; i++) {
 50            ia[i] = byteString.charCodeAt(i);
 51        }
 52
 53        return new Blob([ia], {type:mimeString});
 54    }
 55
 56})
 57.directive("ngFileSelect", function(fileReader, $timeout) {
 58    return {
 59      scope: {
 60        ngModel: '='
 61      },
 62      link: function($scope, el) {
 63        function getFile(file) {
 64          fileReader.readAsDataUrl(file, $scope)
 65            .then(function(result) {
 66              $timeout(function() {
 67                $scope.ngModel = result;
 68                selectedImg = result;
 69              });
 70            });
 71        }
 72
 73        el.bind("change", function(e) {
 74          var file = (e.srcElement || e.target).files[0];
 75          getFile(file);
 76        });
 77      }
 78    };
 79  })
 80.factory("fileReader", function($q, $log) {
 81  var onLoad = function(reader, deferred, scope) {
 82    return function() {
 83      scope.$apply(function() {
 84        deferred.resolve(reader.result);
 85      });
 86    };
 87  };
 88
 89  var onError = function(reader, deferred, scope) {
 90    return function() {
 91      scope.$apply(function() {
 92        deferred.reject(reader.result);
 93      });
 94    };
 95  };
 96
 97  var onProgress = function(reader, scope) {
 98    return function(event) {
 99      scope.$broadcast("fileProgress", {
100        total: event.total,
101        loaded: event.loaded
102      });
103    };
104  };
105
106  var getReader = function(deferred, scope) {
107    var reader = new FileReader();
108    reader.onload = onLoad(reader, deferred, scope);
109    reader.onerror = onError(reader, deferred, scope);
110    reader.onprogress = onProgress(reader, scope);
111    return reader;
112  };
113
114  var readAsDataURL = function(file, scope) {
115    var deferred = $q.defer();
116
117    var reader = getReader(deferred, scope);
118    reader.readAsDataURL(file);
119
120    return deferred.promise;
121  };
122
123  return {
124    readAsDataUrl: readAsDataURL
125  };
126});