flex 摄像头拍照预览 功能

时间:2023-02-07 13:32:11

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark"
      xmlns:mx="library://ns.adobe.com/flex/mx"
      width="1000" height="900"   name="application1"  creationComplete="application1_creationCompleteHandler(event)">
 <fx:Script>
  <![CDATA[
   import flash.net.navigateToURL;
   import mx.containers.TitleWindow;
   import mx.controls.Alert;
   import mx.controls.TextArea;
   import mx.core.IFlexDisplayObject;
   import mx.effects.effectClasses.ZoomInstance;
   import mx.events.CloseEvent;
   import mx.events.FlexEvent;
   import mx.graphics.codec.JPEGEncoder;
   import mx.graphics.codec.PNGEncoder;
   import mx.managers.PopUpManager;
   import spark.components.Image;

   private var CAMERA_WIDTH:int=400;
   private var CAMERA_HEIGHT:int=300;
   private var _camera:Camera;
   private var _pngEn:PNGEncoder;
   private var _bitmapData:BitmapData;
   private var _fr:FileReference;
   private var i_:int=0;
   private var file:FileReference = new FileReference();
   private var datas:XML=new XML();
   private var url_1:String;
   private  var imgbytes:ByteArray;
   
   protected function application1_creationCompleteHandler(event:FlexEvent):void
   {
    _pngEn=new PNGEncoder();
    _fr=new FileReference;
    checkCamera();
    init();
    cameraDisplay.width=CAMERA_WIDTH;
    cameraDisplay.height=CAMERA_HEIGHT;
    img.width=CAMERA_WIDTH;
    img.height=CAMERA_HEIGHT;    
   }
   private function checkCamera(event:CloseEvent=null):void
   {
    _camera=Camera.getCamera();
    if(_camera!=null){
     _camera.setMode(CAMERA_WIDTH,CAMERA_HEIGHT,24);
     _camera.setQuality(0,100);
     
     cameraDisplay.attachCamera(_camera);
     cutCameraBtn.enabled=true;
    }else{
     Alert.okLabel="重试";
     Alert.show("找不到摄像头,程序将重新查找","对不起",4,null,checkCamera);
    }

   }
   private function getDateString():String{
    var date:Date=new Date;
    return "拍照"+date.getFullYear()+(date.getMonth()+1)+date.getDate()+date.getHours()+date.getMinutes()+date.getSeconds()+date.getMilliseconds();
   }
   protected function cutCameraBtn_clickHandler(event:MouseEvent):void
   {
    _bitmapData=new BitmapData(CAMERA_WIDTH,CAMERA_HEIGHT);
    _bitmapData.draw(cameraDisplay);
    var bytes:ByteArray=_pngEn.encode(_bitmapData);
    img.source=bytes;//将资源加到Image中
    BtnUpload.enabled=true;
    img.visible=true;
    imgbytes=bytes;//把照相生成的字节流保存起来
    btnyl.enabled=true;
    openWindow(event);
   }
   protected function  UpLoadImageToserver(event:MouseEvent):void
   {
    var urlrequest:URLRequest = new URLRequest(url_1);
    urlrequest.contentType = 'applicatoin/octet-stream';
    urlrequest.method = URLRequestMethod.POST;
    urlrequest.data = imgbytes;
    navigateToURL(urlrequest);
    Alert.show("上传成功");
    img.visible=false;
   }
   protected function   ImageClose(event:MouseEvent):void
   {
    img.visible=false;   
   }
   private function init():void 
   {
    var url:URLRequest=new URLRequest("Flashconfig.xml");
    var loader:URLLoader=new URLLoader(); 
    loader.addEventListener(Event.COMPLETE,onComplete);
    loader.load(url);
   }
   private function onComplete(event:Event):void
   { 
    var result:URLLoader=URLLoader(event.target);
    datas=XML(result.data); 
    url_1=datas.child("address").text();
   }
   private function ImageFd(event:MouseEvent):void
   {
    CAMERA_WIDTH=1200;
    CAMERA_HEIGHT=800;
    cameraDisplay.width=CAMERA_WIDTH;
    cameraDisplay.height=CAMERA_HEIGHT;
    img.width=CAMERA_WIDTH;
    img.height=CAMERA_HEIGHT;
    _camera=Camera.getCamera();
    if(_camera!=null){
     _camera.setMode(CAMERA_WIDTH*2,CAMERA_HEIGHT*2,24);
     _camera.setQuality(0,100);
     
     cameraDisplay.attachCamera(_camera);
     cutCameraBtn.enabled=true;
    }else{
     Alert.okLabel="重试";
     Alert.show("找不到摄像头,程序将重新查找","对不起",4,null,checkCamera);
    }
   }
   private function ImageFd1(event:MouseEvent):void
   {
    CAMERA_WIDTH=400;
    CAMERA_HEIGHT=300;
    cameraDisplay.width=CAMERA_WIDTH;
    cameraDisplay.height=CAMERA_HEIGHT;
    img.width=CAMERA_WIDTH;
    img.height=CAMERA_HEIGHT;
    _camera=Camera.getCamera();
    if(_camera!=null){
     _camera.setMode(CAMERA_WIDTH*2,CAMERA_HEIGHT*2,24);
     _camera.setQuality(0,100);
     
     cameraDisplay.attachCamera(_camera);
     cutCameraBtn.enabled=true;
    }else{
     Alert.okLabel="重试";
     Alert.show("找不到摄像头,程序将重新查找","对不起",4,null,checkCamera);
    }
   }   
   public function openWindow(event:MouseEvent):void {
    var myPopUp:TextArea;
    myPopUp = new TextArea();
    myPopUp.width= 0;
    myPopUp.height= 0;
    myPopUp.setStyle("fontSize",12);
    myPopUp.setStyle("backgroundAlpha",0.5);
    var panel:TitleWindow=new TitleWindow();
    panel.showCloseButton=true;
    panel.addEventListener(CloseEvent.CLOSE,closeWin);
    panel.title="图片预览";
    panel.setStyle("backgroundAlpha",0);
    panel.setStyle("fontSize",12);
    panel.addChild(myPopUp);
    panel.addChild(img);
    PopUpManager.addPopUp(panel, this, true);
    PopUpManager.centerPopUp(panel);
   }
   private function closeWin(event:CloseEvent):void{
    PopUpManager.removePopUp(IFlexDisplayObject(event.currentTarget));
   }
  ]]>
 </fx:Script>
 <fx:Declarations>
  <!-- 将非可视元素(例如服务、值对象)放在此处 -->
  <s:Wipe id="WipeUp" direction="up" duration="100"/>
  <s:Wipe id="WipeUp1" direction="up" duration="200"/>
 </fx:Declarations>
 <mx:VBox horizontalAlign="center">
  <mx:VideoDisplay id="cameraDisplay"/>
  <mx:HBox  horizontalAlign="center">
   <mx:Button id="cutCameraBtn"
        label="拍照"
        click="cutCameraBtn_clickHandler(event)"
        enabled="false"
        />
   <mx:Button id="BtnUpload"
        label="上传"
        click="UpLoadImageToserver(event)"
        enabled="false"
        />
   <mx:Button id="Btnlv1"
        label="初始化"
        click="ImageFd1(event)"
       />
   <mx:Button id="Btnlv2"
        label="放大2倍"
        click="ImageFd(event)"
        />
   <mx:Button id="btnyl"  enabled="false"  label="预览" click="openWindow(event)"> 
   </mx:Button>
  </mx:HBox>
  <mx:Image id="img"   hideEffect="{WipeUp}"/>
 </mx:VBox>
</s:Application>