I have a flex code that i got from a friend. But i want to customize this code to solve my needs. I want to pass a width and height to the flex code from jsp when it is loading probably on an applet. This width and height will determine the width and height of the captured image. After the image is captured, a “Done” button is clicked which calls a server url passing a java byte[] of the captured image. Below is the code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application initialize="videoDisplay_creationComplete();" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" x="149" y="131">
<mx:Script>
<![CDATA[
import mx.graphics.ImageSnapshot;
import mx.controls.Alert;
public var cam:Camera = Camera.getCamera();
public var video:Video = new Video(320,240);
public var bitmapData:BitmapData = new BitmapData(video.width,video.height);
public var snapshot:BitmapData;
public function init():void
{
videoDisplay.visible=false;
}
public function start():void{
if (cam == null)
{
Alert.show("No camera is installed","Unable to start capture!!!!");
return;
}
video.attachCamera(cam);
video.x = 20;
video.y = 20;
addChild(video);
var bitmap:Bitmap = new Bitmap(bitmapData);
bitmap.x = 360;
bitmap.y = 20;
addChild(bitmap);
}
private function captureImage(e:MouseEvent):void
{
bitmapData.draw(video);
}
private function videoDisplay_creationComplete():void
{
btnRestartCapture.enabled=false;
if (cam!=null) {
videoDisplay.attachCamera(cam);
} else {
Alert.show("You don't seem to have a camera.");
}
}
public function takeSnapshot():void
{
snapshot = ImageSnapshot.captureBitmapData(videoDisplay);
var bitmap:Bitmap= new Bitmap(snapshot);
img.source=bitmap;
videoDisplay.visible=false;
btnRestartCapture.enabled=true;
}
public function sendToServer():void{
var loader:URLLoader= new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.data=snapshot.getPixels(new Rectangle(0,0,img.width,img.height));
}
private function callJavaScript():void {
ExternalInterface.call("recievePictureFromFlex",snapshot.getPixels(new Rectangle(0,0,img.width,img.height)));
}
public function restartCap():void{
videoDisplay.visible=true;
btnRestartCapture.enabled=false;
}
public function done():void
{
Alert.show("Done taking pix", "Done");
}
]]>
</mx:Script>
<!--<mx:Button label="Capture Image" click="captureImage(null);" id="capture_mc"/>-->
<mx:VBox x="405" y="115" backgroundColor="white" >
<mx:Canvas width="100%" height="100%">
<mx:Image width="350" id="img" height="300"/>
<mx:VideoDisplay id="videoDisplay"
creationComplete="videoDisplay_creationComplete();"
width="350"
height="300" x="0" y="0"/>
</mx:Canvas>
<mx:HBox>
<mx:Button id="btnRestartCapture" label="Clear Picture" click="restartCap();"/>
<mx:Button id="snapButton" label="Snap" click="takeSnapshot()" width="113"/>
<mx:Button id="doneButton" label="Done" click="done()" width="113"/>
</mx:HBox>
</mx:VBox>
</mx:Application>
For the jsp –> Flex communication, you can use flashvars :
http://livedocs.adobe.com/flex/3/html/help.html?content=passingarguments_3.html