<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:net="flash.net.*"
layout="vertical" fontSize="12" viewSourceURL="srcview/index.html">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.utils.XMLUtil;
import mx.utils.ObjectProxy;
import mx.collections.XMLListCollection;
import mx.core.UIComponent;
import com.jooce.net.uploadFile;
import com.adobe.images.JPGEncoder;
import mx.rpc.events.AbstractEvent;
private var file:FileReference = new FileReference();
private var bitmapData:BitmapData;
private function videoDisplay_creationComplete():void {
var camera:Camera = Camera.getCamera();
if (camera) {
videoDisplay.attachCamera(camera);
} else {
Alert.show("偵測不到webCam");
}
}
private function takePhoto():void{
bitmapData = new BitmapData(320, 240);
var bmp:Bitmap = new Bitmap(bitmapData);
var holder:UIComponent = new UIComponent();
holder.addChild(bmp);
bitmapData.draw(videoDisplay);
swfLoader.source = new Bitmap(bitmapData);
}
private function savejpg():void{
bitmapData.draw(videoDisplay);
var loader: URLLoader = new URLLoader();
loader.addEventListener( Event.COMPLETE, onURLLoaderComplete );
loader.addEventListener( IOErrorEvent.IO_ERROR, onURLLoaderFailure );
var byteArray:ByteArray = new JPGEncoder(85).encode(bitmapData);
var filename:String=(Math.floor(Math.random()*10000000*10)>>0).toString();
filename=filename+".jpg";
uploadFile(loader, 'http://localhost:1625/src/savefile.aspx', byteArray, filename);
}
private function onURLLoaderComplete(evt:Event):void{
Alert.show("上傳成功");
}
private function onURLLoaderFailure(evt:Event):void{
Alert.show("上傳失敗");
}
]]>
</mx:Script>
<mx:ApplicationControlBar width="691">
<mx:Button id="button"
label="重新偵測webCam"
click="videoDisplay_creationComplete();" />
<mx:Button label="快照" click="takePhoto()"/>
<mx:Button label="儲存" click="savejpg()"/>
<mx:Spacer width="100%"/>
<mx:Label text="Flex Webcam快照【http://www.minwt.com】"/>
</mx:ApplicationControlBar>
<mx:HBox>
<mx:Panel title="webCam" width="340" height="280"
verticalScrollPolicy="off" horizontalScrollPolicy="off">
<mx:VideoDisplay id="videoDisplay"
creationComplete="videoDisplay_creationComplete();"
width="320"
height="240" />
</mx:Panel>
<mx:Panel title="快照結果" width="340" height="280"
verticalScrollPolicy="off" horizontalScrollPolicy="off">
<mx:SWFLoader id="swfLoader"/>
</mx:Panel>
</mx:HBox>
</mx:Application>