actionscript3.0 图片裁剪及保存jpg详解

1. 客户端生成BitmapData 对象。
2. 用JPGEncoder 对其编码相应的字节数组
3 用URLRequest和URLLoader发送数据。
4.服务用request.getInputStream()接收流..
5.保存为图片格式。

package
...{
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.* ;
    import flash.display.*;
    import flash.geom.* ;
    import flash.net.*;
    import flash.utils.ByteArray;
      
    import com.wdxc.util.* ;   
        
    
    public class Main extends Sprite
    ...{
        private var _picx:int = 0 ;
        private var _picy:int = 120 ;  
        private var _picwidth = 0 ;
        private var _picheight = 0 ;
          
        private var _bitmapdata:BitmapData ;
      
 
        private var _rect:Sprite ;//裁剪区域     
        
        private var _url:String ="http://192.168.0.211/test.jsp";
    
        public function Main():void...{     
            this.loadPic("F:\a.jpg");  
            this.init();
             
        }      
         
        public function init():void ...{
            cut_btn.addEventListener(MouseEvent.MOUSE_DOWN, grabPic);
            save_btn.addEventListener(MouseEvent.MOUSE_DOWN, savePic) ;
        }

           
        /** *//****
         * 装载图片
         * @param    picurl  
         */   
        public function loadPic(picurl:String):void...{
            if (picurl == null || picurl == "")
            ...{
                return ;    
            }
            var request:URLRequest = new URLRequest(picurl);
            var loader:Loader = new Loader() ;
            loader.load(request);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHander);
                
        }
        
        /** *//**  
         * 对图片编码并提交给服务端保存。
         * @param bitmapdata 图片数据  
         */       
        public function submit(bitmapdata:BitmapData):void ...{
                 
            //每次URL不同,AS3才会提交每一次,否则将不提交。
            var url :String = _url+"?"+Math.floor(Math.random()*1000) ;  
            var request:URLRequest = new URLRequest(url);  
             
            //编码成图片格式流
            var encoder:JPGEncoder = new JPGEncoder(80);
            var bytes:ByteArray = encoder.encode(bitmapdata);      
            
            request.method = URLRequestMethod.POST;
            request.data = bytes ;   
                    
            //设置成文件流形式
            request.contentType = "application/octet-stream";

            var loader:URLLoader = new URLLoader();                  
            loader.load(request) ;        
            loader.addEventListener(Event.COMPLETE, sendCompleteHandler) ;  
        }
            
        /** *//**
         * 服务端保存完回调
         * @param    e
         */
        private function sendCompleteHandler(e:Event):void ...{
            trace("save success........");     
        }  
          
        /** *//**
         * 保存裁剪图片
         * @param    e
         */
        private function savePic(e:MouseEvent)...{
             
            //组装要裁剪区域    
            var cutpic:Rectangle = new Rectangle(_rect.x,_rect.y,_rect.width,_rect.height);
            var bitmapdata:BitmapData = this.getCutData(_bitmapdata, cutpic);  
            
            var bitmap:Bitmap = new Bitmap(bitmapdata) ;           
            bitmap.x = 100 ;
            bitmap.y = 0 ;  
            //只显示裁剪区域图片,去除其他空位置  
            this.addChild(bitmap);    
            this.submit(cutbitmapdata);       
        }   
          
        /** *//**   
         * 获取所裁剪区域的数据。所有像素信息
         * @param source        源图数据   
         * @param rect          所有裁剪的区域     
         * @return BitmapData   裁剪出新图的数据
         */
        public function getCutData(source:BitmapData,rect:Rectangle):BitmapData ...{
            //存放裁剪出的新图片
            var cutbitmapdata:BitmapData = new BitmapData(Math.floor(_rect.width),Math.ceil(_rect.height)) ;
                   
            //坐标转化,把坐标移到裁剪区域的位置,宽度和高度在cutbitmapdata里指定。
            var matrix:Matrix = new Matrix(1, 0, 0, 1, -Math.floor(_rect.x), -Math.floor(_rect.y));
             
            cutbitmapdata.draw(_bitmapdata, matrix);  
            return cutbitmapdata ;  
        }
        



        /** *//****  
         * 装载图片完成之后,显示图片到场景
         * @param    e
         */
        private function completeHander(e:Event):void ...{
            var loader:Loader = e.target.loader as Loader;
               
            this._picwidth = loader.width ;
            this._picheight = loader.height ;      
            _bitmapdata= new BitmapData(loader.width, loader.height);
            _bitmapdata.draw(loader);
            var _bitmap:Bitmap = new Bitmap(_bitmapdata);        
            _bitmap.x = this._picx;
            _bitmap.y = this._picy ;
            this.addChild(_bitmap);
        }
        
        /** *//****      
         * 生成裁剪区域框,用来抓起图片
         * @param    e     
         */    
        private function grabPic(e:MouseEvent):void
        ...{
           
            _rect= new Sprite () ;
            
            /** *//***设置裁剪区域边框样式*/
            _rect.graphics.lineStyle(3, 0xFF0000, 0.5, true, LineScaleMode.NONE, CapsStyle.ROUND);
            _rect.graphics.beginFill(0xCCFF00,0);   
            _rect.graphics.drawRect(this._picx, this._picy, this._picwidth/4,this._picheight/4);
            _rect.graphics.endFill();  
            _rect.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler) ;
            _rect.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandle) ;
            this.addChild(_rect) ;         
        }
         
        /** *//**
         * 鼠标控制裁剪区域,可拖动,拉伸。
         * @param    e
         */
        private function mouseDownHandler(e:MouseEvent):void ...{
             
            //裁剪区域移动范围,不能超过原图区域。
            var _rectangle:Rectangle = new Rectangle();
            _rectangle.width = _picwidth-_rect.width ;
            _rectangle.height = _picheight-_rect.height ;       
            _rect.startDrag(false,_rectangle);             
        }
         
        private function mouseUpHandle(e:MouseEvent):void ...{
            _rect.stopDrag();
        }
    }
}
 
服务端处理代码
<%
    
    String filePath = request.getRealPath(System.currentTimeMillis()+".jpg");
    System.out.print("filepath=="+filePath);
    BufferedInputStream inputStream = new BufferedInputStream(request.getInputStream());
    FileOutputStream outputStream = new FileOutputStream(new File(filePath));
    byte [] bytes = new byte[1024];
    
    int v;
    while((v=inputStream.read(bytes))>0){
        outputStream.write(bytes,0,v);
    }
    outputStream.close();
    inputStream.close();
%>

你可能感兴趣的:(.net,jsp,Flash,UP,中国移动)