

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->_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->output_dir) || !is_writeable($this->output_dir)) trigger_error ("please create a 'temp' directory first with write access", E_USER_ERROR); $data = $ba->data; $title = "/".$title.".jpg"; file_put_contents($this->output_dir .$title, $data); $picurl = $this->server_url .$this->output_dir .$title; $ip=$_SERVER["REMOTE_ADDR"]; $sql="INSERT INTO picwall ( picurl,ip,time) VALUES ('$picurl', '$ip', now());"; $rs=mysql_query($sql, $this->_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->_db); return $rs; } } ?> |
Flash端给出获取摄像头图像并发送的代码,其余的自行下载查看,就不占用地方了。
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 + "=>" + 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,请见谅。
点击下载该示例文件




