Created
October 26, 2011 15:38
-
-
Save alanleard/1316722 to your computer and use it in GitHub Desktop.
Android Camera with Gallery button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var win = Titanium.UI.createWindow({ | |
backgroundColor:'white' | |
}); | |
win.open(); | |
var toolbar = Titanium.UI.createView({ | |
width:50, | |
borderColor:'#000', | |
borderWidth:5, | |
borderRadius:15, | |
left:0, | |
top:0, | |
bottom:0 | |
}); | |
var button = Titanium.UI.createButton({ | |
color:'#fff', | |
backgroundColor:'red', | |
bottom:10, | |
width:301, | |
height:57, | |
font:{fontSize:20,fontWeight:'bold',fontFamily:'Helvetica Neue'}, | |
title:'Take Picture' | |
}); | |
var galleryButton= Titanium.UI.createView({ | |
height:30, | |
width:30, | |
top:30, | |
backgroundColor:'green' | |
}); | |
toolbar.add(galleryButton); | |
galleryButton.addEventListener('click', function(){ | |
Titanium.Media.openPhotoGallery({ | |
success:function(event) | |
{ | |
var cropRect = event.cropRect; | |
var image = event.media; | |
// set image view | |
Ti.API.debug('Our type was: '+event.mediaType); | |
if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) | |
{ | |
imageView.image = image; | |
} | |
else | |
{ | |
// is this necessary? | |
} | |
Titanium.API.info('PHOTO GALLERY SUCCESS cropRect.x ' + cropRect.x + ' cropRect.y ' + cropRect.y + ' cropRect.height ' + cropRect.height + ' cropRect.width ' + cropRect.width); | |
}, | |
cancel:function() | |
{ | |
}, | |
error:function(error) | |
{ | |
}, | |
allowEditing:true, | |
mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO,Ti.Media.MEDIA_TYPE_PHOTO] | |
}); | |
}); | |
var overlay = Titanium.UI.createView(); | |
overlay.add(toolbar); | |
overlay.add(button); | |
button.addEventListener('click',function() | |
{ | |
Ti.Media.takePicture(); | |
}); | |
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 | |
}); | |
win.open(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment