Flex完整代码:
程序代码:
Code
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:1888/Web/TestWebService.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="视频拍照" fontSize="12">
151 <mx:VideoDisplay id="t_vd_Video" width="160" height="120"/>
152 <mx:ControlBar horizontalAlign="right">
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="拍照图片" fontSize="12" visible="false">
157 <mx:Image id="t_img_Picture" x="0" y="0" width="160" height="120"/>
158 <mx:ControlBar horizontalAlign="right">
159 <mx:Button id="t_ban_Save" label="保存" click="SavePicture()" />
160 </mx:ControlBar>
161 </mx:Panel>
162</mx:Application>
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:1888/Web/TestWebService.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="视频拍照" fontSize="12">
151 <mx:VideoDisplay id="t_vd_Video" width="160" height="120"/>
152 <mx:ControlBar horizontalAlign="right">
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="拍照图片" fontSize="12" visible="false">
157 <mx:Image id="t_img_Picture" x="0" y="0" width="160" height="120"/>
158 <mx:ControlBar horizontalAlign="right">
159 <mx:Button id="t_ban_Save" label="保存" click="SavePicture()" />
160 </mx:ControlBar>
161 </mx:Panel>
162</mx:Application>
C# WebService代码:
程序代码
Code
1 [WebMethod]
2 public string SavePicture(int pic_width, int pic_height, string bitmap_data)
3 {
4 try
5 {
6 Bitmap m_pic = new Bitmap(pic_width, pic_height);
7 string[] m_tempPics = bitmap_data.Split(',');
8
9 for (int i = 0; i < pic_width; i++)
10 {
11 for (int j = 0; j < pic_height; j++)
12 {
13 uint pic_argb = (uint)long.Parse(m_tempPics[i * pic_height + j]);
14 int pic_a = (int)(pic_argb >> 24 & 0xFF);
15 int pic_r = (int)(pic_argb >> 16 & 0xFF);
16 int pic_g = (int)(pic_argb >> 8 & 0xFF);
17 int pic_b = (int)(pic_argb & 0xFF);
18
19 m_pic.SetPixel(i, j, Color.FromArgb(pic_a, pic_r, pic_g, pic_b));
20 }
21 }
22
23 string filePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Photo\\";
24
25 //判断路径是否存在,若不存在则创建路径
26 DirectoryInfo upDir = new DirectoryInfo(filePath);
27 if (!upDir.Exists)
28 {
29 upDir.Create();
30 }
31
32 //生成随机文件名
33 Random objRand = new Random();
34 DateTime date = DateTime.Now;
35
36 //生成随机文件名
37 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);
38 string fileName = saveName + ".jpg";
39
40 m_pic.Save(filePath + fileName, ImageFormat.Jpeg);
41 return "保存成功";
42 }
43 catch(Exception ex)
44 {
45 return ex.Message;
46 }
47 }
1 [WebMethod]
2 public string SavePicture(int pic_width, int pic_height, string bitmap_data)
3 {
4 try
5 {
6 Bitmap m_pic = new Bitmap(pic_width, pic_height);
7 string[] m_tempPics = bitmap_data.Split(',');
8
9 for (int i = 0; i < pic_width; i++)
10 {
11 for (int j = 0; j < pic_height; j++)
12 {
13 uint pic_argb = (uint)long.Parse(m_tempPics[i * pic_height + j]);
14 int pic_a = (int)(pic_argb >> 24 & 0xFF);
15 int pic_r = (int)(pic_argb >> 16 & 0xFF);
16 int pic_g = (int)(pic_argb >> 8 & 0xFF);
17 int pic_b = (int)(pic_argb & 0xFF);
18
19 m_pic.SetPixel(i, j, Color.FromArgb(pic_a, pic_r, pic_g, pic_b));
20 }
21 }
22
23 string filePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Photo\\";
24
25 //判断路径是否存在,若不存在则创建路径
26 DirectoryInfo upDir = new DirectoryInfo(filePath);
27 if (!upDir.Exists)
28 {
29 upDir.Create();
30 }
31
32 //生成随机文件名
33 Random objRand = new Random();
34 DateTime date = DateTime.Now;
35
36 //生成随机文件名
37 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);
38 string fileName = saveName + ".jpg";
39
40 m_pic.Save(filePath + fileName, ImageFormat.Jpeg);
41 return "保存成功";
42 }
43 catch(Exception ex)
44 {
45 return ex.Message;
46 }
47 }