actionScript 3.0 图片裁剪及旋转

  1. package com.wdxc {
  2.     
  3.     /**
  4.     * ... 
  5.     * @author luqinglong
  6.     */
  7.     public class MouseInfo {
  8.         /*鼠标左边缘、右边缘等*/
  9.         private var _position:uint = 100 ;
  10.          
  11.         /*鼠标状态 按下、放开*/   
  12.         private var _state :uint = 100;  
  13.         
  14.         public function MouseInfo() {
  15.             
  16.         }
  17.           
  18.         public function set position(p:uint):void {
  19.             this._position = p ;
  20.         }
  21.         public function get position():uint {
  22.             return this._position ;
  23.         } 
  24.         
  25.         public function set state(s:uint):void {
  26.             this._state = s  ;
  27.         } 
  28.         public function get state():uint {
  29.             return _state ;
  30.         }
  31.              
  32.         /**  
  33.          * 是否满足对左边进行拖动拉伸的条件。
  34.          * @return boolean
  35.          */    
  36.         public function isStartLeftDrag():Boolean {
  37.             if (_position == Constant.LEFT && _state == Constant.MOUSE_DOWN) {
  38.                 return true ;
  39.             }
  40.             return false ;
  41.         }
  42.         
  43.         /**      
  44.          * 是否满足对右边界进行拖动拉伸的条件。
  45.          * @return boolean
  46.          */    
  47.         public function isStartRightDrag():Boolean {
  48.             if (_position == Constant.RIGHT && _state == Constant.MOUSE_DOWN) {
  49.                 return true ;
  50.             } 
  51.             return false ;
  52.         }
  53.         
  54.         /**      
  55.          * 是否满足对上边界进行拖动拉伸的条件。
  56.          * @return boolean
  57.          */        
  58.         public function isStartUpDrag():Boolean {
  59.             if (_position == Constant.UP && _state == Constant.MOUSE_DOWN) {
  60.                 return true ;
  61.             } 
  62.             return false ;   
  63.         }
  64.          
  65.         /**      
  66.          * 是否满足对下边界进行拖动拉伸的条件。
  67.          * @return boolean
  68.          */       
  69.         public function isStartDownDrag():Boolean {
  70.             if (_position == Constant.DOWN && _state == Constant.MOUSE_DOWN) {
  71.                 return true ;
  72.             } 
  73.             return false ;
  74.         }
  75.         /**       
  76.          * 是否满足对裁剪区域进行拖动拉伸的条件。
  77.          * @return boolean
  78.          */           
  79.         public function isStartMiddleDrag():Boolean {
  80.             if (_position == Constant.MIDDLE && _state == Constant.MOUSE_DOWN) {
  81.                 return true ;
  82.             } 
  83.             return false ;
  84.         }
  85.         /**        
  86.          * 是否满足对左上角拖动拉伸的条件。
  87.          * @return boolean
  88.          */           
  89.         public function isStratLeftUPDrag():Boolean {
  90.             if (_position == Constant.LEFT_UP && _state == Constant.MOUSE_DOWN) {
  91.                 return true ;
  92.             } 
  93.             return false ;
  94.         }   
  95.         /**         
  96.          * 是否满足对左下角拖动拉伸的条件。
  97.          * @return boolean
  98.          */            
  99.         public function isStratLeftDownDrag():Boolean {
  100.             if (_position == Constant.LEFT_DOWN && _state == Constant.MOUSE_DOWN) {
  101.                 return true ;
  102.             } 
  103.             return false ;
  104.         }   
  105.         /**        
  106.          * 是否满足对右上角拖动拉伸的条件。
  107.          * @return boolean
  108.          */            
  109.         public function isStratRightUPDrag():Boolean {
  110.             if (_position == Constant.RIGHT_UP && _state == Constant.MOUSE_DOWN) {
  111.                 return true ;
  112.             } 
  113.             return false ;
  114.         }   
  115.         /**         
  116.          * 是否满足对右下角拖动拉伸的条件。
  117.          * @return boolean
  118.          */            
  119.         public function isStratRightDownDrag():Boolean {
  120.             if (_position == Constant.RIGHT_DOWN && _state == Constant.MOUSE_DOWN) {
  121.                 return true ;
  122.             } 
  123.             return false ;
  124.         }           
  125.     } 
  126.     
  127. }

代码一:

  1. package 
  2.     import com.wdxc.ImageCut;
  3.     import com.wdxc.ImageRotation;
  4.     import flash.geom.Point;
  5.     import flash.geom.Rectangle;
  6.     import flash.net.URLLoader;
  7.     import flash.net.URLRequest;
  8.     import flash.events.* ;
  9.     import flash.display.*;
  10.     import flash.geom.* ;
  11.     import flash.net.*;
  12.     import flash.text.TextField;
  13.     import flash.utils.ByteArray;
  14.     import com.wdxc.util.* ;      
  15.     import fl.containers.ScrollPane;
  16.     import flash.display.Shape;
  17.     import fl.events.ScrollEvent;
  18.     import flash.external.ExternalInterface;
  19.     import flash.system.Security;
  20.     import flash.utils.Timer;
  21.     import flash.events.TimerEvent ;
  22.     import com.wdxc.* ;
  23.     
  24.     
  25.     /**  
  26.      * @author luqinglong
  27.      */ 
  28.     public class Main extends MovieClip
  29.     {
  30.         //存储加载进来的图片数据
  31.         private var _bitmap:Bitmap ;               
  32.         //进度条
  33.         private var _progressbar:TextField ;
  34.         
  35.         private var picname:String ; 
  36.             
  37.         public var _imageCut:ImageCut = new ImageCut() ;
  38.                
  39.         //场景引用
  40.         public static var SCENE:MovieClip ;
  41.            
  42.         public static var _handerState:int = 0 ;
  43.                
  44.             
  45.         public var pelsHint:TextField = new TextField(); 
  46.                        
  47.         public var photoid :String = "0"
  48.          
  49.         public var ir:ImageRotation ; 
  50.             
  51.         /**
  52.          * 原图和中图的压缩比例  
  53.          */    
  54.         public var picScale :Number = 2 ;
  55.                
  56.            
  57.         public function Main():void {           
  58.             flash.system.Security.allowDomain("*");         
  59.             
  60.             photoid = this.loaderInfo.parameters['photoid'];   
  61.                     
  62.             //photoid = "284186" ;    
  63.            
  64.             SCENE = this;           
  65.             
  66.             menu_mc.cut_btn.addEventListener(MouseEvent.MOUSE_DOWN, this._imageCut.startCutPic);
  67.          
  68.             menu_mc.rotate_btn.addEventListener(MouseEvent.MOUSE_DOWN, rotationHandler) ;  
  69.         
  70.             checkAvailable();   
  71.                   
  72.             this.addPelsHint();     
  73.             startLoadPic();       
  74.         }   
  75.                
  76.         /**
  77.          * 加入象素提醒  
  78.          */
  79.         private function addPelsHint():void {
  80.             this.addChild(pelsHint);      
  81.             //pelsHint.text = "象素不够" ;
  82.             pelsHint.x = 150 ; 
  83.             pelsHint.y = 0 ;                      
  84.             pelsHint.height = 20 ;           
  85.             this.pelsHint.width = 350 ; 
  86.             pelsHint.textColor =0xFF0000;
  87.         }
  88.           
  89.         public function setPelsHint(p:Number,hint:String):void {
  90.                                  
  91.             this.pelsHint.text = "像素:" + String(p) + "  " + hint  ;     
  92.                
  93.         }       
  94.            
  95.         private function rotationHandler(e:MouseEvent):void {    
  96.             ir = new ImageRotation() ;  
  97.             ir.x = menu_mc.x ;
  98.             ir.y = menu_mc.y ;     
  99.             this.stage.addChild(ir);    
  100.         }   
  101.         
  102.           
  103.         /**
  104.          * 得到最低象素值 。
  105.          */
  106.         /*
  107.         public function getMaxPels():Number {
  108.   
  109.             return this.plength/ 500 ;
  110.         }  */
  111.           
  112.         //public function setPelsScale(p:Number) {
  113.             //this.plength = p / 500 ;
  114.         //}
  115.               
  116.         
  117.           
  118.         public function set bitmap(bp:Bitmap):void {
  119.             this._bitmap = bp ;
  120.         } 
  121.             
  122.         public function get bitmap():Bitmap {
  123.             return this._bitmap ;
  124.         }
  125.                 
  126.         public function startLoadPic():void {
  127.                              
  128.             var param:URLVariables = new URLVariables() ; 
  129.             var url:String = "/photo/LoaderPhotoServlet";      
  130.             //var url:String = "http://localhost:8080//photo/LoaderPhotoServlet" ;
  131.                
  132.             param.random = Math.floor(Math.random() * 1000);                 
  133.             param.photoid = photoid ;           
  134.             //output.text = photoid ;
  135.             //url += Math.floor(Math.random() * 1000);
  136.             
  137.                 
  138.             var request:URLRequest = new URLRequest(url);   
  139.             request.data = param ;
  140.             var loader :Loader = new Loader();
  141.             loader.load(request);      
  142.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, picLoadCompleteHandler);
  143.         } 
  144.         /**  
  145.          * 导入二进制完成
  146.          * @param   e    
  147.          */    
  148.         private function picLoadCompleteHandler(e:Event):void {
  149.                
  150.             /**删除进度条*/    
  151.             if (Main._handerState == 1||Main._handerState==2) {
  152.                  this.delVagueBg();    
  153.                  delLoadingBar();
  154.                  calljs();
  155.             }           
  156.             Main._handerState = 0 ;
  157.               
  158.             /*
  159.             if (this._bitmap.bitmapData != null) {
  160.                 
  161.                 this._bitmap.bitmapData.dispose() ;   
  162.                 //this.removeChild(_bitmap);
  163.             } */          
  164.             
  165.             this._bitmap =  new Bitmap() ;          
  166.                      
  167.             var loader:Loader = e.target.loader as Loader;        
  168.               
  169.             var _bitmapdata:BitmapData = new BitmapData(loader.width, loader.height);
  170.             _bitmapdata.draw(loader); 
  171.                 
  172.                  
  173.             /**
  174.              * 判断横版照片,竖版照片
  175.              */ 
  176.             /*
  177.             if ( loader.height > loader.width) {
  178.                 //竖版照片  
  179.                 this.cutscaleFlag = Constant.VERTICAL ;    
  180.             } */  
  181.                
  182.                   
  183.                                 
  184.             this._bitmap.bitmapData = _bitmapdata;               
  185.             this.addChild(_bitmap);  
  186.             this.reset() ;       
  187.             //设置原图引用                 
  188.             this._imageCut.bitmap = this._bitmap ;                   
  189.             this.addChild(this._imageCut);
  190.                
  191.             e.currentTarget.removeEventListener(Event.COMPLETE, picLoadCompleteHandler); 
  192.                   
  193.         }     
  194.           
  195.            
  196.         
  197.         /** 
  198.          * 删除以前的Bitmapt对象,注入新的进去 。
  199.          */
  200.         public function removeBitmap():void {
  201.                      
  202.             this.removeChild(_bitmap);
  203.         }        
  204.         public function reset():void {  
  205.             this._bitmap.x = (Constant.LEFT_AREA_WIDTH-_bitmap.width)/2 ;
  206.             this._bitmap.y = (Constant.SCRENE_HEIGHT - _bitmap.height) / 2 ;
  207.         }
  208.         
  209.         public function checkAvailable():void {
  210.             if (ExternalInterface.available){
  211.                 try {    
  212.                     ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript);
  213.                     if (checkJavaScriptReady()) {
  214.                         //trace("JavaScript is ready.\n");
  215.                     } else { 
  216.                                     
  217.                         //trace("JavaScript is not ready, creating timer.\n");
  218.                         var readyTimer:Timer = new Timer(100, 0);
  219.                         readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
  220.                         readyTimer.start();       
  221.                     }
  222.                 }catch (error:SecurityError) {
  223.                     //trace("A SecurityError occurred: " + error.message + "\n");
  224.                     //output.text = "error1";
  225.                 }catch (error:Error) {  
  226.                     
  227.                     //trace("An Error occurred: " + error.message + "\n");
  228.                 }   
  229.             }else{   
  230.                 //trace("External interface is not available for this container.");
  231.             }
  232.          
  233.         } 
  234.          
  235.         private function timerHandler(event:TimerEvent):void {
  236.            
  237.             var isReady:Boolean = checkJavaScriptReady(); 
  238.             //output.appendText("123");
  239.             if (isReady) {              
  240.                 //trace("JavaScript is ready.\n"); 
  241.                 Timer(event.target).stop();
  242.             }
  243.         }
  244.                   
  245.                               
  246.         private function receivedFromJavaScript(pid:String, plength:Number):void {       
  247.                             
  248.             //pelsHint.text = pid + "==" + plength ;
  249.             this.photoid = pid ;      
  250.               
  251.             //设置中图和原图的像素比
  252.             this.picScale = plength/500 ;     
  253.               
  254.             //this._bitmap.bitmapData.dispose();
  255.             //output.text = value ;      
  256.             removeBitmap();        
  257.             // 点击了裁剪按钮时
  258.             if (Main._handerState == 5) {
  259.                 _imageCut.removeCutMenu();    
  260.                  
  261.             }    
  262.             // 裁剪时候单击保存时候 
  263.             else if (Main._handerState == 6) {
  264.                 _imageCut.removeCutMenu();    
  265.                 _imageCut.removeHint();              
  266.             } else if (Main._handerState == 10) {
  267.                 ir.remove();
  268.             }  
  269.             
  270.      
  271.             
  272.             
  273.             //this._bitmap = null ;
  274.             startLoadPic();        
  275.         }
  276.         private function checkJavaScriptReady():Boolean {
  277.             var isReady:Boolean = ExternalInterface.call("isReady");
  278.             return isReady;
  279.         } 
  280.                   
  281.         public function calljs():void {
  282.             if (ExternalInterface.available) {     
  283.                 ExternalInterface.call("rationAndCropComplete""123");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值