Back to Demos List

GSW Browser Camera

This demonstration will show the ability to access the camera from within the browser.




About This Demo

This demo is using the cordova-plugin-camera plugin. For more information click here:


		var GSW = GSW || {};
GSW.Tests = GSW.Tests || {};


GSW.Tests.onDeviceReady = function() {
	// Cordova is now initialized.

	console.log('Now running cordova-' + cordova.platformId + '@' + cordova.version);

	GSW.Tests.cameraTakePicture = function() {
		navigator.camera.getPicture(GSW.Tests.onSuccess, GSW.Tests.onFail, {
			quality: 50,
			correctOrientation: true,
			destinationType: Camera.DestinationType.DATA_URL
		});
	}

    GSW.Tests.onSuccess = function(imageData) {
    	console.log('onSuccess');
    
    	// Display the image
    	var image = document.getElementById('myImage');
    	image.src = "data:image/jpeg;base64," + imageData;
    
    	// Upload to server
    	var uri = "https://www.georgiasoftworks.info/cordova/demos/file-upload.php";
    	var uploadStatus = document.getElementById("status");
    
    	var xhr = new XMLHttpRequest();
    	xhr.open("POST", uri, true);
    	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
    	xhr.onload = function () {
    		if (xhr.status === 200) {
    			console.log("Upload success:", xhr.responseText);
    			uploadStatus.innerHTML = "Upload operation successful";
    			uploadStatus.style.color = "green";
    		} else {
    			console.error("Upload failed:", xhr.responseText);
    			uploadStatus.innerHTML = "Upload failed. Server responded with status " + xhr.status;
    			uploadStatus.style.color = "red";
    		}
    	};
    
    	xhr.onerror = function () {
    		console.error("Upload error");
    		uploadStatus.innerHTML = "Upload operation error.";
    		uploadStatus.style.color = "red";
    	};
    
    	// Send Base64 as POST data
    	var params = "uNQRZFecBPbU7JuRg93L=" + encodeURIComponent(imageData);
    	xhr.send(params);
    };


	GSW.Tests.onFail = function(message) {
		var para = document.getElementById('alert');
		let code = message;
		if(code === 20){
		    para.innerHTML = "Camera permissions are not enabled. Please enable camera permissions for GSW ConnectBot";
		}else{
			para.innerHTML = "Operation was not completed due to: " + message;				    
		}
	}
	document.getElementById("cameraTakePicture").addEventListener("click", GSW.Tests.cameraTakePicture);
}
document.addEventListener('deviceready', GSW.Tests.onDeviceReady, false);