[AS3]as3.0运用JPGEncoder来生成与保存图片源代码实例JSP版
[AS3]as3.0运用JPGEncoder来生成与保存图片源代码实例JSP版,写了下面的应用实例
晚上正好能静下心来,把工作中用到Flash位图数据保存整理一下,写了下面的应用实例。
- 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_base.x = _base.y = 5;
- addChild(_base);
- drawLine();
- _rnd_button = new Button();
- _rnd_button.label = "CuPlayer.com提示随机画图";
- _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 = "CuPlayer.com提示保存图片";
- _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;
- //CuPlayer.com提示这个是关键,内容类型必须是下面文件流形式;
- 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("CuPlayer.com提示图片上传成功");
- _rnd_button.enabled = true;
- _submit_button.enabled = true;
- }
- private function errorHandler(event:IOErrorEvent):void{
- trace("CuPlayer.com提示图片上传失败");
- _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;
- }
- }
- }
- }
服务端代码(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();
- %>
[AS3]as3.0运用JPGEncoder来生成与保存图片源代码实例JSP版
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...