Flexけちけちしたプログラム


ユーザーがカスタマイズした顔写真機能は、ユーザーがカスタマイズした顔写真を実現し、画像指定エリアを顔写真として選択することができます.    いろいろ考えましたが、資料を探して、やっと関連の文章を見つけました.そしてソースコードを提供しました.目には見えたが、いけないようだ.    http://www.flashas.net/html/flashasyy/20080423/2950.html  (BitmapDataケチ)AS 3が実現しました.    しかし、これはFlexの中を走ります.調整が必要です.自分の一時期のでたらめを経て…    ようやく基本機能が実現しました.8間違えました.記録は来ました.これから車輪を繰り返さないようにします.へへへ!必要な友達にも参考にしてください.    実装コードは以下の通りです.パッケージ類CutImageTest   (指定領域表示の選択)          package{
 import flash.display.*;    
 import flash.events.*;     
 import flash.filters.*;    
 import flash.geom.*;        

 import mx.controls.Image;   

  import mx.core.UIComponent;    

 /**      * @author CYPL      *           Image      */     

public class CutImageTest extends UIComponent {         

private var _imageBitmapData : BitmapData;         

private var _imageHotAreaData:BitmapData;         

private var _imageBitmap : Bitmap;         

private var _mouseRectContainer:Sprite;         

private var _mouseRectStartX:Number;        

private var _mouseRectStartY:Number;         

private var _imageClipDraging:Boolean;         

private var _currentDragClip:Sprite;                

 [Bindable]         

public var clibImg:Bitmap;//                      

public function CutImageTest( image:Image ) {                        

 _mouseRectContainer=new Sprite;             

image.visible=false;            

 _imageBitmapData=new BitmapData(image.width,image.height,true,0),_imageBitmapData.draw(image);             _imageBitmap=Bitmap(addChild(new Bitmap(_imageBitmapData)))            

 _imageBitmap.x=30            

 _imageBitmap.y=30             

configMouseEvent();             

//----------hitTestArea------------------------             

var c:ColorTransform=new ColorTransform;             

c.color=0xff0000;             

_imageHotAreaData=_imageBitmapData.clone();             

_imageHotAreaData.draw(_imageHotAreaData,null,c);         }         

private function configMouseEvent():void {            
 this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler,false,0,true);             
this.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler,false,0,true);         }         

/**************************drawRect handler*******************************/         

private function mouseDownHandler(evt:MouseEvent):void {//mouse_down            

 if (_imageClipDraging) {                 return;             }             

addChild(_mouseRectContainer);             

_mouseRectStartX=evt.stageX;             

_mouseRectStartY=evt.stageY;             

this.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);         }         

private function mouseUpHandler(evt:MouseEvent):void {//mouse_up                         

//_currentDragClip&&();            
 _imageClipDraging&&(_currentDragClip.stopDrag(),_imageClipDraging=false,_currentDragClip.alpha=1)
||(cutImage(checkIntersection()),_mouseRectContainer.graphics.clear(),this.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler))                     }         

private function mouseMoveHandler(evt:MouseEvent):void {//mouse_move             

evt.updateAfterEvent();             

var minX:Number=Math.min(evt.stageX,_mouseRectStartX)             

var minY:Number=Math.min(evt.stageY,_mouseRectStartY)             

var maxX:Number=Math.max(evt.stageX,_mouseRectStartX)             

var maxY:Number=Math.max(evt.stageY,_mouseRectStartY) 

with(_mouseRectContainer.graphics){             

clear();             

lineStyle(0);             

beginFill(0xffff00,.5);             

drawCircle( (maxX-minX)/2, (maxY-minY)/2, (maxX-minX)/2 );            
 //drawRect(0,0,maxX-minX,maxY-minY);  

           }            

 _mouseRectContainer.x=minX;            

 _mouseRectContainer.y=minY;         

}         

/************************************************************************/        
 /**************************drag  handler*******************************/         
private function clipMouseDownHandler(evt:MouseEvent):void {//mouse_down             

var target:Sprite=evt.target as Sprite;             

_currentDragClip=target;            

 _currentDragClip.alpha=.5;             

_imageClipDraging=true;             

addChild(target);             

_currentDragClip.startDrag(false);        

 }         

/************************************************************************/         

private function checkIntersection():Rectangle {             

var intersectRect:Rectangle=_imageBitmapData.rect.intersection(new Rectangle(_mouseRectContainer.x-_imageBitmap.x,_mouseRectContainer.y-_imageBitmap.y,_mouseRectContainer.width,_mouseRectContainer.height));             //trace("   BitmapData    :"+intersectRect);             

if (intersectRect.width==0 || intersectRect.height==0) {                 return null;             }            

 var bitmapData:BitmapData=new BitmapData(intersectRect.width,intersectRect.height,true,0);             bitmapData.draw(_imageHotAreaData,new Matrix(1,0,0,1,-intersectRect.x,-intersectRect.y),null,null,new Rectangle(0,0,intersectRect.width,intersectRect.height));             

var intersectHotAreaRect:Rectangle=bitmapData.getColorBoundsRect(0xFFFF0000, 0xFFFF0000,true);            

 trace("        :"+intersectHotAreaRect);            

 if (intersectHotAreaRect.width==0 || intersectHotAreaRect.height==0) {                                                 return null;             } 

 //                     

intersectHotAreaRect.x-=1             

intersectHotAreaRect.y-=1            

 intersectHotAreaRect.width+=2             

intersectHotAreaRect.height+=2             

return intersectHotAreaRect;         }         

private function cutImage(rect:Rectangle):void {//                    

if (!rect) {                 return;             }             

var clipBitmapData:BitmapData=new BitmapData(rect.width,rect.height,true,0);             

varcliptX:Number=(_mouseRectContainer.x=_mouseRectContainer.x<_imageBitmap.x?0:_mouseRectContainer.x-_imageBitmap.x)+rect.x;             varcliptY:Number=(_mouseRectContainer.y=_mouseRectContainer.y<_imageBitmap.y?0:_mouseRectContainer.y-_imageBitmap.y)+rect.y;             clipBitmapData.draw(_imageBitmapData,new Matrix(1,0,0,1,-cliptX,-cliptY),null,null,new Rectangle(0,0,rect.width,rect.height));//intersectHotAreaRect)             

var clipBitmap:Bitmap=new Bitmap(clipBitmapData);             

clibImg = clipBitmap;//              
  }    
 } 
} 
/*****************************************************MXML**********************************************/         

<?xml version="1.0" encoding="utf-8"?>

 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">     <mx:Script>         <![CDATA[             

[Bindable]             

private var cutImg:CutImageTest;             

private function init():void {                 

cutImg = new CutImageTest( image );                 

this.addChild( cutImg );             

}         

]]>     

</mx:Script>     

<mx:Image left="60" top="50" right="520" bottom="220" id="image" source="../img/11.jpg"/>     

<mx:Image  id="clibImg" source="{cutImg.clibImg}" width="200" height="200" right="50" bottom="100"/> 

</mx:Application> 



/**************************************************************************************************************/     

    CutImageTest   (      )     

package {     

import flash.display.*;     

import flash.events.*;     

import flash.filters.*;     

import flash.geom.*;         

import mx.controls.Image;     

import mx.core.UIComponent;     

/**      * @author CYPL      *           Image      */     

public class CutImageTest extends UIComponent {         

private var _imageBitmapData : BitmapData;         

private var _imageHotAreaData:BitmapData;         

private var _imageBitmap : Bitmap;         

private var _mouseRectContainer:Sprite;         

private var _mouseRectStartX:Number;         

private var _mouseRectStartY:Number;         

private var _imageClipDraging:Boolean;         

private var _currentDragClip:Sprite;                 

[Bindable]         

public var clibImg:Bitmap;//                      

public function CutImageTest( image:Image ) {                        

 _mouseRectContainer=new Sprite;             

image.visible=false;             

_imageBitmapData=new BitmapData(image.width,image.height,true,0),_imageBitmapData.draw(image);             _imageBitmap=Bitmap(addChild(new Bitmap(_imageBitmapData)))             

_imageBitmap.x=30 ;            

_imageBitmap.y=30 ;           

configMouseEvent();             

//----------hitTestArea------------------------             

var c:ColorTransform=new ColorTransform;             

c.color=0xff0000;             

_imageHotAreaData=_imageBitmapData.clone();             

_imageHotAreaData.draw(_imageHotAreaData,null,c);         

}         

private function configMouseEvent():void {             
this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler,false,0,true);             
this.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler,false,0,true);         

}         

/**************************drawRect handler*******************************/         

private function mouseDownHandler(evt:MouseEvent):void {//mouse_down             

if (_imageClipDraging) {                 return;             }            

 addChild(_mouseRectContainer);            

 _mouseRectStartX=evt.stageX;             

_mouseRectStartY=evt.stageY;             

this.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);         

}         private function mouseUpHandler(evt:MouseEvent):void {//mouse_up                         

//_currentDragClip&&();             
_imageClipDraging&&(_currentDragClip.stopDrag(),_imageClipDraging=false,_currentDragClip.alpha=1)
||(cutImage(checkIntersection()),_mouseRectContainer.graphics.clear(),this.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler))                     }        

 private function mouseMoveHandler(evt:MouseEvent):void {//mouse_move             

evt.updateAfterEvent();             

var minX:Number=Math.min(evt.stageX,_mouseRectStartX)             

var minY:Number=Math.min(evt.stageY,_mouseRectStartY)             

var maxX:Number=Math.max(evt.stageX,_mouseRectStartX)             

var maxY:Number=Math.max(evt.stageY,_mouseRectStartY) 

with(_mouseRectContainer.graphics){  

           clear();            

 lineStyle(0);             

beginFill(0xffff00,.5);             

drawRect(0,0,maxX-minX,maxY-minY);}            

 _mouseRectContainer.x=minX;             

_mouseRectContainer.y=minY;         

}         

/************************************************************************/         

/**************************drag  handler*******************************/         

private function clipMouseDownHandler(evt:MouseEvent):void {//mouse_down             

var target:Sprite=evt.target as Sprite;             

_currentDragClip=target;             

_currentDragClip.alpha=.5;             

_imageClipDraging=true;             

addChild(target);             

_currentDragClip.startDrag(false);         

}         

/************************************************************************/         

private function checkIntersection():Rectangle {             

var intersectRect:Rectangle=_imageBitmapData.rect.intersection(new Rectangle(_mouseRectContainer.x-_imageBitmap.x,_mouseRectContainer.y-_imageBitmap.y,_mouseRectContainer.width,_mouseRectContainer.height));             trace("   BitmapData    :"+intersectRect);             

if (intersectRect.width==0 || intersectRect.height==0) {                 return null;             } 

 var bitmapData:BitmapData=new BitmapData(intersectRect.width,intersectRect.height,true,0);             bitmapData.draw(_imageHotAreaData,new Matrix(1,0,0,1,-intersectRect.x,-intersectRect.y),null,null,new Rectangle(0,0,intersectRect.width,intersectRect.height));             

var intersectHotAreaRect:Rectangle=bitmapData.getColorBoundsRect(0xFFFF0000, 0xFFFF0000,true);             

trace("        :"+intersectHotAreaRect);             

if (intersectHotAreaRect.width==0 || intersectHotAreaRect.height==0) {                                                 return null;             }  

           //                     

intersectHotAreaRect.x-=1            

 intersectHotAreaRect.y-=1            

 intersectHotAreaRect.width+=2             

intersectHotAreaRect.height+=2             

return intersectHotAreaRect;        

 }         

private function cutImage(rect:Rectangle):void {//                   

 if (!rect) {                 return;             }             

var clipBitmapData:BitmapData=new BitmapData(rect.width,rect.height,true,0);             

varcliptX:Number=(_mouseRectContainer.x=_mouseRectContainer.x<_imageBitmap.x?0:_mouseRectContainer.x-_imageBitmap.x)+rect.x;             varcliptY:Number=(_mouseRectContainer.y=_mouseRectContainer.y<_imageBitmap.y?0:_mouseRectContainer.y-_imageBitmap.y)+rect.y;             clipBitmapData.draw(_imageBitmapData,new Matrix(1,0,0,1,-cliptX,-cliptY),null,null,new Rectangle(0,0,rect.width,rect.height));//intersectHotAreaRect)   

          var clipBitmap:Bitmap=new Bitmap(clipBitmapData);            

 clipBitmap.filters=[new GlowFilter(0,1,2,2,10,1)];             

var sprite:Sprite=new Sprite            

 with(sprite.graphics){            

 lineStyle(0);             

lineTo(rect.width,0);            

 lineTo(rect.width,rect.height);             

lineTo(0,rect.height);             

lineTo(0,0);}            

 sprite.x=_mouseRectContainer.x+rect.x+_imageBitmap.x            

 sprite.y=_mouseRectContainer.y+rect.y+_imageBitmap.y            
 sprite.addEventListener(MouseEvent.MOUSE_DOWN,clipMouseDownHandler);             

Sprite(addChild(sprite)).addChild(clipBitmap)            

 var fillRect:Rectangle=new Rectangle(sprite.x-_imageBitmap.x,sprite.y-_imageBitmap.y,rect.width,rect.height);             _imageBitmapData.fillRect(fillRect,0);             

_imageHotAreaData.fillRect(fillRect,0);             

  clibImg = clipBitmap;//           

   } 

    } 

} 

/--------------------------------------------------------------  ------- ---------------------------------------------------------------------/