FlashPlayer10
FileReference新增了save的功能。
能直接產生一個實體的檔案讓使用者下載。
在這兒我們就來練習載入一張圖片,畫圖後,再重新另存成一張新的.jpg

除了筆者的程式碼, 還需要下載JPGEncoder編碼類別。
可以在這兒下載。
http://code.google.com/p/as3corelib/downloads/list
還有bit101作者的組件。
http://milkmidi.blogspot.com/2008/12/as3.html
需要使用FlashCS4或是FlexSDK4才能發怖成功。
FileReference新增了save的功能。
能直接產生一個實體的檔案讓使用者下載。
在這兒我們就來練習載入一張圖片,畫圖後,再重新另存成一張新的.jpg

除了筆者的程式碼, 還需要下載JPGEncoder編碼類別。
可以在這兒下載。
http://code.google.com/p/as3corelib/downloads/list
還有bit101作者的組件。
http://milkmidi.blogspot.com/2008/12/as3.html
需要使用FlashCS4或是FlexSDK4才能發怖成功。
package
{
import com.bit101.components.PushButton;
//匯入bit101作者的組件包。
import com.adobe.images.JPGEncoder;
//匯入JPGEncoder編碼
import flash.display.*;
import flash.events.*;
import flash.geom.Rectangle;
import flash.text.*;
import flash.net.*;
import flash.utils.ByteArray;
[SWF(width="800", height="600", backgroundColor="#FFFFFF", framerate="30")]
public class ImageFileLoadAndSave extends Sprite
{
private var _file :FileReference;
private var _ldr :Loader = new Loader();
public function ImageFileLoadAndSave():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
//設定stage屬性。
this.addChild(_ldr);
//將_ldr加入至目前的可視物件容器。
_ldr.y = 40;
var pub1:PushButton = new PushButton(this, 0, 0, "Load File", onFileLoad);
var pub2:PushButton = new PushButton(this, 100, 0, "Save File", onSave);
//建立二個Button。
_file = new FileReference();
_file.addEventListener(Event.SELECT, onSelect);
//當檔案被選取時。
_file.addEventListener(Event.COMPLETE, onComplete);
//當檔案被成功載入時。
}
private function onFileLoad(e:Event):void{
_file.browse([new FileFilter("Images Formats (*.jpg,*.gif,*.png,*.swf)", "*.jpg;*.gif;*.png;*.swf", "JPEG;jp2_;GIFF;SWF")]);
}
private function onSelect(e:Event):void {
_file.load();
//載入。
}
private function onComplete(e:Event):void{
var _data:ByteArray = _file.data;
_ldr.loadBytes(_data);
}
private function onSave(e:Event):void {
var bmp:BitmapData = new BitmapData(400, 300, false);
bmp.draw(this);
//使用BitmapData的draw方法, 繪製目前的物件。
var jpg:JPGEncoder = new JPGEncoder(80);
//建立JPGEncoder編碼類別。
var by:ByteArray = jpg.encode(bmp);
//將BitmapData編碼成ByteArray格式。
try {
_file.save(by, _file.name.split(".")[0] + ".jpg");
//使用FilReference類別的save方法
//將ByteArray存成實體的.jpg。
}catch (err:Error){
trace(err);
}
}
}
}
留言
總算解決了~~
好在升上Flex4碼都還通用~~
不用大改~
都一樣是使用AS3
不用重學就可以有新功能
這問題卡了我好久,感謝您!!
http://www.libspark.org/wiki/yossy/swfassist/Tutorial/Alpha/Writing