flex C#在线拍照

使用flex+fluorineFx+webservices(C#)
其中注意的是.net环境与flex环境的结合,fluorineFx的引用,端口的配置。代码是没多少难度的,网上有很多资料。

可以去我的资源里下代码:http://download.csdn.net/source/569396

 

.net flex(FluorineFX) 项目建立参考:
1.唐勇blog:[翻译]配置FluorineFX环境
http://hi.baidu.com/ishowing/blog/item/68cf1330a572359da8018e36.html
2. Baijinwen blog:vs2008+flex builder 3+FluorineFx項目搭建
http://blog.csdn.net/Baijinwen/archive/2008/03/20/2199587.aspx

不过这个我参照的时候有flex建立professional都会出现一个错误:C:/Program Files/Adobe/Flex Builder 3/sdks/3.0.0

/frameworks/locale/en_US-services"../../WEB-INF/flex/services-config.xml"-context-root/WebSite1'
有人说用vs2008没事,试过也是同样的。不知道是什么原因(对和个太生疏了)。解决的办法(只是解决了,应该不是用这个方法):把services-config.xml复制到你的flex mxml源文件的文件夹。

 

flex 和webservices代码:

flex:

 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application  xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="388" height="222" creationComplete="initApp()">
  3.     <mx:Style>
  4.         Alert{font-size:12px;}
  5.     </mx:Style>
  6.     <mx:Script>
  7.         <![CDATA[
  8.             import mx.events.CloseEvent;
  9.             import mx.rpc.events.FaultEvent;
  10.             import mx.rpc.events.ResultEvent;
  11.             import mx.controls.Alert;
  12.             
  13.             private static const DEFAULT_CAMERA_WIDTH:Number = 160; //摄像头显示宽度
  14.             private static const DEFAULT_CAMERA_HEIGHT:Number = 120; //摄像头显示高度
  15.             private static const DEFAULT_WEBSERVICE_URL:String = "http://localhost:6666/WebSite1/WebService.asmx?WSDL"; //WebService地址
  16.             
  17.             private var m_camera:Camera; //定义一个摄像头
  18.             private var m_localVideo:Video; //定义一个本地视频
  19.             private var m_pictureBitmapData:BitmapData //定义视频截图
  20.             [Bindable]
  21.             private var m_pictureData:String;
  22.             
  23.             private function initApp():void
  24.             {
  25.                 t_btn_Shooting.enabled = false;
  26.                 t_ban_Save.enabled = false;
  27.                 initCamera();
  28.             }
  29.             
  30.             //初始化摄像头
  31.             private function initCamera():void
  32.             {
  33.                 m_camera = Camera.getCamera();
  34.                 if(m_camera != null)
  35.                 {
  36.                     m_camera.addEventListener(StatusEvent.STATUS,__onCameraStatusHandler);
  37.                     
  38.                     m_camera.setMode(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT,30);
  39.                     m_localVideo = new Video();
  40.                     m_localVideo.width = DEFAULT_CAMERA_WIDTH;
  41.                     m_localVideo.height = DEFAULT_CAMERA_HEIGHT;
  42.                     m_localVideo.attachCamera(m_camera);
  43.                     t_vd_Video.addChild(m_localVideo);
  44.                 }
  45.                 else
  46.                 {
  47.                     Alert.show("没有找到摄像头,是否重新查找。","提示:",Alert.OK|Alert.NO,this,__InitCamera);
  48.                     return;
  49.                 }
  50.             }
  51.             
  52.             //拍照按钮事件,进行视频截图
  53.             private function SnapshotPicture():void
  54.             {
  55.                 m_pictureBitmapData = new BitmapData(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT);
  56.                 m_pictureBitmapData.draw(t_vd_Video,new Matrix());
  57.                 
  58.                 var m_pictureBitmap:Bitmap = new Bitmap(m_pictureBitmapData);
  59.                 t_img_Picture.addChild(m_pictureBitmap);
  60.                 
  61.                 t_panel_Picture.visible = true;
  62.                 t_ban_Save.enabled = true;
  63.             }
  64.             
  65.             //保存按钮事件,保存视频截图
  66.             //通过WebService保存
  67.             private function SavePicture():void
  68.             {
  69.                 m_pictureData = "";
  70.                 for(var i:int = 0; i < DEFAULT_CAMERA_WIDTH; i++)
  71.                 {
  72.                     for(var j:int = 0; j < DEFAULT_CAMERA_HEIGHT; j++)
  73.                     {
  74.                         if(m_pictureData.length > 0)
  75.                         {
  76.                             m_pictureData += "," + m_pictureBitmapData.getPixel32(i,j).toString();
  77.                         }
  78.                         else
  79.                         {
  80.                             m_pictureData = m_pictureBitmapData.getPixel32(i,j).toString();
  81.                         }
  82.                     }
  83.                 }
  84.                 t_ws_SavePicture.SavePicture.send();
  85.             }
  86.             
  87.             //检测摄像头权限事件
  88.             private function __onCameraStatusHandler(event:StatusEvent):void
  89.             {
  90.                 if(!m_camera.muted)
  91.                 {
  92.                     t_btn_Shooting.enabled = true;
  93.                 }
  94.                 else
  95.                 {
  96.                     Alert.show("无法链接到活动摄像头,是否重新检测。","提示:",Alert.OK|Alert.NO,this,__InitCamera);
  97.                 }
  98.                 m_camera.removeEventListener(StatusEvent.STATUS,__onCameraStatusHandler);
  99.             }
  100.             
  101.             //当摄像头不存在,或连接不正常时重新获取
  102.             private function __InitCamera(event:CloseEvent):void
  103.             {
  104.                 if(event.detail == Alert.OK)
  105.                 {
  106.                     initApp();
  107.                 }
  108.             }
  109.             
  110.             //WebService保存图片成功事件
  111.             private function __onSavePictureResult(event:ResultEvent):void
  112.             {
  113.                 //trace(event.result);
  114.                 if(event.result.toString() == "保存成功")
  115.                 {
  116.                     Alert.show(event.result.toString(),"提示",Alert.OK,this,__onAlertCloseHandler);
  117.                 }
  118.                 else
  119.                 {
  120.                     Alert.show(event.result.toString(),"提示",Alert.OK);
  121.                 }
  122.             }
  123.             
  124.             //连接WebService失败事件
  125.             private function __onSavePictureFault(event:FaultEvent):void
  126.             {
  127.                 //Alert.show(event.fault.toString(),"提示",Alert.OK);
  128.                 Alert.show("连接WebService失败。","提示",Alert.OK);
  129.             }
  130.             
  131.             //保存图片成功后的弹出窗口确认事件
  132.             private function __onAlertCloseHandler(event:CloseEvent):void
  133.             {
  134.                 if(event.detail == Alert.OK)
  135.                 {
  136.                     //trace("转向页面");
  137.                 }
  138.             }
  139.         ]]>
  140.     </mx:Script>
  141.     <mx:WebService id="t_ws_SavePicture" showBusyCursor="true" wsdl="{DEFAULT_WEBSERVICE_URL}" useProxy="false" result="__onSavePictureResult(event)" fault="__onSavePictureFault(event)">
  142.         <mx:operation name="SavePicture">
  143.             <mx:request>
  144.                 <pic_width>{DEFAULT_CAMERA_WIDTH}</pic_width>
  145.                 <pic_height>{DEFAULT_CAMERA_HEIGHT}</pic_height>
  146.                 <bitmap_data>{m_pictureData}</bitmap_data>
  147.             </mx:request>
  148.         </mx:operation>
  149.     </mx:WebService>
  150.     <mx:Panel x="10" y="10" width="180" height="200" layout="absolute" title="视频拍照" >
  151.         <mx:VideoDisplay id="t_vd_Video" width="160" height="120"/>
  152.         <mx:ControlBar >
  153.             <mx:Button id="t_btn_Shooting" label="拍照" click="SnapshotPicture()"/>
  154.         </mx:ControlBar>
  155.     </mx:Panel>
  156.     <mx:Panel id="t_panel_Picture" x="198" y="10" width="180" height="200" layout="absolute" title="拍照图片"  visible="false">
  157.         <mx:Image id="t_img_Picture" x="0" y="0" width="160" height="120"/>
  158.         <mx:ControlBar>
  159.             <mx:Button id="t_ban_Save" label="保存" click="SavePicture()" />
  160.         </mx:ControlBar>
  161.     </mx:Panel>
  162. </mx:Application>

webservices:

 

  1. using System;
  2. using System.Collections;
  3. using System.Web;
  4. using System.Web.Services;
  5. using System.Web.Services.Protocols;
  6. using System.Drawing;
  7. using System.IO;
  8. /// <summary>
  9. ///WebService 的摘要说明
  10. /// </summary>
  11. [WebService(Namespace = "http://tempuri.org/")]
  12. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  13. //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
  14. // [System.Web.Script.Services.ScriptService]
  15. public class WebService : System.Web.Services.WebService
  16. {
  17.     public WebService()
  18.     {
  19.         //如果使用设计的组件,请取消注释以下行 
  20.         //InitializeComponent(); 
  21.     }
  22.     [WebMethod]
  23.     public string SavePicture(int pic_width, int pic_height, string bitmap_data)
  24.     {
  25.         try
  26.         {
  27.             Bitmap m_pic = new Bitmap(pic_width, pic_height);
  28.             string[] m_tempPics = bitmap_data.Split(',');
  29.             for (int i = 0; i < pic_width; i++)
  30.             {
  31.                 for (int j = 0; j < pic_height; j++)
  32.                 {
  33.                     uint pic_argb = (uint)long.Parse(m_tempPics[i * pic_height + j]);
  34.                     int pic_a = (int)(pic_argb >> 24 & 0xFF);
  35.                     int pic_r = (int)(pic_argb >> 16 & 0xFF);
  36.                     int pic_g = (int)(pic_argb >> 8 & 0xFF);
  37.                     int pic_b = (int)(pic_argb & 0xFF);
  38.                     m_pic.SetPixel(i, j, Color.FromArgb(pic_a, pic_r, pic_g, pic_b));
  39.                 }
  40.             }
  41.             string filePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Photo//";
  42.             //判断路径是否存在,若不存在则创建路径
  43.             DirectoryInfo upDir = new DirectoryInfo(filePath);
  44.             if (!upDir.Exists)
  45.             {
  46.                 upDir.Create();
  47.             }
  48.             //生成随机文件名
  49.             Random objRand = new Random();
  50.             DateTime date = DateTime.Now;
  51.             //生成随机文件名
  52.             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);
  53.             string fileName = saveName + ".jpg";
  54.             m_pic.Save(filePath + fileName, System.Drawing.Imaging.ImageFormat.Png);
  55.             return "保存成功";
  56.         }
  57.         catch (Exception ex)
  58.         {
  59.             return ex.Message;
  60.         }
  61.     }
  62. }

aspx:

  1. <div>
  2.     <object id="paizhao" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"
  3.                         height="368" width="440" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"  >
  4.                         <param name="_cx" value="11642"/>
  5.                         <param name="_cy" value="9737"/>
  6.                         <param name="FlashVars" value=""/>
  7.                         <param name="Movie" value="Flex/pz.swf"/>
  8.                         <param name="Src" value="Flex/pz.swf"/>
  9.                         <param name="WMode" value="Window"/>
  10.                         <param name="Play" value="-1"/>
  11.                         <param name="Loop" value="-1"/>
  12.                         <param name="Quality" value="High"/>
  13.                         <param name="SAlign" value=""/>
  14.                         <param name="Menu" value="-1"/>
  15.                         <param name="Base" value=""/>
  16.                         <param name="AllowScriptAccess" value=""/>
  17.                         <param name="Scale" value="ShowAll"/>
  18.                         <param name="DeviceFont" value="0"/>
  19.                         <param name="EmbedMovie" value="0"/>
  20.                         <param name="BGColor" value=""/>
  21.                         <param name="SWRemote" value=""/>
  22.                         <param name="MovieData" value=""/>
  23.                         <param name="SeamlessTabbing" value="1"/>
  24.                         <param name="Profile" value="0"/>
  25.                         <param name="ProfileAddress" value=""/>
  26.                         <param name="ProfilePort" value="0"/>
  27.                         <script type="text/javascript "if (navegiator . mimeTypes && navigator . mimeTypes["application/x-shockwave-flash"] { document . write('<embed src="Flex/pz.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer"
  28.                             type="application/x-shockwave-flash" width="220" height="184" name="paizhao"> </embed>')}
  29.                         </script>   
  30.     
  31.     </object>
  32.     </div>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值