flex+FluorineFx ASP.NET 视频拍照

声明:此代码为网上代码修改而来,为与FluorineFX配合做了相应修改([Flex视频截图并通过WebService(C#)保存])
Flex代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application  xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" height="100%" creationComplete="initApp()" horizontalAlign="center" verticalAlign="middle" fontFamily="Courier New" fontSize="12"> 
    
<mx:Style> 
        Alert{font
-size:12px;} 
    
</mx:Style> 
    
<mx:Script> 
        
<![CDATA[ 
            
import mx.graphics.codec.JPEGEncoder;
            
import flash.net.NetConnection;
            
import flash.net.ObjectEncoding;
            
import flash.net.Responder;
            
import mx.events.CloseEvent; 
            
import mx.controls.Alert; 
            
            
//
            private var nc:NetConnection;
            
private var rp:Responder;
            
            
private var DEFAULT_CAMERA_WIDTH:Number = 230//摄像头显示宽度 
            private var DEFAULT_CAMERA_HEIGHT:Number = 167//摄像头显示高度 
             
            
private var m_camera:Camera; //定义一个摄像头 
            private var m_localVideo:Video; //定义一个本地视频 
            private var m_pictureBitmapData:BitmapData //定义视频截图 
            private var m_picUrl:String;//保存的图片名称
             
            
private var fileRef:FileReference;
              
private var urlReq:URLRequest;
            
private function initApp():void 
            { 
                m_picUrl
="";
                t_btn_Shooting.enabled 
= false
                t_ban_Save.enabled 
= false
                initCamera(); 
            } 
             
            
//初始化摄像头 
            private function initCamera():void 
            { 
                m_camera 
= Camera.getCamera(); 
                
if(m_camera != null
                { 
                    DEFAULT_CAMERA_WIDTH 
= t_vd_Video.width;//m_camera.width;
                    DEFAULT_CAMERA_HEIGHT=t_vd_Video.height;//m_camera.height;
                    
                    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 
            { 
                m_pictureBitmapData 
= new BitmapData(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT); 
                m_pictureBitmapData.draw(t_vd_Video,
new Matrix()); 
                 
                var m_pictureBitmap:Bitmap 
= new Bitmap(m_pictureBitmapData); 
                t_img_Picture.addChild(m_pictureBitmap); 
                 
                t_panel_Picture.visible 
= true
                t_ban_Save.enabled 
= true
                
                
//将bitmap转存到byteArray
                
//编码成图片格式流
                
                
//var pixels:ByteArray = m_pictureBitmapData.getPixels( new Rectangle(0,0,DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT) );
                
//上面一行的方法行不通,需要使用以下两行的方法
                var encoder:JPEGEncoder = new JPEGEncoder(100); 
                var pixels:ByteArray 
= encoder.encode(m_pictureBitmapData);
                
                   nc
=new NetConnection();
                
//以处理来自服务器的返回值
                rp=new Responder(successResult,errStatus);
                
//指示应对其调用回调方法的对象
                nc.client=nc;
                
//设置采用的编码
                
                nc.objectEncoding 
= flash.net.ObjectEncoding.AMF3;
                nc.connect(
"http://172.168.0.99/Flfx/Gateway.aspx");
                nc.call(
"Flfx.Sample.getPic",rp,pixels,"test.jpg");
            } 
            
private function successResult(result:String):void {
                trace(
"返回结果:"+result);
                m_picUrl
=result;
                trace(m_picUrl);
                
//关闭连接
                nc.close();
            }
            
private function errStatus(result:Boolean):void {
                trace(
"发生错误!");
                Alert.show(
"不好意思,发生错误!*_!");
                nc.close();
            }
            
//保存按钮事件,下载视频截图 
            private function SavePicture():void 
            { 
                
if(m_picUrl != "" && m_picUrl != "null" )
                {
                    
//Alert.show("http://172.168.0.99/flfx/"+m_picUrl);
                    urlReq = new URLRequest("http://172.168.0.99/flfx/"+m_picUrl);
                    fileRef 
= new FileReference();
                    fileRef.download(urlReq);
                }
            } 
             
            
//检测摄像头权限事件 
            private function onCameraStatusHandler(event:StatusEvent):void 
            { 
                
if(!m_camera.muted) 
                { 
                    t_btn_Shooting.enabled 
= true
                } 
                
else 
                { 
                    Alert.show(
"无法链接到活动摄像头,是否重新检测。","提示:",Alert.OK|Alert.NO,this,InitCamera); 
                } 
                m_camera.removeEventListener(StatusEvent.STATUS,onCameraStatusHandler); 
            } 
             
            
//当摄像头不存在,或连接不正常时重新获取 
            private function InitCamera(event:CloseEvent):void 
            { 
                
if(event.detail == Alert.OK) 
                { 
                    initApp(); 
                } 
            } 

        ]]
> 
    
</mx:Script> 

    
<mx:Panel x="10" y="10" width="250" height="251" layout="absolute" title="视频拍照"  horizontalAlign="center" verticalAlign="middle"> 
        
<mx:VideoDisplay id="t_vd_Video" width="230" height="167" backgroundColor="#9DC6A7"/> 
        
<mx:ControlBar > 
            
<mx:Button id="t_btn_Shooting" label="拍照" click="SnapshotPicture()"/> 
        
</mx:ControlBar> 
    
</mx:Panel> 
    
<mx:Panel id="t_panel_Picture" x="268" y="10" width="250" height="251" layout="absolute" title="拍照图片"  visible="false"> 
        
<mx:Image id="t_img_Picture" x="0" y="0" width="230" height="167"/> 
        
<mx:ControlBar> 
            
<mx:Button id="t_ban_Save" label="保存" click="SavePicture()" /> 
        
</mx:ControlBar> 
    
</mx:Panel> 
</mx:Application>


ASP.NET代码[FluorineFx web项目 Sample.cs类]:

using System;
using FluorineFx;
using FluorineFx.AMF3; 
using System.IO;   
using System.Net;
using System.Data;   
using System.Drawing;   
using System.Drawing.Imaging;   
using System.Web;

namespace Flfx
{
    
/// <summary>
    
/// Fluorine sample service.
    
/// </summary>
    [RemotingService("Fluorine sample service")]
    
public class Sample
    {
        
public Sample()
        {
        }

        
public string Echo(string text)
        {
            
return "Gateway echo: " + text;
        }

        
//生成图片
        public string getPic(ByteArray byteArray,string fileName)   
        {   

            
uint length = byteArray.Length;   
            
byte[] bytes = new byte[length];   
            byteArray.ReadBytes(bytes, 
0, length);   
                
            Stream ms 
= new MemoryStream(bytes);

            Image img 
= Bitmap.FromStream(ms);   
            Bitmap bmp 
= new Bitmap(img); 
            ms.Position
=0;
            
//Bitmap bmp = (Bitmap)Bitmap.FromStream(ms);   


            
//To save the image to a file   
            MemoryStream tempStream = new MemoryStream();   
            bmp.Save(tempStream,System.Drawing.Imaging.ImageFormat.Jpeg);   

            
//文件名
            DateTime date = DateTime.Now; 
            fileName
=date.Year.ToString() + date.Month.ToString() + date.Day.ToString() + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + Convert.ToString((new Random()).Next(99* 97 + 100); 
            fileName
+=".jpg";

            FileStream fs 
= new FileStream(System.Web.HttpContext.Current.Server.MapPath(fileName), FileMode.Create);   
            tempStream.WriteTo(fs);   
            tempStream.Close();   
            fs.Close();   

            
return fileName;
        }  
    }
}

效果图:

摄像头是"9158虚拟摄像头",放了张图片;D
posted @ 2008-12-25 10:52  aoogur  阅读(1901)  评论(1编辑  收藏  举报