Skip to content

Instantly share code, notes, and snippets.

@srahim
Created April 17, 2012 20:25
Show Gist options
  • Save srahim/2408765 to your computer and use it in GitHub Desktop.
Save srahim/2408765 to your computer and use it in GitHub Desktop.
Camera Overlay
var win = Titanium.UI.createWindow();
function getOrientation(o)
{
switch (o)
{
case Titanium.UI.PORTRAIT:
return 'portrait';
case Titanium.UI.UPSIDE_PORTRAIT:
return 'reverse portrait';
case Titanium.UI.LANDSCAPE_LEFT:
return 'landscape';
case Titanium.UI.LANDSCAPE_RIGHT:
return 'reverse landscape';
case Titanium.UI.FACE_UP:
return 'face up';
case Titanium.UI.FACE_DOWN:
return 'face down';
case Titanium.UI.UNKNOWN:
return 'unknown';
}
}
var scanner = Titanium.UI.createView({
width:260,
height:200,
borderColor:'red',
borderWidth:5,
borderRadius:15
});
var button = Titanium.UI.createButton({
color:'blue',
bottom:10,
width:301,
height:57,
font:{fontSize:20,fontWeight:'bold',fontFamily:'Helvetica Neue'},
title:'Take Picture'
});
var button1 = Titanium.UI.createButton({
color:'blue',
bottom:10,
width:301,
height:57,
right:20,
font:{fontSize:20,fontWeight:'bold',fontFamily:'Helvetica Neue'},
title:'Take Picture'
});
var button2 = Titanium.UI.createButton({
color:'blue',
bottom:10,
width:301,
height:57,
left:20,
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);
overlay.add(button1);
overlay.add(button2);
button1.hide();
button2.hide();
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({
success:function(event)
{
Ti.API.debug("picture was taken");
// place our picture into our window
var imageView = Ti.UI.createImageView({
image:event.media,
width:win.width,
height:win.height
});
win.add(imageView);
// 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
});
function showbutton(){
button1.show();
button2.show();
}
function hidebutton(){
button1.hide();
button2.hide();
}
Ti.Gesture.addEventListener('orientationchange',function(e)
{
// device orientation
var orient = getOrientation(e.orientation);
Ti.API.info("current orientation : "+ orient);
if( e.orientation == Ti.UI.PORTRAIT || e.orientation == Titanium.UI.UPSIDE_PORTRAIT){
hidebutton();
}
else if(e.orientation ==Titanium.UI.LANDSCAPE_LEFT || e.orientation == Titanium.UI.LANDSCAPE_RIGHT){
showbutton();
}
});
win.open();
var currOrientation = Ti.Gesture.orientation;
if(currOrientation ==Titanium.UI.LANDSCAPE_LEFT || currOrientation == Titanium.UI.LANDSCAPE_RIGHT){
showbutton();
}
else{
hidebutton();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment