转自:???????? http://www.voidcn.com/article/p-oilrcbxq-ew.html
由于系统中用到的flex采集照片需要裁减功能,就把前几天搜到的Flex源码进行了修改,实现了裁减功能,不过做的比较简单,呵呵!
Flex部分:
<?xml version="1.0" encoding="utf-8"?>
< mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
???? <mx:Style>
???????? Alert{font-size:12px;}
???? </mx:Style>
???? <mx:Script>
???????? <![CDATA[
???????????? import mx.events.CloseEvent;
???????????? import mx.rpc.events.FaultEvent;
???????????? import mx.rpc.events.ResultEvent;
???????????? import mx.controls.Alert;
???????????
???????????? private static const DEFAULT_CAMERA_WIDTH:Number = 320;
???????????? private static const DEFAULT_CAMERA_HEIGHT:Number = 240;
???????????? private static const DEFAULT_WEBSERVICE_URL:String = "http://localhost:50338/WebSite1/WebService.asmx?WSDL"; //WebService地址
???????????
???????????? private var m_camera:Camera; //定义一个摄像头
???????????? private var m_localVideo:Video; //定义一个本地视频
???????????? private var m_pictureBitmapData:BitmapData //定义视频截图
????????????
???????????? public static var MoveMouseblnMove:Boolean = false;
????????????
???????????? public static var blnMove:Boolean = false;
????????????
???????????? [Bindable]
???????????? private var m_pictureData:String;
???????????
???????????? private function initApp():void
???????????? {
???????????????? t_btn_Shooting.enabled = false;
???????????????? t_ban_Save.enabled = false;
???????????????? initMouseEvent();
???????????????? initCamera();
???????????? }
???????????
???????????? //初始化摄像头
???????????? private function initCamera():void
???????????? {
???????????????? m_camera = Camera.getCamera();
???????????????? if(m_camera != null)
???????????????? {
???????????????????? m_camera.addEventListener(StatusEvent.STATUS,__onCameraStatusHandler);
???????????????????
???????????????????? m_camera.setMode(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT,30);
???????????????????? m_localVideo = new Video();
???????????????????? m_localVideo.width = DEFAULT_CAMERA_WIDTH;
???????????????????? m_localVideo.height = DEFAULT_CAMERA_HEIGHT;
???????????????????? m_localVideo.attachCamera(m_camera);
???????????????????? t_vd_Video.addChild(m_localVideo);
???????????????? }
???????????????? else
???????????????? {
???????????????????? Alert.show("没有找到摄像头,是否重新查找。","提示:",Alert.OK|Alert.NO,this,__InitCamera);
???????????????????? return;
???????????????? }
???????????? }
???????????
???????????? //拍照按钮事件,进行视频截图
???????????? private function SnapshotPicture():void
???????????? {
????????????? var r: Rectangle = new Rectangle(this.t_cv_canvasResize.x,this.t_cv_canvasResize.y,this.t_cv_canvasResize.width,this.t_cv_canvasResize.height);
????????????? var m: Matrix = new Matrix(1,1,-Math.ceil(r.x),-Math.ceil(r.y));
????????????? m_pictureBitmapData = new BitmapData(Math.floor(r.width),Math.floor(r.height));
??????????????? m_pictureBitmapData.draw(t_vd_Video,m);?????????
??????????????? var m_pictureBitmap:Bitmap = new Bitmap(m_pictureBitmapData);
??????????????? t_img_Picture.addChild(m_pictureBitmap);
???????????????
??????????????? t_panel_Picture.visible = true;
??????????????? t_ban_Save.enabled = true;
???????????? }
???????????
???????????? //保存按钮事件,保存视频截图
???????????? //通过WebService保存
???????????? private function SavePicture():void
???????????? {
???????????????? m_pictureData = "";
???????????????? for(var i:int = 0; i < this.t_cv_canvasResize.width; i++)
???????????????? {
???????????????????? for(var j:int = 0; j < this.t_cv_canvasResize.height; j++)
???????????????????? {
???????????????????????? if(m_pictureData.length > 0)
???????????????????????? {
???????????????????????????? m_pictureData += "," + m_pictureBitmapData.getPixel32(i,j).toString();
???????????????????????? }
???????????????????????? else
???????????????????????? {
???????????????????????????? m_pictureData = m_pictureBitmapData.getPixel32(i,j).toString();
???????????????????????? }
???????????????????? }
???????????????? }
???????????????? t_ws_SavePicture.SavePicture.send();
???????????? }
???????????
???????????? //检测摄像头权限事件
???????????? private function __onCameraStatusHandler(event:StatusEvent):void
???????????? {
???????????????? if(!m_camera.muted)
???????????????? {
???????????????????? t_btn_Shooting.enabled = true;
???????????????? }
???????????????? else
???????????????? {
???????????????????? Alert.show("无法链接到活动摄像头,是否重新检测。",__InitCamera);
???????????????? }
???????????????? m_camera.removeEventListener(StatusEvent.STATUS,__onCameraStatusHandler);
???????????? }
???????????
???????????? //当摄像头不存在,或连接不正常时重新获取
???????????? private function __InitCamera(event:CloseEvent):void
???????????? {
???????????????? if(event.detail == Alert.OK)
???????????????? {
???????????????????? initApp();
???????????????? }
???????????? }
???????????
???????????? //WebService保存图片成功事件
???????????? private function __onSavePictureResult(event:ResultEvent):void
???????????? {
???????????????? //trace(event.result);
???????????????? if(event.result.toString() == "保存成功")
???????????????? {
???????????????????? Alert.show(event.result.toString(),"提示",Alert.OK,__onAlertCloseHandler);
???????????????? }
???????????????? else
???????????????? {
???????????????????? Alert.show(event.result.toString(),Alert.OK);
???????????????? }
???????????? }
???????????
???????????? //连接WebService失败事件
???????????? private function __onSavePictureFault(event:FaultEvent):void
???????????? {
???????????????? //Alert.show(event.fault.toString(),Alert.OK);
???????????????? Alert.show("连接WebService失败。",Alert.OK);
???????????? }
???????????
???????????? //保存图片成功后的弹出窗口确认事件
???????????? private function __onAlertCloseHandler(event:CloseEvent):void
???????????? {
???????????????? if(event.detail == Alert.OK)
???????????????? {
???????????????????? //trace("转向页面");
???????????????? }
???????????? }
????????? //添加鼠标事件监听??
???????? public function initMouseEvent() : void
???????? {
???????????? this.t_cv_canvasResize.addEventListener(MouseEvent.MOUSE_DOWN,this.MoveMouseDownHandler);
???????????? this.t_cv_canvasResize.addEventListener(MouseEvent.MOUSE_UP,this.MoveMouseUpHandler);
???????????? this.t_cv_canvasParent.addEventListener(MouseEvent.ROLL_OUT,this.ParentMouSEOverHandler);
???????????? this.t_cv_canvasParent.addEventListener(MouseEvent.MOUSE_UP,this.ParentMouSEOverHandler);
??????????????? return;
???????? }// end function
??????????????
????????????
???????? public function MoveMouseDownHandler(event:MouseEvent) : void
???????? {
???????????? MoveMouseblnMove = true;
???????????? var _loc_2:Rectangle = new Rectangle();
???????????? _loc_2.width = this.t_vd_Video.width - this.t_cv_canvasResize.width;
???????????? _loc_2.height = this.t_vd_Video.height - this.t_cv_canvasResize.height;
???????????? this.t_cv_canvasResize.startDrag(false,_loc_2);
???????????? return;
????????? }// end function
????????????
???????? public function MoveMouseUpHandler(event:MouseEvent) : void
???????? {
???????????? this.t_cv_canvasResize.stopDrag();
???????????? MoveMouseblnMove = true;
???????????? return;
???????? }// end function
????????
???????? public function ParentMouSEOverHandler(event:MouseEvent) : void
???????? {
???????????? this.t_cv_canvasResize.stopDrag();
???????????? blnMove = false;
???????????? MoveMouseblnMove = false;
???????????? return;
???????? }// end function
????????
???????? ]]>
???? </mx:Script>
???? <mx:WebService id="t_ws_SavePicture" showBusyCursor="true" wsdl="{DEFAULT_WEBSERVICE_URL}" useProxy="false" result="__onSavePictureResult(event)" fault="__onSavePictureFault(event)">
???????? <mx:operation name="SavePicture">
???????????? <mx:request>
???????????????? <pic_width>{this.t_cv_canvasResize.width}</pic_width>
???????????????? <pic_height>{this.t_cv_canvasResize.height}</pic_height>
???????????????? <bitmap_data>{m_pictureData}</bitmap_data>
???????????? </mx:request>
???????? </mx:operation>
???? </mx:WebService>
????
???? <mx:Canvas height="600" id="t_ca_Camera" width="800" x="10" y="10">
???? <mx:Panel id="t_panel_shoot_Picture" backgroundColor="#00ffffff" borderStyle="none" color="#00333333" cornerRadius="0" fontSize="12" height="325" layout="absolute" themeColor="16777215" title="视频拍照" width="340">
?? <mx:Canvas height="240" id="t_cv_canvasParent" width="320">
??? <mx:VideoDisplay height="240" id="t_vd_Video" width="320" x="0" y="0"/>
??? <mx:Canvas backgroundAlpha="0.05" backgroundColor="#00333333" borderColor="9145227" borderStyle="solid" height="120" id="t_cv_canvasResize" visible="true" width="160" x="30" y="30"/>
?? </mx:Canvas>
?? <mx:ControlBar horizontalAlign="right">
???????????? <mx:Button id="t_btn_Shooting" label="拍照" click="SnapshotPicture()"/>
????????? </mx:ControlBar>
</mx:Panel>
?
???? <mx:Panel id="t_panel_Picture" x="360" y="10" width="180" height="200" layout="absolute" title="拍照图片" fontSize="12" visible="false">
???????? <mx:Image id="t_img_Picture" x="0" y="0" width="160" height="120"/>
???????? <mx:ControlBar?? horizontalAlign="right">
???????????? <mx:Button id="t_ban_Save" label="保存" click="SavePicture()" />
???????? </mx:ControlBar>
???? </mx:Panel>
???? </mx:Canvas>
< /mx:Application>
WebService部分:
? [WebMethod]
??? public string SavePicture(int pic_width,int pic_height,string bitmap_data)
??? {
??????? try
??????? {
??????????? Bitmap m_pic = new Bitmap(pic_width,pic_height);
??????????? string[] m_tempPics = bitmap_data.Split(',');
??????????? for (int i = 0; i < pic_width; i++)
??????????? {
??????????????? for (int j = 0; j < pic_height; j++)
??????????????? {
??????????????????? uint pic_argb = (uint)long.Parse(m_tempPics[i * pic_height + j]);
??????????????????? int pic_a = (int)(pic_argb >> 24 & 0xFF);
??????????????????? int pic_r = (int)(pic_argb >> 16 & 0xFF);
??????????????????? int pic_g = (int)(pic_argb >> 8 & 0xFF);
??????????????????? int pic_b = (int)(pic_argb & 0xFF);
??????????????????? m_pic.SetPixel(i,j,Color.FromArgb(pic_a,pic_r,pic_g,pic_b));
??????????????? }
??????????? }
??????????? string filePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Photo//";
??????????? //判断路径是否存在,若不存在则创建路径
??????????? DirectoryInfo upDir = new DirectoryInfo(filePath);
??????????? if (!upDir.Exists)
??????????? {
??????????????? upDir.Create();
??????????? }
??????????? //生成随机文件名
??????????? Random objRand = new Random();
??????????? DateTime date = DateTime.Now;
??????????? //生成随机文件名
??????????? string saveName = date.Year.ToString() + date.Month.ToString() + date.Day.ToString() + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + Convert.ToString(objRand.Next(99) * 97 + 100);
??????????? string fileName = saveName + ".jpg";
??????????? m_pic.Save(filePath + fileName,ImageFormat.Jpeg); ??????????? return "保存成功"; ??????? } ??????? catch (Exception ex) ??????? { ??????????? return ex.Message; ??????? } ??? }