码迷,mamicode.com
首页 > 其他好文 > 详细

[ActionScript 3.0] 通过BitmapData将对象保存成jpg图片

时间:2015-06-19 18:38:26      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

此方法需要用到JPGEncoder.as和BitString.as这两个类,是将BitmapData对象转换成ByteArray,然后通过FileStream把此ByteArray写入到文件保存成jpg图片,因为用到File相关类,故需要用air播放器发布flash,在此提供两个下载JPGEncoder.as和BitString.as类的地址,

CSDN:http://download.csdn.net/source/3205224

Adobe官方的CoreLib下载: http://code.google.com/p/as3corelib/ 使用其中的:com.adobe.images.JPEGEncoder

以下是一个简单例子,SaveToJPEG .as 类 可直接作为文档类

 

 1 package 
 2 {
 3     import JPEGEncoder.JPGEncoder;
 4     import flash.display.Bitmap;
 5     import flash.display.BitmapData;
 6     import flash.display.Loader;
 7     import flash.display.MovieClip;
 8     import flash.display.Sprite;
 9     import flash.events.Event;
10     import flash.events.MouseEvent;
11     import flash.filesystem.File;
12     import flash.filesystem.FileMode;
13     import flash.filesystem.FileStream;
14     import flash.geom.Rectangle;
15     import flash.net.URLRequest;
16     import flash.text.TextField;
17     import flash.text.TextFieldAutoSize;
18     import flash.utils.ByteArray;
19 
20     /**
21       * ...
22       * @author Frost.Yen
23       */
24     public class SaveToJPEG extends Sprite
25     {
26         var ldr:Loader = new Loader();
27         var rect:Rectangle;
28         var bytearr:ByteArray;
29         public function SaveToJPEG():void
30         {
31             init();
32         }
33 
34         private function init():void
35         {
36             var btnMC:MovieClip = new MovieClip();
37             btnMC.graphics.beginFill(0xcccccc);
38             btnMC.graphics.drawRoundRect(0, 0, 100, 40, 10, 10);
39             btnMC.graphics.endFill();
40             btnMC.x = 630;
41             btnMC.y = 50;
42             btnMC.buttonMode = true;
43             btnMC.addEventListener(MouseEvent.CLICK, onClick);
44             this.addChild(btnMC);
45 
46             var txt:TextField = new TextField();
47             txt.autoSize = TextFieldAutoSize.LEFT;
48             txt.selectable = false;
49             txt.mouseEnabled = false;
50             txt.text = "点击保存此图片";
51             txt.x = (btnMC.width - txt.width) / 2;
52             txt.y = (btnMC.height - txt.height) / 2;
53             btnMC.addChild(txt);
54 
55             ldr.load(new URLRequest("http://hiphotos.baidu.com/frostyen/pic/item/7e49d8b53f6b48ffd9335aa2.jpg"));
56             ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
57         }
58 
59         private function onClick(e:MouseEvent):void
60         {
61             if (bytearr != null)
62             {
63                 var file:File = new File();
64                 file.browseForDirectory("保存");
65                 file.addEventListener(Event.SELECT, onSelectPath);
66             }
67         }
68 
69         private function onSelectPath(e:Event):void
70         {
71             var file:File = (e.target as File).resolvePath("松竹梅副本.jpg");
72             var filestream:FileStream = new FileStream();
73             filestream.open(file, FileMode.WRITE);
74             filestream.writeBytes(bytearr);
75         }
76 
77         private function onComplete(e:Event):void
78         {
79             this.addChild(ldr);
80             rect = ldr.getRect(ldr);
81 
82             var bmpd:BitmapData = new BitmapData(rect.width,rect.height);
83             bmpd.draw(ldr);
84             var jpg:JPGEncoder = new JPGEncoder();
85             bytearr = jpg.encode(bmpd) as ByteArray;
86         }
87     }
88 }

 

[ActionScript 3.0] 通过BitmapData将对象保存成jpg图片

标签:

原文地址:http://www.cnblogs.com/frost-yen/p/4589308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!