裁剪图片
程序员文章站
2022-04-09 09:15:18
...
<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="416" height="352" showCloseButton="true"
creationComplete="init()" title="图片上传" xmlns:components="com.teamsun.framework.components.*" close="titlewindow1_closeHandler(event)">
<mx:Script>
<![CDATA[
import com.flex3.events.WindowCloseEvent;
import com.flex3.util.CommonFunctions;
import com.teamsun.framework.common.pojo.Designcomponent;
import com.teamsun.framework.design.ws.IDesigncomponentServiceWSRO;
import flash.net.FileReference;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.events.MoveEvent;
import mx.graphics.codec.JPEGEncoder;
import mx.managers.PopUpManager;
import mx.rpc.events.ResultEvent;
private var file: FileReference;
private var size:String;
private var fileName:String;
private var filePath:String = "";
private var request:URLRequest;
public var comp:Designcomponent;
private function init(): void{
Security.allowDomain("*");
}
private function upload():void
{
file = new FileReference();
file.addEventListener(Event.SELECT, file_selectHandler);
file.addEventListener(Event.COMPLETE, file_completeHandler);
file.browse(new Array(new FileFilter("图像文件 (*.jpg,*.gif,*.png)", "*.jpg; *.gif; *.png")));
}
private function file_selectHandler(event:Event):void
{
file.load();
}
private function file_completeHandler(event:Event):void
{
if (file.data.length>0)
{
picture.source = file.data;
}
else
{
CommonFunctions.showInfo("错误提示","图片加载错误!");
}
}
private function cutPicture():void
{
var bx:Number = cutPictureBox.x - picture.x;
var by:Number = cutPictureBox.y - picture.y ;
var bw:Number = cutPictureBox.boxWidth ;
var bh:Number = cutPictureBox.boxHeight ;
var pictureBD:BitmapData = new BitmapData(picture.width, picture.height);
pictureBD.draw(picture);
var pixels:ByteArray = pictureBD.getPixels(new Rectangle(bx, by, bw, bh));
pixels.position=0;
var cutBD:BitmapData = new BitmapData(bw, bh);
cutBD.setPixels(new Rectangle(0, 0, bw, bh), pixels);
var sb:Bitmap = new Bitmap(cutBD);
sb.smoothing = true;
sb.pixelSnapping = PixelSnapping.ALWAYS;
preImg.source = sb;
}
private function save():void
{
confimBtn.enabled = false;
var picture:ByteArray = null;
if (preImg.source is Bitmap)
{
var bitmapData:BitmapData = new BitmapData(preImg.width, preImg.height);
bitmapData.draw(preImg);
picture = new JPEGEncoder(100).encode(bitmapData);
IDesigncomponentServiceWSRO.INSTANCE.updateComponentImg(comp,picture,on_updateComponentImg_handle,CommonFunctions.faultmethod);
}
else(preImg.source is ByteArray)
{
picture = preImg.source as ByteArray;
IDesigncomponentServiceWSRO.INSTANCE.updateComponentImg(comp,picture,on_updateComponentImg_handle,CommonFunctions.faultmethod);
}
}
private function on_updateComponentImg_handle(event:ResultEvent):void
{
confimBtn.enabled = true;
var reobj:Object = event.result;
if(reobj)
{
if(reobj.isSuccued == true){
var closeevent:WindowCloseEvent = new WindowCloseEvent(WindowCloseEvent.WINDOW_CLOSE);
closeevent.IsConfirm = true;
closeevent.ReturnValue = reobj.value;
dispatchEvent(closeevent);
PopUpManager.removePopUp(this);
}
CommonFunctions.showInfo("提示",reobj.message);
}else
{
CommonFunctions.showInfo("提示","操作失败!");
}
}
protected function picture_completeHandler(event:Event):void
{
picture.x = 0;
picture.y = 0;
cutPictureBox.x = 0;
cutPictureBox.y = 0;
}
protected function picture_ioErrorHandler(event:IOErrorEvent):void
{
CommonFunctions.showInfo("错误提示","图片加载错误!");
}
protected function titlewindow1_closeHandler(event:CloseEvent):void
{
PopUpManager.removePopUp(this);
}
private var lockX:Number;
private var lockY:Number;
private var isMove:Boolean = false;
protected function picture_mouseDownHandler(event:MouseEvent):void
{
isMove = true;
lockX = event.localX;
lockY = event.localY;
}
protected function picture_mouseMoveHandler(event:MouseEvent):void
{
if(isMove)
{
var tempX:Number = event.localX - lockX;
var tempY:Number = event.localY - lockY;
picture.x += tempX;
picture.y += tempY;
cutPictureBox.x +=tempX;
cutPictureBox.y +=tempY;
}
}
protected function picture_mouseUpHandler(event:MouseEvent):void
{
if(!cutPictureBox.visible)
{
return;
}
isMove = false;
cutPicture();
}
protected function picture_doubleClickHandler(event:MouseEvent):void
{
var tempX:Number = cutPictureBox.boxWidth;
var tempY:Number = cutPictureBox.boxHeight;
if(( event.localX >= tempX/2) && (event.localX+tempX/2 <= picture.width))
cutPictureBox.x = picture.x + event.localX - tempX/2;
if(event.localY >= tempY/2 && (event.localY+tempY/2 <= picture.height))
cutPictureBox.y = picture.y + event.localY- tempY/2;
cutPicture();
}
protected function picture_updateCompleteHandler(event:FlexEvent):void
{
var temp:Number = picture.width;
if(0 == temp)
return ;
if(picture.width < 65 || picture.height<65)
{
cutPictureBox.visible =false;
preImg.source = file.data;
picture.x = (imgCanvas.width-preImg.width)/2;
picture.y = (imgCanvas.height-preImg.height)/2;
picture.doubleClickEnabled= false;
picture.removeEventListener(MouseEvent.MOUSE_DOWN,picture_mouseDownHandler);
picture.removeEventListener(MouseEvent.MOUSE_MOVE,picture_mouseMoveHandler);
picture.removeEventListener(MouseEvent.DOUBLE_CLICK,picture_doubleClickHandler);
picture.removeEventListener(MouseEvent.MOUSE_UP,picture_mouseUpHandler);
return ;
}else
{
picture.doubleClickEnabled=true;
picture.addEventListener(MouseEvent.MOUSE_DOWN,picture_mouseDownHandler);
picture.addEventListener(MouseEvent.MOUSE_MOVE,picture_mouseMoveHandler);
picture.addEventListener(MouseEvent.DOUBLE_CLICK,picture_doubleClickHandler);
picture.addEventListener(MouseEvent.MOUSE_UP,picture_mouseUpHandler);
}
cutPictureBox.visible =true;
}
]]>
</mx:Script>
<mx:HBox height="100%" width="100%" >
<mx:VBox height="100%" width="100%" verticalAlign="middle" horizontalAlign="center">
<!--<components:ScaleLabels id="scale"/>-->
<mx:Canvas width="256" height="256" left="10" id="imgCanvas" top="40" borderThickness="1" borderStyle="solid" horizontalScrollPolicy="off" verticalScrollPolicy="off">
<mx:Image id="picture"
smoothBitmapContent="true" updateComplete="picture_updateCompleteHandler(event)"
complete="picture_completeHandler(event)"
ioError="picture_ioErrorHandler(event)"/>
<components:ScaleBox id="cutPictureBox"
cutObject="{picture}" visible="false"
boxWidth="64" boxHeight="64"
cut="cutPicture()" x="64" y="55"/>
</mx:Canvas>
</mx:VBox>
<mx:VBox height="100%" width="120" verticalAlign="middle" horizontalAlign="center">
<mx:VBox height="256" width="100" verticalAlign="middle" horizontalAlign="center" verticalGap="15" borderStyle="solid" borderThickness="1"
paddingBottom="8" paddingTop="8" paddingRight="8">
<!--<mx:Label text="图片大小在64*64显示效果最佳" />-->
<mx:Label text="图像预览:" />
<mx:Canvas width="64" height="64" borderThickness="1" borderStyle="solid">
<mx:Image id="preImg" width="100%" height="100%"/>
</mx:Canvas>
<mx:Label text="64×64" />
<mx:Button id="loadBtn" click="upload();" label="本地上传"/>
</mx:VBox>
</mx:VBox>
</mx:HBox>
<mx:ControlBar horizontalAlign="right">
<mx:Button label="确定" click="save();" id="confimBtn"/>
<mx:Button label="关闭" click="titlewindow1_closeHandler(null)"/>
</mx:ControlBar>
</mx:TitleWindow>
上一篇: js 对象转换成数组