热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Flex截图实例

import mx.events.CloseEvent; import mx.managers.PopUpManager;

xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx&#61;"http://ns.adobe.com/mxml/2009"
xmlns:s
&#61;"library://ns.adobe.com/flex/spark"
xmlns:mx
&#61;"library://ns.adobe.com/flex/mx"
minWidth
&#61;"955" minHeight&#61;"600">
<fx:Declarations>

fx:Declarations>
<fx:Script>

import mx.events.CloseEvent;
import mx.managers.PopUpManager;
import mx.containers.TitleWindow;
import mx.controls.Alert;
private var initX:Number;
private var initY:Number;
private var borderCan:Canvas;

//测试用图片地址&#xff0c;可以用鼠标拖拽进行选择区域

[Bindable]private var imgPath:String&#61;"../images/img8.jpg";
private function downHander(e:MouseEvent):void{
if(borderCan!&#61;null){

this.mainCan.removeChild(borderCan);
}
initX &#61; preUploadImage.mouseX;
initY &#61; preUploadImage.mouseY;
this.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
this.addEventListener(MouseEvent.MOUSE_UP,upHander);
borderCan &#61; new Canvas();
borderCan.width&#61;1;
borderCan.height&#61;1
borderCan.setStyle("borderStyle","solid");
borderCan.setStyle("borderColor","red");
borderCan.x&#61;initX;
borderCan.y&#61;initY;
this.mainCan.addChild(borderCan);
}
private var upX:Number;
private var upY:Number;
private function upHander(e:MouseEvent):void{
upX &#61; borderCan.x
upY &#61; borderCan.y
this.txtX.text &#61;"X坐标&#xff1a;"&#43;upX&#43;" 宽度&#xff1a; "&#43;borderCan.width;
this.txtY.text &#61; "Y坐标&#xff1a;"&#43;upY&#43;" 高度&#xff1a; "&#43;borderCan.height;
this.removeEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
this.removeEventListener(MouseEvent.MOUSE_UP,upHander);
}

private function moveHandler(e:MouseEvent):void{
this.borderCan.width &#61; this.mainCan.mouseX-initX;
this.borderCan.height &#61; this.mainCan.mouseY - initY;
}

private var w:Number&#61;0;
private var h:Number&#61;0;
private function getImg():void{
w &#61; borderCan.width;
h &#61; borderCan.height;
//处理截图时候鼠标反向拖拽的坐标问题
var finalX:Number&#61;borderCan.x;
var finalY:Number&#61;borderCan.y;
if(w<0){
finalX &#61; borderCan.x &#43; w;
w&#61; -w;
}
if(h<0){
finalY &#61; borderCan.y &#43; h;
h&#61; -h;
}

//用bitmapdata全部获取预览图片的像素
var initBD:BitmapData &#61; new BitmapData(preUploadImage.width,preUploadImage.height);
initBD.draw(preUploadImage);
var bytearray:ByteArray &#61; new ByteArray();
//矩形为要截取区域
var re:Rectangle &#61; new Rectangle(finalX,finalY,w,h);
bytearray &#61; initBD.getPixels(re); //截取出所选区域的像素集合
var imgBD:BitmapData &#61; new BitmapData(w,h);
bytearray.position&#61;0; //必须的&#xff0c;当前的bytearray.position为最大长度&#xff0c;要设为从0开始读取
var fillre:Rectangle &#61; new Rectangle(0,0,w,h);
imgBD.setPixels(fillre,bytearray); //将截取出的像素集合存在新的bitmapdata里&#xff0c;大小和截取区域一样
showImg(imgBD); //显示图片
}

private function showImg(e:BitmapData):void{
var t:TitleWindow &#61; new TitleWindow();
t.showCloseButton&#61;true;
t.addEventListener(CloseEvent.CLOSE,closeWindow);
t.width &#61; w&#43;t.getStyle("borderThickness");
t.height &#61;h&#43;t.getStyle("borderThickness")&#43;t.getStyle("headerHeight");
var img:Image &#61; new Image();
img.width &#61; w;
img.height &#61; h;
img.source &#61; new Bitmap(e);
t.addChild(img);
PopUpManager.addPopUp(t,this,true);
PopUpManager.centerPopUp(t);
}
private function closeWindow(e:CloseEvent):void{
var t:TitleWindow &#61; e.currentTarget as TitleWindow;
PopUpManager.removePopUp(t);
}
]]>
fx:Script>
<mx:Canvas id&#61;"mainCan" width&#61;"600" height&#61;"400" borderColor&#61;"white" borderStyle&#61;"solid"
mouseDown
&#61;"downHander(event)">
<mx:Image id&#61;"preUploadImage" width&#61;"100%" height&#61;"100%" source&#61;"{imgPath}">
mx:Image>
mx:Canvas>
<s:Button x&#61;"10" y&#61;"410" label&#61;"截图" click&#61;"getImg()" fontSize&#61;"12"/>
<s:Label id&#61;"txtX" x&#61;"100" y&#61;"410" text&#61;"Label"/>
<s:Label id&#61;"txtY" x&#61;"200" y&#61;"410" text&#61;"Label"/>
s:Application>

 

转:https://www.cnblogs.com/Hero-Qiang/archive/2011/09/08/2171042.html



推荐阅读
author-avatar
棋牌
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有