Skip to content

Instantly share code, notes, and snippets.

@clathrop
Created July 26, 2012 18:49
Show Gist options
  • Save clathrop/3183771 to your computer and use it in GitHub Desktop.
Save clathrop/3183771 to your computer and use it in GitHub Desktop.
Titanium: Using a camera overlay and saving images to internal storage
var win = Titanium.UI.createWindow();
var scanner = Titanium.UI.createView({
width : 260,
height : 200,
borderColor : 'red',
borderWidth : 5,
borderRadius : 15
});
var button = Titanium.UI.createButton({
color : '#fff',
//backgroundImage : '../images/BUTT_grn_on.png',
//backgroundSelectedImage : '../images/BUTT_grn_off.png',
//backgroundDisabledImage : '../images/BUTT_gry_on.png',
bottom : 10,
width : 301,
height : 57,
font : {
fontSize : 20,
fontWeight : 'bold',
fontFamily : 'Helvetica Neue'
},
title : 'Take Picture'
});
var messageView = Titanium.UI.createView({
height : 30,
width : 250,
visible : false
});
var indView = Titanium.UI.createView({
height : 30,
width : 250,
backgroundColor : '#000',
borderRadius : 10,
opacity : 0.7
});
messageView.add(indView);
// message
var message = Titanium.UI.createLabel({
text : 'Picture Taken',
color : '#fff',
font : {
fontSize : 20,
fontWeight : 'bold',
fontFamily : 'Helvetica Neue'
},
width : 'auto',
height : 'auto'
});
messageView.add(message);
var overlay = Titanium.UI.createView();
overlay.add(scanner);
overlay.add(button);
overlay.add(messageView);
button.addEventListener('click', function() {
scanner.borderColor = 'blue';
Ti.Media.takePicture();
messageView.animate({
visible : true
});
setTimeout(function() {
scanner.borderColor = 'red';
messageView.animate({
visible : false
});
}, 500);
});
Titanium.Media.showCamera({
saveToPhotoGallery : true,
success : function(event) {
// place our picture into our window
var imageView = Ti.UI.createImageView({
image : event.media,
width : 320,
height : 480
});
win.add(imageView);
win.open();
alert("picture was taken");
//alert(event);
// programatically hide the camera
//Ti.Media.hideCamera();
},
cancel : function() {
},
error : function(error) {
var a = Titanium.UI.createAlertDialog({
title : 'Camera'
});
if (error.code == Titanium.Media.NO_CAMERA) {
a.setMessage('Please run this test on device');
} else {
a.setMessage('Unexpected error: ' + error.code);
}
a.show();
},
overlay : overlay,
showControls : false, // don't show system controls
mediaTypes : Ti.Media.MEDIA_TYPE_PHOTO,
autohide : false // tell the system not to auto-hide and we'll do it ourself
});
Copy link

ghost commented Feb 7, 2015

Hi,
I found this post very helpful, but i have a small doubt. Is it possible to capture photo without clicking that button. say like setting some time delay 5 or 10 seconds. (Capture automatically after opening camera in 10 seconds and send back the captured image)

@junerockwell
Copy link

How do you adjust the camera? Using this custom overlay, the camera shows on the entire screen of the phone.

@aakashsharmaindore
Copy link

hey , can any one help me..?
record video with overlay ???
camera is just hide and recording is done, camera record whatever is front of camera.

@mg4u
Copy link

mg4u commented Jun 18, 2016

Hi
Thanks for this nice code
when i run it on emulator it works fine,but when i run it in actual device i get this error
error code:-1 , file == null
what dose that mean?

@ChanakyaSrinivas
Copy link

How to apply the selfie functionality

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment