我有一个从朋友那里得到的弹性代码。但我想自定义此代码来解决我的需求。当它可能在小程序上加载时,我想将宽度和高度从 jsp 传递给 flex 代码。这个宽度和高度将决定捕获图像的宽度和高度。捕获图像后,单击“完成”按钮,该按钮调用服务器 url,传递捕获图像的 java byte[]。下面是代码:
<?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>