以二进制形式读取外部图片文件(AIR)

时间:2014-06-13 14:47:49
【文件属性】:

文件名称:以二进制形式读取外部图片文件(AIR)

文件大小:4KB

文件格式:TXT

更新时间:2014-06-13 14:47:49

二进制

package { import flash.display.Bitmap; import flash.display.Loader; import flash.display.NativeMenu; import flash.display.NativeMenuItem; import flash.display.NativeWindow; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.filesystem.File; import flash.filesystem.FileMode; import flash.filesystem.FileStream; import flash.net.FileFilter; import flash.net.navigateToURL; import flash.net.URLRequest; import flash.utils.ByteArray; public class Main extends Sprite { private var rootMenu:NativeMenu = new NativeMenu; private var fileMenu:NativeMenu = new NativeMenu; private var openItem:NativeMenuItem = new NativeMenuItem("打开文件"); private var quitItem:NativeMenuItem = new NativeMenuItem("退出"); private var helpMenu:NativeMenu = new NativeMenu; private var fsnhf:NativeMenuItem = new NativeMenuItem("00"); private var window:NativeWindow = stage.nativeWindow; private var loader:Loader = new Loader; private var imageFileFilter:FileFilter = new FileFilter("图片(*.jpg;*.png;*.gif;*.jpeg)", "*.jpg;*.png;*.gif;*.jpeg"); public function Main():void { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; buildMenu(); configListeners(); } private function buildMenu():void { rootMenu.addSubmenu(fileMenu, "文件"); fileMenu.addItem(openItem); fileMenu.addItem(quitItem); rootMenu.addSubmenu(helpMenu, "帮助"); helpMenu.addItem(fsnhf); window.menu = rootMenu; } private function configListeners():void { openItem.addEventListener(Event.SELECT, eventHandler); quitItem.addEventListener(Event.SELECT, eventHandler); fsnhf.addEventListener(Event.SELECT, eventHandler); } private function eventHandler(event:Event):void { switch(event.target) { case openItem: var file:File = new File; file.addEventListener(Event.SELECT, selectFile); file.browseForOpen("打开文件", [imageFileFilter]); break; case quitItem: window.close(); break; case fsnhf: navigateToURL(new URLRequest("00")); break; } } private function selectFile(event:Event):void { var file:File = event.target as File; var stream:FileStream = new FileStream; stream.open(file, FileMode.READ); var bytes:ByteArray = new ByteArray; stream.readBytes(bytes, 0, stream.bytesAvailable); stream.close(); loader.unload(); loader = new Loader; loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded); loader.loadBytes(bytes); addChild(loader); } private function imageLoaded(event:Event):void { event.target.removeEventListener(Event.COMPLETE, imageLoaded); var bitmap:Bitmap = Bitmap(event.target.loader.content); window.width = bitmap.width; window.height = bitmap.height + 30; } } }


网友评论

  • 好人好报,资源很给力,帮我解决了一个技术难题
  • 一般般吧 看懂之后自己写起来很便利的