Skip to content

Instantly share code, notes, and snippets.

@vinothbabu
Created September 7, 2012 13:40
Show Gist options
  • Save vinothbabu/3666340 to your computer and use it in GitHub Desktop.
Save vinothbabu/3666340 to your computer and use it in GitHub Desktop.
Capture picture using Titanium
function capturePictureDialog(opts) {
var dialogCallback = (typeof opts.callback == "undefined") ? new Function("") : opts.callback;
function cropImageBlob(image, cropRect) {
var h = image.height;
var w = image.width;
var toH = cropRect.height;
if (toH > h) {
toH = h;
}
var toW = cropRect.width;
if (toW > w) {
toW = w;
}
var cropX = cropRect.x;
if (cropX < 0) {
cropX = 0;
}
var cropY = cropRect.y;
if (cropY < 0) {
cropY = 0;
}
var baseImage = Titanium.UI.createImageView({
image: image,
width: w,
height: h
});
var cropView = Titanium.UI.createView({
width: toW,
height: toH
});
cropView.add(baseImage);
baseImage.left = -cropX;
baseImage.top = -cropY;
return cropView.toImage();
}
function rotateImageBlob90degreesClockwise(image) {
var img = Ti.UI.createImageView({
image: image,
transform: Ti.UI.create2DMatrix().rotate(-90)
});
return img.toImage();
}
Titanium.Media.showCamera({
saveToPhotoGallery : false,
showControls : false, // don't show system controls
mediaTypes : Ti.Media.MEDIA_TYPE_PHOTO,
success: function (event) {
if (event.mediaType != Ti.Media.MEDIA_TYPE_PHOTO) {
alert("Not an image!");
return false;
}
var image = event.media;
var cropRect = event.cropRect;
if (cropRect) {
var workingImage = cropImageBlob(image, cropRect);
} else {
var workingImage = cropImageBlob(image, {
x: 0,
y: 0,
height: image.height,
width: image.width
});
}
var win = Ti.UI.createWindow({
backgroundColor: 'white'
});
var hrView = Ti.UI.createView({
top:'10',
left:'100',
layout:'horizontal'
});
var workingImageView = Titanium.UI.createImageView({
image: workingImage,
width: workingImage.width,
height: workingImage.height
});
var recaptureButton = Ti.UI.createButton({
height: 30,
title: 'Re-capture'
});
recaptureButton.addEventListener('click', function (e) {
win.close();
capturePictureDialog(opts);
});
var acceptButton = Ti.UI.createButton({
height: 30,
title: 'Accept'
});
acceptButton.addEventListener('click', function (e) {
win.close();
dialogCallback({
status: "ok",
image: workingImage
});
});
var cancelButton = Ti.UI.createButton({
height: 30,
title: 'Cancel'
});
cancelButton.addEventListener('click', function (e) {
win.close();
dialogCallback({
status: "cancelled"
});
});
var rotateButton = Ti.UI.createButton({
height: 30,
title: 'Rotate'
});
rotateButton.addEventListener('click', function (e) {
workingImage = rotateImageBlob90degreesClockwise(workingImage);
workingImageView.image = workingImage;
});
win.add(workingImageView);
hrView.add(rotateButton);
hrView.add(recaptureButton);
hrView.add(acceptButton);
hrView.add(cancelButton);
win.add(hrView);
win.open();
},
cancel: function () {
dialogCallback({
status: "cancelled"
});
},
error: function (er) {
Titanium.UI.createAlertDialog({
title: 'Error',
message: 'An error occured while trying to reference your camera!'
}).show();
dialogCallback({
status: "error"
});
},
});
}
capturePictureDialog({
callback:function(obj){
if(obj.status=="ok"){
var img=Titanium.UI.createImageView({image:obj.image,width:obj.image.width, height:obj.image.height});
var win = Ti.UI.createWindow({backgroundColor: 'white'});
win.add(img);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment