编写as3工程时 有几处需注意地方
1:同步安全策略文档 crossdomain.xml ,在完成安全文档下载之后才进行内容的下载:实现:
loader.load(new URLRequest(url), new LoaderContext(true));
2:设置图片的旋转中心,可以使用marix 类动态的去算 也可以设置将容器至于图片中心的位置。让player给你算。前者的代码相对简单。这里简单处理
protected function updateCenterPoint():void{ loader.x = -(loader.width/2); loader.y = -(loader.height/2); }
3:某些你在flex框架中依赖的类,将不复存在。如果这个类的框架的耦合性不强,可以从将源代码拷贝出来。例如:
框架中的位置:
//////////////////////////////////////////////////////////////////////////////// // // ADOBE SYSTEMS INCORPORATED // Copyright 2007 Adobe Systems Incorporated // All Rights Reserved. // // NOTICE: Adobe permits you to use, modify, and distribute this file // in accordance with the terms of the license agreement accompanying it. // //////////////////////////////////////////////////////////////////////////////// package mx.graphics.codec { import flash.display.BitmapData; import flash.utils.ByteArray; /** * The JPEGEncoder class converts raw bitmap images into encoded * images using Joint Photographic Experts Group (JPEG) compression. * * For information about the JPEG algorithm, see the document * http://www.opennet.ru/docs/formats/jpeg.txt by Cristi Cuturicu. */ public class JPEGEncoder implements IImageEncoder
copy到工程后:
package component { import flash.display.BitmapData; import flash.utils.ByteArray; public class JPEGEncoder
as3 工程:
工程主类 (main 入口)
package { import component.ComponentCopy; import component.MainWindow; import component.ScalePreview; import component.SourceImage; import component.Sysvo; import component.ToolBarEvent; import component.Uploader; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Sprite; import flash.events.DataEvent; import flash.events.ErrorEvent; import flash.events.Event; import flash.events.MouseEvent; public class ImgTool extends Sprite { private var mainWin:MainWindow = new MainWindow(); [Embed(source="assets/save_bt01.png")] public const UpSkin:Class; private const skinBtnSaveUp:Bitmap = new UpSkin(); [Embed(source="assets/save_bt02.png")] public const DownSkin:Class; private const skinBtnSaveDown:Bitmap = new DownSkin(); private var preview:ScalePreview = new ScalePreview(); [Embed(source="assets/uploading_bt01.png")] public const UpSkin2:Class; public const skinBtnUploadUp:Bitmap = new UpSkin2(); [Embed(source="assets/uploading_bt02.png")] public const DownSkin2:Class; public const skinBtnUploadDown:Bitmap = new DownSkin2(); private var hasContent:Boolean = false; public function ImgTool() { stage.align = "TL"; stage.scaleMode = "noScale"; Sysvo.fill(root.loaderInfo.parameters); Debug.warning("v1011011626"); root.addEventListener(ErrorEvent.ERROR, unknowException); addEventListener(SourceImage.EVE_DRAGING, dragingHandler); addEventListener(SourceImage.IMG_LOADED, loadedHandler); addEventListener(ToolBarEvent.EVE_ROTATION_LEFT, dragingHandler); addEventListener(ToolBarEvent.EVE_ROTATION_RIGHT, dragingHandler); addEventListener(ToolBarEvent.EVE_ZOOM_IN, dragingHandler); addEventListener(ToolBarEvent.EVE_ZOOM_OUT, dragingHandler); Uploader.instance.addEventListener(Uploader.EVE_SAVED, function(e:DataEvent):void{ mainWin.loadContent(e.data); Sysvo.completeCallJavascript(); }) mainWin.x = 10; mainWin.y = 50; addChild(mainWin); mainWin.loadContent(Sysvo.hasPicUrl); addChild(createButton(10, 413, skinBtnSaveUp, skinBtnSaveDown, saveAndUpload)); addChild(createButton(10, 15, skinBtnUploadUp, skinBtnUploadDown, firstUpload)); preview.x = 380; preview.y = 60; addChild(preview); } private function dragingHandler(e:Event):void{ var bitmapdata:BitmapData = ComponentCopy.copy( 187, 187, -91 , -91, mainWin); preview.reflush(bitmapdata); } public function unknowException(e:ErrorEvent):void{ } private function createButton(x:Number, y:Number, upskin:Bitmap, downSkin:Bitmap, onclick:Function):Sprite{ var button:Sprite = new Sprite(); button.addChild(upskin); button.addChild(downSkin); button.x = x; button.y = y; button.getChildAt(1).visible = false; button.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void{ e.stopImmediatePropagation(); e.stopPropagation(); Sprite(e.target).getChildAt(0).visible = false; Sprite(e.target).getChildAt(1).visible = true; }); button.addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent):void{ e.stopImmediatePropagation(); e.stopPropagation(); Sprite(e.target).getChildAt(0).visible = true; Sprite(e.target).getChildAt(1).visible = false; onclick(); }); return button; } public function saveAndUpload():void{ if(hasContent){ var bitmapdata:BitmapData = ComponentCopy.copy( 187, 187, -91 , -91, mainWin); Uploader.instance.uploadBitmapData(bitmapdata, Sysvo.fixUploadUrl); }else{ Debug.warning("相片剪切板内没有图片"); } } public function loadedHandler(e:Event):void{ hasContent = true; dragingHandler(null); } public function firstUpload():void{ try{ Uploader.instance.url = Sysvo.firstUploadUrl; Uploader.instance.selectFile(); Uploader.instance.addEventListener(Uploader.UPLOADED, function(e:DataEvent):void{ mainWin.loadContent(e.data); }); }catch(e:Error){ Debug.log("firstUpload::"+e.toString()); } } } }
主操作类:相片的旋转 放大缩小 :
package component { import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.net.URLRequest; import flash.system.LoaderContext; public class SourceImage extends Sprite { public var loader:Loader = new Loader(); private var _loaded:Boolean = false; public static const EVE_DRAGING:String = "draging"; public static const IMG_LOADED:String = "LOADED"; public function SourceImage() { super(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); addChild(loader); this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); this.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); } public function load(url:String):void{ loader.unload(); _loaded = false; if(url != null && url.length > 0){ reset(); loader.load(new URLRequest(url), new LoaderContext(true)); } } public function reset():void{ loader.scaleX = 1; loader.scaleY = 1; loader.rotation = 0; this.scaleX = 1; this.scaleY = 1; this.rotation = 0; } private function mouseDownHandler(e:MouseEvent):void{ this.startDrag(); this.addEventListener(MouseEvent.MOUSE_MOVE, draging); } private function draging(e:MouseEvent):void{ this.dispatchEvent(new MouseEvent(EVE_DRAGING)); } private function mouseUpHandler(e:MouseEvent):void{ this.stopDrag(); this.removeEventListener(MouseEvent.MOUSE_MOVE, draging); } private function completeHandler(e:Event):void{ _loaded = true; updateCenterPoint(); dispatchEvent(new Event(IMG_LOADED, true, true)); } public function zoom(setup:Number):void{ loader.scaleY = loader.scaleX += setup;//0.1; updateCenterPoint(); } protected function updateCenterPoint():void{ loader.x = -(loader.width/2); loader.y = -(loader.height/2); } public function hasContent():Boolean{ return _loaded; } override public function set rotation(value:Number):void{ super.rotation = value; } } }
缩放预览组件:
package component { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Sprite; import flash.geom.Matrix; public class ScalePreview extends Sprite { private var scale33:Sprite = new Sprite(); private var scale66:Sprite = new Sprite(); private var scale99:Sprite = new Sprite(); public function ScalePreview(){ addChild(scale99); scale66.y = 111; addChild(scale66); scale33.y = 189; addChild(scale33); } public function reflush(bitmapdata:BitmapData):void{ _reflsh2(scale99, bitmapdata, 99); _reflsh2(scale66, bitmapdata, 66); _reflsh2(scale33, bitmapdata, 33); } public function _reflsh2(traget:Sprite, bitmapdata:BitmapData, wide:Number):void{ traget.graphics.clear(); var m:Matrix = new Matrix(wide/bitmapdata.width , 0,0, wide/bitmapdata.height, 0,0); traget.graphics.beginBitmapFill(bitmapdata, m, false, false); traget.graphics.drawRect(0,0,wide, wide); traget.graphics.endFill(); } } }
工具栏组件:
package component { import flash.display.Bitmap; import flash.display.Sprite; import flash.events.MouseEvent; import flash.geom.Point; public class ToolBar { private var tollbarParent:Sprite; private var operator:SourceImage; [Embed(source="../assets/clockwise.png")] public var rightr:Class; public const rightrCmp:Sprite = new Sprite(); [Embed(source="../assets/anticlockwise.png")] public const leftr:Class; public const leftrCmp:Sprite = new Sprite(); [Embed(source="../assets/zoom in.png")] public const zoomin:Class; public const zoominCmp:Sprite = new Sprite(); [Embed(source="../assets/zoom out.png")] public const zoomout:Class; public const zoomoutCmp:Sprite = new Sprite(); public function ToolBar(parent:Sprite, opraterTarget:SourceImage) { tollbarParent = parent; operator = opraterTarget; rightrCmp.addChild(new rightr() as Bitmap); rightrCmp.addEventListener(MouseEvent.CLICK, rightRotation); leftrCmp.addChild(new leftr() as Bitmap); leftrCmp.addEventListener(MouseEvent.CLICK, leftRotation); zoominCmp.addChild(new zoomin() as Bitmap); zoominCmp.addEventListener(MouseEvent.CLICK, zoominHandler); zoomoutCmp.addChild(new zoomout() as Bitmap); zoomoutCmp.addEventListener(MouseEvent.CLICK, zoomoutHandler); } public function initLayOut(zi:Point, zo:Point, rr:Point, lr:Point):void{ zoominCmp.x = zi.x; zoominCmp.y = zi.y; leftrCmp.x = lr.x; leftrCmp.y = lr.y; rightrCmp.x = rr.x; rightrCmp.y = rr.y; zoomoutCmp.x = zo.x; zoomoutCmp.y = zo.y; tollbarParent.addChild(zoominCmp); tollbarParent.addChild(leftrCmp); tollbarParent.addChild(rightrCmp); tollbarParent.addChild(zoomoutCmp); } protected function rightRotation(e:MouseEvent):void{ if(operator.hasContent()){ var oldVal:Number = operator.rotation; operator.rotation += 90; e.target.dispatchEvent(new ToolBarEvent(ToolBarEvent.EVE_ROTATION_RIGHT, 90, oldVal, true, true)); } } protected function leftRotation(e:MouseEvent):void{ if(operator.hasContent()){ var oldVal:Number = operator.rotation; operator.rotation -= 90; e.target.dispatchEvent(new ToolBarEvent(ToolBarEvent.EVE_ROTATION_LEFT, -90, oldVal, true, true)); } } protected function zoominHandler(e:MouseEvent):void{ if(operator.hasContent() && operator.loader.scaleX <= 4){ operator.zoom(0.1); e.target.dispatchEvent(new ToolBarEvent(ToolBarEvent.EVE_ZOOM_IN, 0.1, -1, true, true)); } } protected function zoomoutHandler(e:MouseEvent):void{ if(operator.hasContent() && operator.loader.scaleX >= 0.25){ operator.zoom(-0.1); e.target.dispatchEvent(new ToolBarEvent(ToolBarEvent.EVE_ZOOM_OUT, 0.1, -1, true, true)); } } } }
主窗体:
package component { import flash.display.Sprite; import flash.geom.Point; public class MainWindow extends Sprite { private var sourceImage:SourceImage = new SourceImage(); private var tb:ToolBar; private var viewPort:Sprite = new Sprite(); private var masker:Sprite = new Sprite(); private var border:Sprite = new Sprite(); public function MainWindow() { super(); addChild(masker); masker.graphics.beginFill(0xFF0000); masker.graphics.drawRect(0, 0, 351, 351); masker.graphics.endFill(); mask = masker; sourceImage.x = 185; sourceImage.y = 185; addChild(sourceImage); tb = new ToolBar(this, sourceImage); tb.initLayOut(new Point(5,5), new Point(30, 5) , new Point(80, 5), new Point(55, 5)); addChild(border); drawBorder([{x:350,y:0},{x:350,y:350},{x:0,y:350},{x:0,y:0}], 0xD4D4D4, 2); viewPort.y = viewPort.x = 90; viewPort.height = viewPort.width = 190; addChild(viewPort); border.graphics.moveTo(90, 90); drawBorder([{x:280,y:90},{x:280,y:280},{x:90,y:280},{x:90,y:90}], 0xFFAA01, 2); } public function drawBorder(path:Array, color:uint, tikness:int):void{ for(var i:int=0; i<path.length; i++){ border.graphics.lineStyle(tikness, color); border.graphics.lineTo(path[i].x , path[i].y); } } public function loadContent(url:String):void{ sourceImage.reset(); sourceImage.x = 185; sourceImage.y = 185; sourceImage.load(url); } } }