flash中的代码:
在舞台上放上一个视频元件,大小是160×120,名字为mybox。再放一个button按扭,名字为photo_btn,第一桢代码如下:
1 import mx.data.components.WebServiceConnector;
2 this.cam = Camera.get();
3 var bitmap_data:String = "";
4 var pic_width:Number;
5 var pic_height:Number;
6 mybox.attachVideo(this.cam);
7 photo_btn.onRelease = function() {
8 myBitmap = new flash.display.BitmapData(160, 120, true, 0);
9 myBitmap.draw(mybox);
10 var tempObj = _root.createEmptyMovieClip("photo", 100);
11 tempObj._x = 300;
12 tempObj._y = 100;
13 pic_width = myBitmap.width;
14 pic_height = myBitmap.height;
15 tempObj.attachBitmap(myBitmap, 1, "always", true);
16 for (i=0; i<pic_width; i++) {
17 for (j=0; j<pic_height; j++) {
18 bitmap_data += myBitmap.getPixel32(i, j).toString()+",";
19 }
20 }
21 trace(pic_width+" "+pic_height);
22 trace(bitmap_data);
23 wsConn.params = [pic_width, pic_height, bitmap_data];
24 bitmap_data = "";
25 wsConn.trigger();
26 };
27 var res:Function = function (evt:Object) {
28 //返回值的处理函数
29 trace(evt.target.results);
30 };
31 var wsConn:WebServiceConnector = new WebServiceConnector();
32 wsConn.addEventListener("result", res);
33 wsConn.WSDLURL = "http://localhost/bitmap_process/bitmap_process.asmx?Wsdl";
34 wsConn.operation = "bitmap_data_store2";
35 wsConn.suppressInvalidCalls = true;
36
2 this.cam = Camera.get();
3 var bitmap_data:String = "";
4 var pic_width:Number;
5 var pic_height:Number;
6 mybox.attachVideo(this.cam);
7 photo_btn.onRelease = function() {
8 myBitmap = new flash.display.BitmapData(160, 120, true, 0);
9 myBitmap.draw(mybox);
10 var tempObj = _root.createEmptyMovieClip("photo", 100);
11 tempObj._x = 300;
12 tempObj._y = 100;
13 pic_width = myBitmap.width;
14 pic_height = myBitmap.height;
15 tempObj.attachBitmap(myBitmap, 1, "always", true);
16 for (i=0; i<pic_width; i++) {
17 for (j=0; j<pic_height; j++) {
18 bitmap_data += myBitmap.getPixel32(i, j).toString()+",";
19 }
20 }
21 trace(pic_width+" "+pic_height);
22 trace(bitmap_data);
23 wsConn.params = [pic_width, pic_height, bitmap_data];
24 bitmap_data = "";
25 wsConn.trigger();
26 };
27 var res:Function = function (evt:Object) {
28 //返回值的处理函数
29 trace(evt.target.results);
30 };
31 var wsConn:WebServiceConnector = new WebServiceConnector();
32 wsConn.addEventListener("result", res);
33 wsConn.WSDLURL = "http://localhost/bitmap_process/bitmap_process.asmx?Wsdl";
34 wsConn.operation = "bitmap_data_store2";
35 wsConn.suppressInvalidCalls = true;
36
Flex 2.0代码:
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()" fontSize="25">
3 <mx:Script>
4 <![CDATA[
5 import flash.media.Camera;
6 import flash.media.Video;
7 import flash.display.*;
8 import mx.rpc.soap.*;
9 [Bindable]
10 public var bitmap_data:String = "";
11 public var pic_width:int;
12 public var pic_height:int;
13 public function init():void
14 {
15 var cam:Camera = Camera.getCamera();
16 if (cam != null) {
17 video_mc.attachCamera(cam);
18 } else {
19 trace("You need a camera.");
20 }
21 }
22 public function but_click():void {
23 var myBitmap:BitmapData = new flash.display.BitmapData(160, 120, true, 0);
24 myBitmap.draw(video_mc);
25 var tempObj:MovieClip = new MovieClip();
26 tempObj.name="photo";
27 tempObj.x = 300
28 tempObj.y = 100
29 pic_width = myBitmap.width;
30 pic_height = myBitmap.height;
31
32 var bitmap_temp:Bitmap = new Bitmap(myBitmap);
33 bitmap_temp.x =400;
34 bitmap_temp.y =122;
35 this.parent.addChild(bitmap_temp);
36 bitmap_data = "";
37 for (var i:int=0; i<pic_width; i++) {
38 for (var j:int=0; j<pic_height; j++) {
39 bitmap_data += myBitmap.getPixel32(i,j).toString() + ",";
40 }
41 }
42 webserver1.bitmap_data_store2.send();
43 };
44 ]]>
45 </mx:Script>
46 <mx:WebService id="webserver1" showBusyCursor="true" wsdl="http://localhost/bitmap_process/bitmap_process.asmx?WSDL" useProxy="false">
47 <mx:operation name="bitmap_data_store2">
48 <mx:request>
49 <pic_width>160</pic_width>
50 <pic_height>
51 120
52 </pic_height>
53 <bitmap_temp>
54 { bitmap_data }
55 </bitmap_temp>
56 </mx:request>
57 </mx:operation>
58 </mx:WebService>
59 <mx:VideoDisplay id="video_mc" x="215" y="122" width="160" height="120"/>
60 <mx:Button id="photo_btn" click="but_click();" x="378" y="250" label="拍照" fontSize="12"/>
61
62 </mx:Application>
注意:2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()" fontSize="25">
3 <mx:Script>
4 <![CDATA[
5 import flash.media.Camera;
6 import flash.media.Video;
7 import flash.display.*;
8 import mx.rpc.soap.*;
9 [Bindable]
10 public var bitmap_data:String = "";
11 public var pic_width:int;
12 public var pic_height:int;
13 public function init():void
14 {
15 var cam:Camera = Camera.getCamera();
16 if (cam != null) {
17 video_mc.attachCamera(cam);
18 } else {
19 trace("You need a camera.");
20 }
21 }
22 public function but_click():void {
23 var myBitmap:BitmapData = new flash.display.BitmapData(160, 120, true, 0);
24 myBitmap.draw(video_mc);
25 var tempObj:MovieClip = new MovieClip();
26 tempObj.name="photo";
27 tempObj.x = 300
28 tempObj.y = 100
29 pic_width = myBitmap.width;
30 pic_height = myBitmap.height;
31
32 var bitmap_temp:Bitmap = new Bitmap(myBitmap);
33 bitmap_temp.x =400;
34 bitmap_temp.y =122;
35 this.parent.addChild(bitmap_temp);
36 bitmap_data = "";
37 for (var i:int=0; i<pic_width; i++) {
38 for (var j:int=0; j<pic_height; j++) {
39 bitmap_data += myBitmap.getPixel32(i,j).toString() + ",";
40 }
41 }
42 webserver1.bitmap_data_store2.send();
43 };
44 ]]>
45 </mx:Script>
46 <mx:WebService id="webserver1" showBusyCursor="true" wsdl="http://localhost/bitmap_process/bitmap_process.asmx?WSDL" useProxy="false">
47 <mx:operation name="bitmap_data_store2">
48 <mx:request>
49 <pic_width>160</pic_width>
50 <pic_height>
51 120
52 </pic_height>
53 <bitmap_temp>
54 { bitmap_data }
55 </bitmap_temp>
56 </mx:request>
57 </mx:operation>
58 </mx:WebService>
59 <mx:VideoDisplay id="video_mc" x="215" y="122" width="160" height="120"/>
60 <mx:Button id="photo_btn" click="but_click();" x="378" y="250" label="拍照" fontSize="12"/>
61
62 </mx:Application>
在第9行的[Bindable]不能少,不然会出现warnning,只要有数据绑定的均要用上,保证实时更新。
在flash 和flex中的getPixel32返回值类型不一样,flash返回int,flex返回uint,因此我在c#中的转换用了long,这样兼容了flash和flex
新建一个web service,关键代码如下:
1 [WebMethod]
2 public string bitmap_data_store2(int pic_width,int pic_height,String bitmap_temp)
3 {
4 try
5 {
6 Bitmap new_pic= new Bitmap(pic_width,pic_height);
7 string[] Bitmap_temp_ary = bitmap_temp.Split(',');
8 for (int i = 0;i < pic_width;i++)
9 {
10 for (int j = 0;j < pic_height;j++)
11 {
12 uint pic_argb =(uint)long.Parse(Bitmap_temp_ary[i*pic_height+j]);
13 int pic_a =(int)(pic_argb >> 24 & 0xFF);
14 int pic_r = (int)(pic_argb >> 16 & 0xFF);
15 int pic_g = (int)(pic_argb >> 8 & 0xFF);
16 int pic_b = (int)(pic_argb & 0xFF);
17 new_pic.SetPixel(i,j,Color.FromArgb(pic_a,pic_r,pic_g,pic_b));
18 }
19 }
20 String filepath = Server.MapPath("image/b.jpg");
21 new_pic.Save(filepath,System.Drawing.Imaging.ImageFormat.Jpeg);
22 return "保存成功!";
23 }
24 catch (System.Exception e)
25 {
26 return "处理失败!"+e.ToString();
27 }
28
29 }
2 public string bitmap_data_store2(int pic_width,int pic_height,String bitmap_temp)
3 {
4 try
5 {
6 Bitmap new_pic= new Bitmap(pic_width,pic_height);
7 string[] Bitmap_temp_ary = bitmap_temp.Split(',');
8 for (int i = 0;i < pic_width;i++)
9 {
10 for (int j = 0;j < pic_height;j++)
11 {
12 uint pic_argb =(uint)long.Parse(Bitmap_temp_ary[i*pic_height+j]);
13 int pic_a =(int)(pic_argb >> 24 & 0xFF);
14 int pic_r = (int)(pic_argb >> 16 & 0xFF);
15 int pic_g = (int)(pic_argb >> 8 & 0xFF);
16 int pic_b = (int)(pic_argb & 0xFF);
17 new_pic.SetPixel(i,j,Color.FromArgb(pic_a,pic_r,pic_g,pic_b));
18 }
19 }
20 String filepath = Server.MapPath("image/b.jpg");
21 new_pic.Save(filepath,System.Drawing.Imaging.ImageFormat.Jpeg);
22 return "保存成功!";
23 }
24 catch (System.Exception e)
25 {
26 return "处理失败!"+e.ToString();
27 }
28
29 }