Red Eye Reduction in Flex

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" creationComplete="init()" backgroundColor="white">
<mx:Script>
<![CDATA[
import mx.graphics.codec.JPEGEncoder;
import mx.controls.Alert;
 
private var red:Number = -1;
private var blue:Number = -1;
private var green:Number = -1;
private var repo:BitmapData;
private var isMouseDown:Boolean = false;
private var numMouseX:Number = -1;
private var numMouseY:Number = -1;
private var selectionRect:Rectangle;
private var curr:Number = -1;
private var file:FileReference;
 
private function init():void
{
selectionRect = new Rectangle();
wrapper.addEventListener(MouseEvent.MOUSE_DOWN,handleMouseEvent);
wrapper.addEventListener(MouseEvent.MOUSE_MOVE,handleMouseEvent);
wrapper.addEventListener(MouseEvent.MOUSE_UP,handleMouseEvent);
wrapper.addEventListener(MouseEvent.MOUSE_OUT,handleMouseEvent);   
}
 
private function handleMouseEvent(event:MouseEvent):void
{
if(event.type == MouseEvent.MOUSE_DOWN)
{
isMouseDown = true;
numMouseX = event.localX;
numMouseY = event.localY;
}
else if(event.type == MouseEvent.MOUSE_MOVE && isMouseDown)
{
wrapper.graphics.clear();
wrapper.graphics.lineStyle(1,0xFFFFFF,1);
wrapper.graphics.beginFill(0xFFFFFF,0.2);
wrapper.graphics.drawRect(numMouseX,numMouseY,(event.localX - numMouseX),(event.localY - numMouseY));
wrapper.graphics.endFill();
selectionRect = new Rectangle(numMouseX,numMouseY,(event.localX - numMouseX),(event.localY - numMouseY));
}
else if(event.type == MouseEvent.CLICK)
wrapper.graphics.clear();
else
isMouseDown = false;
}
 
public function convertToRGB(color:uint):void
{
red = (color >> 16) & 0xFF;
green = (color >> 8) & 0xFF;
blue = color & 0xFF;
}
 
private function convertToHEX(red:Number, green:Number, blue:Number):uint
{
var tem:uint = ((red) << 16 | (green) << 8 | (blue));
return  tem;
}
 
private function applyRedEye():void
{
var sourceArray:ByteArray = ((imgRedEye.content as Bitmap).bitmapData.clone()).getPixels(selectionRect);//repo.getPixels(selectionRect);
var tempData:BitmapData = new BitmapData(selectionRect.width,selectionRect.height,true,0x00ffffff);
sourceArray.position = 0;
tempData.setPixels(new Rectangle(0,0,selectionRect.width,selectionRect.height),sourceArray);
for(var x:uint=0; x < tempData.width; x++)
{
for(var y:uint=0; y < tempData.height; y++)
{
convertToRGB(tempData.getPixel(x,y));
var prev:Number = (red - (blue + green));
if((red - (blue + green)) > -20)
{       
trace("Red Eye" + prev);                       
tempData.setPixel(x,y,convertToHEX((green+blue)/2,(green+blue)/2,(green+blue)/2));
}
else
{
trace(prev);
}
}
}
var intBitData:BitmapData = (imgRedEye.content as Bitmap).bitmapData.clone();
var ba:ByteArray = tempData.getPixels(new Rectangle(0,0,selectionRect.width,selectionRect.height));
ba.position = 0;
intBitData.setPixels(selectionRect,ba);
imgRedEye.source = new Bitmap(intBitData.clone(),PixelSnapping.ALWAYS,true);
wrapper.graphics.clear();
}
 
private function onLoadComplete():void
{
repo = (imgRedEye.content as Bitmap).bitmapData.clone();
}               
 
]]>
</mx:Script>
<mx:Canvas id="can">
<mx:Image source="http://kirankum/347.jpg"  width="768" height="1024" complete="onLoadComplete()" id="imgRedEye" maintainAspectRatio="false"/>
<mx:HBox id="wrapper" width="768" height="1024"/>
</mx:Canvas>   
<mx:HSlider minimum="1" maximum="4" change="can.scaleX = can.scaleY = slide.value" id="slide"/>
<mx:Button label="Apply" click="applyRedEye()"/>
</mx:Application>
 

Leave a comment

Rotate any Flex Component

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
	creationComplete="init()">
<mx:Script>
	<![CDATA[
		import mx.formatters.SwitchSymbolFormatter;
		private var isMouseDown:Boolean = false;
		private var prevQuad:Number;
		private var prevAngle:Number;
 
		private function init():void
		{
			addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
		}
 
		private function handleMouseDown(event:MouseEvent):void
		{
			isMouseDown = true;
		}
 
		private function handleMouseUp(event:MouseEvent):void
		{
			isMouseDown = false;
		}
		private function handleMouseMove(event:MouseEvent):void
		{
			if(isMouseDown)
				CallRotate(event);
		}
 
		private function CallRotate(event:Event):void
		{
			var x:Number = can.x;// + (can.width / 2);
			var y:Number = can.y;// + (can.height / 2);
			var strQuadrant:Number;
 
			if(this.mouseX > x && this.mouseY < y)
			{
				strQuadrant = 4;
			}
			else if(this.mouseX < x && this.mouseY < y)
			{
				strQuadrant = 3;
			}
			else if(this.mouseX < x && this.mouseY > y)
			{
				strQuadrant = 2;
			}
			else if(this.mouseX > x && this.mouseY > y)
			{
				strQuadrant = 1;
			}
			var m1:Number = 0;//(this.mouseY - (can.y)) / (this.mouseX - (can.x));
			var m2:Number = (this.mouseY - x) / (this.mouseX - y);
			var angle:Number = Math.atan(m2 - m1/(1 + (m1 * m2)));
			angle = angle * 180 / Math.PI;
			switch(strQuadrant)
			{
				case 1:
						angle = angle;
					break;
				case 2:
						angle = 180 + angle;
					break;
				case 3:
						angle = 270 - (90 - angle);
					break;
				case 4:
						angle = angle;
					break;
			}
			//Normalizing the points exactly on the Quadrant whose slopes are either 0 or undefined. ie., Horizontal and Vertical axes		
			if(strQuadrant.toString() == "NaN")
			{
				switch(prevQuad)
				{
					case 1:
							if(prevAngle > 0 && prevAngle < 10)
								angle = 0;
							else if(prevAngle < 90)
								angle = 90;
						break;
					case 2:
							if(prevAngle > 90 && prevAngle < 100)
								angle = 90;
							else if(prevAngle < 180)
								angle = 180;
						break;
					case 3:
							if(prevAngle > 180 && prevAngle < 190)
								angle = 180;
							else if(prevAngle < 270)
								angle = 270;
						break;
					case 4:
							if(prevAngle > -90 && prevAngle < -80)
								angle = -90;
							else if(prevAngle < 0)
								angle = 0;
						break
				}
			}
			trace("Quadrant : "+ strQuadrant + " Angle : "+angle)
			prevAngle = angle;
			prevQuad = strQuadrant;
			can.rotation = angle;
		}
	]]>
</mx:Script>
	<mx:Image visible="true" source="@Embed(source='Sunset.png')" id="can" width="300" height="200" maintainAspectRatio="false" x="300" y="300"/>
	<!--<mx:TextArea enabled="false" visible="true" text="Hello" id="can" width="300" height="200" x="300" y="300"/>-->
	<!--<mx:HRule width="100%" strokeWidth="2" y="{can.y}"/>
	<mx:VRule height="100%" strokeWidth="2" x="{can.x}"/>	-->
</mx:Application>

Leave a comment

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

1 Comment