Skip to content

Instantly share code, notes, and snippets.

@vinothbabu
Created September 10, 2012 13:50
Show Gist options
  • Save vinothbabu/3691041 to your computer and use it in GitHub Desktop.
Save vinothbabu/3691041 to your computer and use it in GitHub Desktop.
Capture picture and Scribbling using Titanium
function capturePictureDialog(opts)
{
var dialogCallback=(typeof opts.callback=="undefined")?new Function(""):opts.callback;
var scribblingEnabled=(typeof opts.scribbling=="undefined")?false:opts.scribbling;
if(scribblingEnabled)
{
Titanium.Paint = require('ti.paint');
Ti.Paint = Titanium.Paint;
}
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 rotateImageBlob90degreesCCW(image)//counterclockwise
{
var img = Ti.UI.createImageView({image:image, transform:Ti.UI.create2DMatrix().rotate(90)});
return img.toImage();
}
function rotateImageBlob90degreesCW(image)//clockwise
{
var img = Ti.UI.createImageView({image:image, transform:Ti.UI.create2DMatrix().rotate(-90)});
return img.toImage();
}
Titanium.Media.showCamera({
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'});
if(scribblingEnabled)
{
var workingImageView= Ti.Paint.createPaintView({image:workingImage,top:0,right:0,bottom:120,left:0,
strokeColor:'#0f0',strokeAlpha:255,strokeWidth:10,eraseMode:false});
}else{
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:workingImageView.image});
});
var cancelButton = Ti.UI.createButton({height: 30,title: 'Cancel'});
cancelButton.addEventListener('click', function (e)
{
win.close();dialogCallback({status:"cancelled"});
});
var rotateCWButton = Ti.UI.createButton({height: 30,title: 'Rotate right'});
rotateCWButton.addEventListener('click', function (e)
{
workingImage=rotateImageBlob90degreesCW(workingImageView.image);
workingImageView.image=workingImage;
});
var rotateCCWButton = Ti.UI.createButton({height: 30,title: 'Rotate left'});
rotateCCWButton.addEventListener('click', function (e)
{
workingImage=rotateImageBlob90degreesCCW(workingImageView.image);
workingImageView.image=workingImage;
});
if(scribblingEnabled)
{
var buttonStrokeWidth = Ti.UI.createButton({left: 10,bottom: 10,right: 10,height: 30,title: 'Decrease Stroke Width'});
buttonStrokeWidth.addEventListener('click', function (e)
{
workingImageView.strokeWidth = (workingImageView.strokeWidth === 10) ? 5 : 10;
e.source.title = (workingImageView.strokeWidth === 10) ? 'Decrease Stroke Width' : 'Increase Stroke Width';
})
var buttonStrokeColorRed = Ti.UI.createButton({bottom: 100,left: 10,width: 75,height: 30,title: 'Red'});
buttonStrokeColorRed.addEventListener('click', function ()
{
workingImageView.strokeColor = 'red';
});
var buttonStrokeColorGreen = Ti.UI.createButton({bottom: 70,left: 10,width: 75,height: 30,title: 'Green'});
buttonStrokeColorGreen.addEventListener('click', function ()
{
workingImageView.strokeColor = '#0f0';
});
var buttonStrokeColorBlue = Ti.UI.createButton({bottom: 40,left: 10,width: 75,height: 30,title: 'Blue'});
buttonStrokeColorBlue.addEventListener('click', function ()
{
workingImageView.strokeColor = '#0000ff';
});
var buttonClear = Ti.UI.createButton({bottom: 40,left: 100,width: 75,height: 30,title: 'Clear'});
buttonClear.addEventListener('click', function ()
{
workingImageView.clear();
});
var buttonStrokeAlpha = Ti.UI.createButton({bottom: 70,right: 10,width: 100,height: 30,title: 'Alpha : 100%'});
buttonStrokeAlpha.addEventListener('click', function (e)
{
workingImageView.strokeAlpha = (workingImageView.strokeAlpha === 255) ? 127 : 255;
e.source.title = (workingImageView.strokeAlpha === 255) ? 'Alpha : 100%' : 'Alpha : 50%';
});
var buttonStrokeColorEraser = Ti.UI.createButton({bottom: 40,right: 10,width: 100,height: 30,title: 'Erase : Off'});
buttonStrokeColorEraser.addEventListener('click', function (e)
{
workingImageView.eraseMode = (workingImageView.eraseMode) ? false : true;
e.source.title = (workingImageView.eraseMode) ? 'Erase : On' : 'Erase : Off';
});
}
win.add(workingImageView);
win.add(rotateButton);
win.add(recaptureButton);
if(scribblingEnabled)
{
win.add(buttonStrokeWidth);
win.add(buttonStrokeColorRed);
win.add(buttonStrokeColorGreen);
win.add(buttonStrokeColorBlue);
win.add(buttonClear);
win.add(buttonStrokeAlpha);
win.add(buttonStrokeColorEraser);
}
win.add(acceptButton);
win.add(cancelButton);
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"});
},
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment