这段时间工作忙,一直抽不出空来写点内容。今天晚上正好能静下心来,把工作中用到Flash位图数据保存整理一下,写了下面的应用实例。
Flash客户端代码(test12.swf)
- package project.test {
-
- import flash.display.*;
- import flash.net.*;
- import flash.events.*;
- import flash.utils.ByteArray;
-
- import fl.controls.Button;
-
- import com.klstudio.images.JPGEncoder;
-
- public class Test12 extends Sprite{
- private var _base:Shape;
- private var _rnd_button:Button;
- private var _submit_button:Button;
-
- private static const UPLOAD_PAGE:String="http://localhost:8080/upload.jsp";
-
- public function Test12(){
- _base = new Shape();
- _base.x = _base.y = 5;
- addChild(_base);
- drawLine();
-
- _rnd_button = new Button();
- _rnd_button.label = "随机画图";
- _rnd_button.setSize(90,22);
- _rnd_button.move(5,205);
- _rnd_button.addEventListener(MouseEvent.CLICK,clickHandler);
- addChild(_rnd_button);
-
- _submit_button = new Button();
- _submit_button.label = "保存图片";
- _submit_button.setSize(90,22);
- _submit_button.move(105,205);
- _submit_button.addEventListener(MouseEvent.CLICK,clickHandler);
- addChild(_submit_button);
- }
-
- public function drawRndRect():void{
- _base.graphics.beginFill(getRndColor());
- var gWidth:uint = getRndInt();
- var gHeight:uint = getRndInt();
- var gX:uint = getRndInt(5,180-gWidth+5);
- var gY:uint = getRndInt(5,180-gHeight+5);
- _base.graphics.drawRect(gX,gY,gWidth,gHeight);
- }
-
- public function getRndInt(min:uint=10,max:uint=180):uint{
- return uint(Math.random()*(max-min))+min;
- }
-
- public function getRndColor():uint{
- var r:uint = getRndInt(10,220);
- var g:uint = getRndInt(10,220);
- var b:uint = getRndInt(10,220);
- var c:uint = (r << 16) + (g << 8) + b;
- return c;
- }
-
- public function drawLine():void{
- _base.graphics.beginFill(0x666666);
- _base.graphics.drawRect(0,0,190,190);
- _base.graphics.beginFill(0xFFFFFF);
- _base.graphics.drawRect(1,1,188,188);
- _base.graphics.endFill();
- }
-
- public function getBitmapData():BitmapData{
- var bmd:BitmapData = new BitmapData(_base.width,_base.height);
- bmd.draw(_base);
- return bmd;
- }
-
- public function submit():void{
- _rnd_button.enabled = false;
- _submit_button.enabled = false;
- var encoder:JPGEncoder = new JPGEncoder(80);
- var bytes:ByteArray = encoder.encode(getBitmapData());
- var request:URLRequest = new URLRequest(UPLOAD_PAGE);
- //data值就为图片编码数据ByteArray;
- request.data = bytes;
- request.method = URLRequestMethod.POST;
- //这个是关键,内容类型必须是下面文件流形式;
- request.contentType = "application/octet-stream";
-
- var loader:URLLoader = new URLLoader();
- loader.addEventListener(Event.COMPLETE, completeHandler);
- loader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
- loader.load(request);
- }
-
- private function completeHandler(event:Event):void{
- trace("图片上传成功");
- _rnd_button.enabled = true;
- _submit_button.enabled = true;
- }
-
- private function errorHandler(event:IOErrorEvent):void{
- trace("图片上传失败");
- _rnd_button.enabled = true;
- _submit_button.enabled = true;
- }
-
- private function clickHandler(event:MouseEvent):void{
- switch(event.target){
- case _rnd_button:
- _base.graphics.clear();
- drawLine();
- drawRndRect();
- drawRndRect();
- drawRndRect();
- drawRndRect();
- break;
- case _submit_button:
- submit();
- break;
- }
- }
- }
- }
package project.test {<br/> <br/> import flash.display.*; <br/> import flash.net.*;<br/> import flash.events.*;<br/> import flash.utils.ByteArray;<br/> <br/> import fl.controls.Button;<br/> <br/> import com.klstudio.images.JPGEncoder;<br/> <br/> public class Test12 extends Sprite{<br/> private var _base:Shape;<br/> private var _rnd_button:Button;<br/> private var _submit_button:Button;<br/> <br/> private static const UPLOAD_PAGE:String="http://localhost:8080/upload.jsp"; <br/> <br/> public function Test12(){ <br/> _base = new Shape();<br/> _base.x = _base.y = 5;<br/> addChild(_base); <br/> drawLine(); <br/> <br/> _rnd_button = new Button();<br/> _rnd_button.label = "随机画图";<br/> _rnd_button.setSize(90,22);<br/> _rnd_button.move(5,205);<br/> _rnd_button.addEventListener(MouseEvent.CLICK,clickHandler);<br/> addChild(_rnd_button);<br/> <br/> _submit_button = new Button();<br/> _submit_button.label = "保存图片";<br/> _submit_button.setSize(90,22);<br/> _submit_button.move(105,205);<br/> _submit_button.addEventListener(MouseEvent.CLICK,clickHandler);<br/> addChild(_submit_button);<br/> }<br/> <br/> public function drawRndRect():void{<br/> _base.graphics.beginFill(getRndColor());<br/> var gWidth:uint = getRndInt();<br/> var gHeight:uint = getRndInt();<br/> var gX:uint = getRndInt(5,180-gWidth+5);<br/> var gY:uint = getRndInt(5,180-gHeight+5);<br/> _base.graphics.drawRect(gX,gY,gWidth,gHeight); <br/> }<br/> <br/> public function getRndInt(min:uint=10,max:uint=180):uint{<br/> return uint(Math.random()*(max-min))+min;<br/> }<br/> <br/> public function getRndColor():uint{<br/> var r:uint = getRndInt(10,220);<br/> var g:uint = getRndInt(10,220);<br/> var b:uint = getRndInt(10,220);<br/> var c:uint = (r << 16) + (g << 8) + b;<br/> return c;<br/> }<br/> <br/> public function drawLine():void{ <br/> _base.graphics.beginFill(0x666666);<br/> _base.graphics.drawRect(0,0,190,190);<br/> _base.graphics.beginFill(0xFFFFFF);<br/> _base.graphics.drawRect(1,1,188,188);<br/> _base.graphics.endFill();<br/> }<br/> <br/> public function getBitmapData():BitmapData{<br/> var bmd:BitmapData = new BitmapData(_base.width,_base.height);<br/> bmd.draw(_base);<br/> return bmd;<br/> }<br/> <br/> public function submit():void{<br/> _rnd_button.enabled = false;<br/> _submit_button.enabled = false;<br/> var encoder:JPGEncoder = new JPGEncoder(80);<br/> var bytes:ByteArray = encoder.encode(getBitmapData());<br/> var request:URLRequest = new URLRequest(UPLOAD_PAGE);<br/> //data值就为图片编码数据ByteArray;<br/> request.data = bytes;<br/> request.method = URLRequestMethod.POST;<br/> //这个是关键,内容类型必须是下面文件流形式;<br/> request.contentType = "application/octet-stream";<br/> <br/> var loader:URLLoader = new URLLoader(); <br/> loader.addEventListener(Event.COMPLETE, completeHandler);<br/> loader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);<br/> loader.load(request);<br/> }<br/> <br/> private function completeHandler(event:Event):void{<br/> trace("图片上传成功");<br/> _rnd_button.enabled = true;<br/> _submit_button.enabled = true;<br/> }<br/> <br/> private function errorHandler(event:IOErrorEvent):void{<br/> trace("图片上传失败");<br/> _rnd_button.enabled = true;<br/> _submit_button.enabled = true;<br/> }<br/> <br/> private function clickHandler(event:MouseEvent):void{<br/> switch(event.target){<br/> case _rnd_button:<br/> _base.graphics.clear();<br/> drawLine();<br/> drawRndRect();<br/> drawRndRect();<br/> drawRndRect();<br/> drawRndRect();<br/> break;<br/> case _submit_button:<br/> submit();<br/> break;<br/> }<br/> }<br/> } <br/>}<br/>服务端代码(upload.jsp):我使用是Java服务端,当然PHP,ASP.NET也可以了,我在这里就不一一举例了,有兴趣的朋友自己研究了!
- <%@ page contentType="text/html; charset=utf-8" language="java"%>
- <%@ page import="java.util.*"%>
- <%@ page import="java.io.*"%>
- <%
- int v;
- String filePath = request.getRealPath(System.currentTimeMillis()+".jpg");
- BufferedInputStream inputStream = new BufferedInputStream(request.getInputStream());
- FileOutputStream outputStream = new FileOutputStream(new File(filePath));
- byte [] bytes = new byte[1024];
- while((v=inputStream.read(bytes))>0){
- outputStream.write(bytes,0,v);
- }
- outputStream.close();
- inputStream.close();
- %>
<%@ page contentType="text/html; charset=utf-8" language="java"%><br/><%@ page import="java.util.*"%><br/><%@ page import="java.io.*"%><br/><%<br/>int v;<br/>String filePath = request.getRealPath(System.currentTimeMillis()+".jpg");<br/>BufferedInputStream inputStream = new BufferedInputStream(request.getInputStream());<br/>FileOutputStream outputStream = new FileOutputStream(new File(filePath));<br/>byte [] bytes = new byte[1024];<br/>while((v=inputStream.read(bytes))>0){<br/> outputStream.write(bytes,0,v);<br/>}<br/>outputStream.close();<br/>inputStream.close();<br/>%><br/>相关实例文件打包下载:
http://www.klstudio.com/demo/test/test12.rar