0

On my IOS mobile application I am taking picture and then select a picture from gallery. After selecting this picture I am using ng-img-crop.js to crop this image and without saving this cropped image, trying to upload it to a server, the server side is C# WCF. (In this particular example I am trying to upload local IIS) But I am getting this error:

ERROR: {"code":1,"source":"data:image/png;base64,iVBORw0KGgoAAAANSUhE...","target":"http://11.111.11.111/wcf/OCRService.svc/upload","http_status":null,"body":null,"exception":null}
app.js (76,24)

Should I save this cropped image to the gallery before uploading to the server? Is there any way to upload it without saving?

Here is my HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />

    <meta http-equiv="Content-Security-Policy" content="connect-src 'self' http://11.111.11.111/wcf/OCRService.svc/upload 'unsafe-eval' 'unsafe-inline'; default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">

    <title>LoTTo</title>-->

    <!-- LoTTo references -->
    <link href="css/ionicons.css" rel="stylesheet" />
    <link href="css/ionic.css" rel="stylesheet" />
    <link href="css/ng-img-crop.css" rel="stylesheet" />

    <!-- Cordova reference, this is added to your app when it's built. -->
    <script src="scripts/angular.min.js"></script>
    <script src="scripts/ionic.bundle.js"></script>
    <script src="scripts/ng-cordova.min.js"></script>
    <script src="scripts/ng-img-crop.js"></script>
    <script src="cordova.js"></script>
    <script src="scripts/platformOverrides.js"></script>
    <script src="scripts/app.js"></script>
    <script src="scripts/index.js"></script>
    <script src="scripts/ng-file-upload-shim.min.js"></script>
    <script src="scripts/ng-file-upload.min.js"></script>

    <style>
        .cropArea {
            background: #E4E4E4;
            overflow: hidden;
            width: 300px;
            height: 300px;
        }
        .croppedArea {
            background: #E4E4E4;
            overflow: hidden;
            width: 300px;

        }
    </style>
</head>
<body ng-app="starter">
    <ion-header-bar class="bar bar-header bar-light">
        <h1 class="title">LoTTo</h1>
    </ion-header-bar>
    <ion-content ng-controller="ExampleController" padding="true">
        <button class="button button-full button-balanced icon-right ion-ios-camera" ng-click="takePhoto()">
            Resim Çek
        </button>

        <button class="button button-full button-balanced icon-right ion-images" ng-click="choosePhoto()">
            Resim Seç
        </button>
        <center>
            <div class="cropArea">

                <img-crop image="myImage" result-image="myCroppedImage" chargement="'Loading'"
                          area-type="rectangle"
                          area-min-size="50"
                          result-image-format="image/jpeg"
                          result-image-quality="1"
                          result-image-size="{w:300, h:50}"></img-crop>

            </div>
            <div ng-show="myImage !== undefined">Gönderilecek Resim:</div>
            <div class="croppedArea"><img ng-src="{{myCroppedImage}}" ng-show="myImage !== undefined" id="image" /></div>


        </center>
        <button class="button button-full button-balanced icon-right ion-images" ng-click="upload(myCroppedImage);" ng-if="myImage !== undefined">
            Resim Yükle
        </button>

    </ion-content>    
</body>
</html>

Here is the app.js:

angular.module('starter', ['ionic', 'ngCordova', 'ngImgCrop', 'ngFileUpload'])

.controller("ExampleController", ['$scope', '$cordovaCamera', 'Upload', '$timeout', '$cordovaFileTransfer', function ($scope, $cordovaCamera, Upload, $timeout, $cordovaFileTransfer) {

    $scope.takePhoto = function () {
        var options = {
            quality: 100,
            destinationType: Camera.DestinationType.DATA_URL,
            sourceType: Camera.PictureSourceType.CAMERA,
            allowEdit: true,
            encodingType: Camera.EncodingType.JPEG,
            targetWidth: 300,
            targetHeight: 300,
            popoverOptions: CameraPopoverOptions,
            saveToPhotoAlbum: true
        };

        $cordovaCamera.getPicture(options).then(function (imageData) {
            $scope.imgURI = "data:image/jpeg;base64," + imageData;
        }, function (err) {
            console.log(err);
            alert(err);
        });
    }

    $scope.choosePhoto = function () {
        var options = {
            quality: 100,
            destinationType: Camera.DestinationType.DATA_URL,
            sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
            allowEdit: true,
            encodingType: Camera.EncodingType.JPEG,
            targetWidth: 300,
            targetHeight: 300,
            popoverOptions: CameraPopoverOptions,
            saveToPhotoAlbum: false
        };

        $cordovaCamera.getPicture(options).then(function (imageData) {
            $scope.imgURI = "data:image/jpeg;base64," + imageData;
            $scope.myImage = $scope.imgURI;

        }, function (err) {
            console.log(err);
            alert(err);
        });
    }

    $scope.myCroppedImage = '';

    $scope.upload = function (dataUrl) {

    Upload.upload({

        url: 'http://192.168.1.20/wcf/upload',
        data: {
            file: Upload.dataUrltoBlob(dataUrl)
        },

    }).then(function (response) {
        $timeout(function () {
            $scope.result = response.data;
            console.log(response.data);
            alert(response.data);
        });
        console.log(response.data);
    }, function (response) {
        if (response.status > 0) $scope.errorMsg = response.status
            + ': ' + response.data;
        alert(response.status);
    });

}

}]);

Edit 1: How to add Access-Control-Allow-Origin to my mobile app?

Edit 2: Changed the upload section

Best Regards.

Cenk
  • 129
  • 3
  • 15

1 Answers1

1

This example might help you: Check this jsfiddle: [http://jsfiddle.net/xxo3sk41/1/][1]

code :

var app = angular.module('fileUpload', ['ngFileUpload', 'ngImgCrop']);

app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
$scope.upload = function (dataUrl) {
    Upload.upload({
        url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
        data: {
            file: Upload.dataUrltoBlob(dataUrl)
        },
    }).then(function (response) {
        $timeout(function () {
            $scope.result = response.data;
        });
    }, function (response) {
        if (response.status > 0) $scope.errorMsg = response.status 
            + ': ' + response.data;
    }, function (evt) {
        $scope.progress = parseInt(100.0 * evt.loaded / evt.total);
    });
}
}]);
Tushar
  • 3,022
  • 2
  • 26
  • 26
  • I am getting this error ***Failed to load resource: The request timed out.* @Tushar – Cenk Jan 18 '16 at 15:17
  • At home I am getting this error: Failed to load resource: the server responded with a status of 405 (Method Not Allowed) upload (0,0) – Cenk Jan 18 '16 at 20:13
  • I am using local IIS 7.5 and installed asp, isapi etc. Now I am getting 0 from the response. What does it mean? There is no error log on IIS. If 0 means successful, there is no file on the uploaded folder. – Cenk Jan 19 '16 at 21:16
  • I will try this [link] (http://stackoverflow.com/questions/12458444/enabling-cross-origin-resource-sharing-on-iis7) and let you know. – Cenk Jan 20 '16 at 05:43
  • @Cenk, is that issue solved or are you still not able to find a solution ? – Tushar Jan 22 '16 at 02:24
  • not resolved, still working on it. I think there is CORS issue.Should I use Upload.http instead? – Cenk Jan 22 '16 at 05:42
  • @Cenk, the problem might be here : var targetPath = croppedImg; – Tushar Jan 22 '16 at 10:19
  • I already changed the upload section as you suggested but still getting 0 response. Please check my edit 2. – Cenk Jan 22 '16 at 10:42
  • $scope.upload = function (dataUrl) { .... This method requires a fileUrl, but you are passing a data object, not url. – Tushar Jan 22 '16 at 13:56
  • I am trying to send the cropped image without saving it to the gallery. Is there a way to do it? – Cenk Jan 22 '16 at 14:10
  • Using this plugin you can't do that probably. Use other plugin that has that feature. For this plugin you may delete the file after successful upload. That way the file will be uploaded and it won't be there after upload. – Tushar Jan 23 '16 at 02:27
  • Hi @Tushar, can I send this picture to a web service on a server in order to OCR this image and return a result? Is this possible? – Cenk Jul 02 '16 at 10:53
  • Yes why not. It's possible. The server needs to process the image and then return the result as response. – Tushar Jul 03 '16 at 17:50
  • can you suggest another plugin @Tushar in order to send picture to a web service and return the result from the service without saving the cropped image. – Cenk Jul 03 '16 at 18:26
  • Take a look at https://github.com/jcesarmobile/PhonegapOCRPlugin and https://github.com/matiastucci/ionic-ocr-example – Tushar Jul 04 '16 at 03:27
  • don't get me wrong but I already have a OCR service. I just want to send image to this service and get a response from this service. How can I do it with my example above? – Cenk Jul 04 '16 at 08:56