Entries RSS
image


Who is reading My blog

SPONSORS

新浪围脖

01

PhotoWall


PhotoWall,本来想用PV3D来实现照片效果的,PV3D用的不好,所以还是选择了个简单的。

做这个主要是继续学习AMFPHP,这个程序可以通过AMFPHP将图片保存到服务器,用起来还是非常方便的。

PHP端代码:
file_put_contents保存图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!--p header("Content-Type:text/html;charset=utf-8"); define( "DATABASE_SERVER", "localhost"); define( "DATABASE_USERNAME", "root"); define( "DATABASE_PASSWORD", "123456"); define( "DATABASE_NAME", "amfphp"); class PictureWallServer { 	var $output_dir = "temp"; 	 	var $server_url = "http://127.0.0.1/amfphp/services/com/ZhangHaoyun/PictureWall/"; 	 	var $_db; 	public function __construct() 	{ 		try      	 {               $thi-->_db = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);
			  mysql_query("set names UTF8");
              mysql_select_db(DATABASE_NAME, $this-&gt;_db);
          }
          catch (Exception $exc)
          {
              return 'ERROR #1: Could not connect to the database';
          }
	}
 
	/**
	 * Save image from the given bytearray
	 * and return the path of the saved image
	 */
 
	function save($ba, $title)
	{
		if(!file_exists($this-&gt;output_dir) || !is_writeable($this-&gt;output_dir))
			trigger_error ("please create a 'temp' directory first with write access", E_USER_ERROR);
 
		$data = $ba-&gt;data;
		$title = "/".$title.".jpg";
 
		file_put_contents($this-&gt;output_dir .$title, $data);
 
		$picurl = $this-&gt;server_url .$this-&gt;output_dir .$title;
 
		$ip=$_SERVER["REMOTE_ADDR"];
 
		$sql="INSERT INTO picwall ( picurl,ip,time) VALUES ('$picurl', '$ip', now());";
 
		$rs=mysql_query($sql, $this-&gt;_db);
		if($rs)
		{
			return "sucess\n".$picurl."\n".$ip;
		}
		else
		{
			return "failed";
		}
	}
 
	/**
	 *Load image url from MySql
	 */
	function load()
	{
		$sql = "SELECT * FROM picwall";
		$rs = mysql_query($sql, $this-&gt;_db);
		return $rs;
	}
 
}
 
?&gt;

Flash端给出获取摄像头图像并发送的代码,其余的自行下载查看,就不占用地方了。

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.zhanghaoyun.picturewall.view
{
	import com.adobe.images.JPGEncoder;
	import com.adobe.images.PNGEncoder;
	import com.zhanghaoyun.picturewall.events.PicWallEvent;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.Responder;
	import flash.ui.Mouse;
	import flash.utils.ByteArray;
	import flash.events.EventDispatcher;
	/**
	 * ...
	 * @author ZhangHaoyun
	 * @web http://www.zhanghaoyun.com
	 */
 
	public class GetImage extends MovieClip
	{
		private var video:Video;
		private var camera:Camera;
		private var w:int;
		private var h:int;
 
		private var bmpd:BitmapData;
		private var bmp:Bitmap;
 
		private var gateway:String = "http://127.0.0.1/amfphp/gateway.php";
 
		private var connection:NetConnection = new NetConnection();
		private var responderSet:Responder;
 
		private var _title:int;
 
		public function GetImage()
		{
			w = vs.width;
			h = vs.height;
			//tip.visible = false;
 
			video = new Video(w, h);
 
			camera = Camera.getCamera();
 
			if (camera == null)
			{
				trace("找不到摄像头");
				tip.visible = true;
				tip.text = "找不到摄像头";
			}
			else
			{
				trace("找到摄像头:" + camera.name);
				camera.setMode(w ,h ,50 ,true);
				video.attachCamera(camera);
				vs.addChild(video);
 
				tip.text = "点击拍摄获取照片";
				btn_pai.addEventListener(MouseEvent.CLICK, paishe);
 
				connection.connect(gateway);
				responderSet = new Responder(onResultSet, onFault);
			}
 
			btnReturn.addEventListener(MouseEvent.CLICK, returnMain);
		}
 
		private function returnMain(e:MouseEvent):void
		{
			var pe:PicWallEvent = new PicWallEvent(PicWallEvent.RETURN);
			dispatchEvent(pe);
			clear();
		}
 
		private function onResultSet(result:Object):void
		{
			trace("sendOk");
			var pe:PicWallEvent = new PicWallEvent(PicWallEvent.PIC_SEND_OK);
			dispatchEvent(pe);
			tip.text = "";
		}
 
		private function onFault(fault:Object):void
		{
			trace("Fault");
			for(var i in fault)
			{
				trace(i + "=&gt;" + fault[i]);
			}
			tip.text = "send fail";
		}
 
		private function paishe(e:MouseEvent):void
		{
			tip.text = "点击确定上传或者从拍";
			getImage();
			btn_pai.removeEventListener(MouseEvent.CLICK, paishe)
			btn_chongPai.addEventListener(MouseEvent.CLICK, chongPai);
			btn_upload.addEventListener(MouseEvent.CLICK, upload);
		}
 
		private function chongPai(e:MouseEvent):void
		{
			tip.text = "点击拍摄获取照片";
			image.removeChildAt(0);
			btn_pai.addEventListener(MouseEvent.CLICK, paishe)
			btn_chongPai.removeEventListener(MouseEvent.CLICK, chongPai);
		}
 
		private function getImage():void
		{
			bmpd = new BitmapData(w, h, true, 0);
			bmpd.draw(video);
			bmp = new Bitmap(bmpd);
			image.addChild(bmp);
		}
 
		private function upload(e:MouseEvent):void
		{
			var jpgEncoder:JPGEncoder = new JPGEncoder();
			var bytes:ByteArray = jpgEncoder.encode(bmpd);
			clear();
			tip.text = "sending..."
			connection.call("com.ZhangHaoyun.PictureWall.PictureWallServer.save", responderSet, bytes, _title);
		}
 
		public function set title(value:int):void
		{
			_title = value;
		}
 
		private function clear():void
		{
			camera = Camera.getCamera(null);
			camera = null;
			video.attachCamera(null);
		}
 
	}
 
}

只是练习AMFPHP的通信,代码没有优化,想到什么就写的什么,也难免有BUG,请见谅。
点击下载该示例文件

23

Flex+AMFPHP留言本

AMFPHP一个很不错的东西,拿来试了下手,弄了个简单Flex+AMFPHP留言本,用起来真的很方便,后台也很不错。以后得多学习点PHP了。

实例下载 包含PHP脚本以及数据库文件

22

AMFPHP使用常见问题

1.乱码问题 如果你发现你的程序读取的数据是乱码,那么首先你试下发送到数据库里的数据是否是乱码,如果是则现在数据库里整理字符集,如果不是那就打开gateway.php,

$gateway->setCharsetHandler(“utf8_decode”, “ISO-8859-1″, “ISO-8859-1″)

修改为

$gateway->setCharsetHandler( “utf8_decode”, “UTF-8″, “UTF-8″);

最后在你写的PHP文件的读取方法里加入 mysql_query(“set names UTF8″);

这样就应该解决问题了。

2.如果你使用的是godaddy的空间,那么你会发现AMFPHP在godaddy上安装时会报错,怎么解决这个问题呢?删掉.htaccess就可以了,不知道国内用godaddy的人多不。

3.报错,我在使用Flash IDE时报了这样一个错误Error #2044: Unhandled NetStatusEvent:: level=error, 
code=NetConnection.Call.BadVersion at Main()  

应该是安全检测错误,找到gateway.php中的

//Disable profiling, remote tracing, and service browser
  $gateway->disableDebug();
  // Keep the Flash/Flex IDE player from connecting to the gateway. Used for security to stop remote connections. 
  $gateway->disableStandalonePlayer();

将其注释掉就可以了。