Link to home
Start Free TrialLog in
Avatar of Marco Gasi
Marco GasiFlag for Spain

asked on

Cordova Camera plugin fails

Hi everybody.
Im here with the same old mobile app. I'm trying to capture an image through camera and put it in the page, but the calls seems to fail instantly when I click the button 'Take a picture'.
If it doesn't fail, when camera is activate it shutdown the main app. I installed the https://github.com/zebra1024/cordova-plugin-wezka-nativecamera plugin which should fix that but I can't get it working.

This is index.html of my testing app
<!DOCTYPE html>
<html>
    <head>
        <!--
        Customize this policy to fit your own app's needs. For more guidance, see:
            https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
        Some notes:
            * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
            * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
            * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
                * Enable inline JS: add 'unsafe-inline' to default-src
        -->
        <!--<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">-->

		<!--<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: ms-appdata: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">-->  
		<meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
        <!--<script type="text/javascript" src="js/tesseract.js"></script>-->
        <!--<script type="text/javascript" src="https://cdn.rawgit.com/naptha/tesseract.js/1.0.10/dist/tesseract.js"></script>-->
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>		
        <title>Hello OCR</title>
		<style>
			#picture-wrapper{
				width: 100%;
				min-height: 100px;
				text-align: center;
			}
			#picture-wrapper img{
				width: 100%;
				height: auto;
			}
			textarea{
				width: 100%;
				min-height: 200px;
			}
		</style>
    </head>
    <body>
        <div class="app">
			<button id="camera">Take a picture</button>
			<br>
			<br>
			<div id="picture-wrapper">
				<img id="pic" src="" />
			</div>
			<button id="take-text">Take text</button>
			<textarea id="result"></textarea>
        </div>
    </body>
</html>

Open in new window


and this is the only one javascript
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var app = {
	// Application Constructor
	initialize: function () {
		document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
	},
	// deviceready Event Handler
	//
	// Bind any cordova events here. Common events are:
	// 'pause', 'resume', etc.
	onDeviceReady: function () {
		this.receivedEvent('deviceready');
	},
	// Update DOM on a Received Event
	receivedEvent: function (id) {

	}
};

app.initialize();

function setOptions(srcType) {
	var options = {
		// Some common settings are 20, 50, and 100
		quality: 50,
		destinationType: Camera.DestinationType.FILE_URI,
		// In this app, dynamically set the picture source, Camera or photo gallery
		sourceType: srcType,
		encodingType: Camera.EncodingType.JPEG,
		mediaType: Camera.MediaType.PICTURE,
		allowEdit: true,
		correctOrientation: true  //Corrects Android orientation quirks
	};
	return options;
}

function openCamera() {

	var srcType = Camera.PictureSourceType.CAMERA;
	var options = setOptions(srcType);
	var func = createNewFileEntry;

	navigator.camera.getPicture(function(imageUri) {
		displayImage(imageUri);
		// You may choose to copy the picture, save it somewhere, or upload.
//		func(imageUri);
	}, function(error) {
		console.debug("Unable to obtain picture: " + error, "app");
	}, options);
}
function createNewFileEntry(imgUri) {
    window.resolveLocalFileSystemURL(cordova.file.cacheDirectory, function success(dirEntry) {

        // JPEG file
        dirEntry.getFile("tempFile.jpeg", { create: true, exclusive: false }, function (fileEntry) {

            // Do something with it, like write to it, upload it, etc.
            // writeFile(fileEntry, imgUri);
            console.log("got file: " + fileEntry.fullPath);
            // displayFileData(fileEntry.fullPath, "File copied to");

        }, onErrorCreateFile);

    }, onErrorResolveUrl);
}
function displayImage(imgUri) {

    var elem = document.getElementById('pic');
    elem.src = imgUri;
}

$(document).ready(function () {

	$('#camera').on('click', function () {
		console.log('click');
		openCamera();
//		navigator.camera.getPicture(onSuccess, onFail, {quality: 50,
//			destinationType: Camera.DestinationType.FILE_URI});
//
//		function onSuccess(imageURI) {
//			console.log(imageURI);
//			$('#pic').attr('src', imageURI);
//		}
//
//		function onFail(message) {
//			alert('Failed because: ' + message);
//		}
	});

	$('#take-text').on('click', function () {
//			Tesseract.recognize($('#pic')).then(function (result) {
//				$('#result').val(result);
//				console.log(result);
//			});

	});

});

Open in new window


If someone good guy (remember we are near to Cristmas) can hlp me, I would really appreciate :)
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marco Gasi

ASKER

Solved.