2011-09-19 51 views
1

有沒有人在flashbuilder中成功使用過這種編碼器4.5.1? 我得到這個錯誤?通常在裝載第二張照片。任何幫助表示讚賞。jpegencoder.swc from http://last.instinct.se/graphics-and-effects/using-the-fast-asynchronous-alchemy-jpeg-encoder-in-flash/640

RangeError: Error #1506: The specified range is invalid. 
at cmodule.aircall::FSM_imalloc$/start() 
at cmodule.aircall::FSM_pubrealloc/work() 
at cmodule.aircall::CRunner/work() 
at Function/<anonymous>() 
at flash.utils::Timer/_timerDispatch() 
at flash.utils::Timer/tick() 

我的代碼如下:

private var im:img; 
private var jpgStream:ByteArray; 
private var jpeglib:Object; 
private var jpeginit:CLibInit = new CLibInit(); // get library 

public function save_image(_im:img, pg:Number):void 
{ 
    var bm:BitmapData = Bitmap(_im.getChildAt(0)).bitmapData; 
    im = _im; 

    var imgData:ByteArray = bm.getPixels(bm.rect); 
    jpgStream = new ByteArray(); 
    imgData.position = 0; 

    trace(bm == null); 
    var jpegQuality:Number = 40; 

    if (!jpeglib) { 
      jpeglib=jpeginit.init(); 
    } 
    jpeglib.encodeAsync(encodeComplete, imgData, jpgStream, bm.width, bm.height, jpegQuality); 
} 

private function encodeComplete(e:Event):void { 
    trace("Encoding complete"); 

    file = File.documentsDirectory.resolvePath("Pic-A-Note/" + albumName + "/object_layer_" + im.name + ".pn"); 
    fs.open(file, FileMode.WRITE); 
    fs.writeBytes(jpgStream,0); 
    fs.close(); 
} 

回答

2

問題是您正在初始化兩次CLibInit。我創建了一個自定義類來處理使用jpegencoder.swc透明地對jpg進行編碼,就像處理本地類一樣。

public class JPGAlchemyEncoder { 

    private static var alchemyWrapper:Object; 
    private var quality:Number; 

    public function JPGAlchemyEncoder(quality:Number) { 
     this.quality = quality; 
     if (!alchemyWrapper){ 
      var loader:CLibInit = new CLibInit; 
      alchemyWrapper = loader.init(); 
     } 
    } 

    public function encode(bitmapData:BitmapData):ByteArray{ 
     var data: ByteArray = bitmapData.clone().getPixels(bitmapData.rect); 
     data.position = 0; 
     return alchemyWrapper.write_jpeg_file(data, bitmapData.width, bitmapData.height, 3, 2, quality); 
    } 

    public function encodeAsync(bitmapData:BitmapData, completeHandler:Function):void{ 
     var encodedData:ByteArray = new ByteArray(); 
     var data: ByteArray = bitmapData.clone().getPixels(bitmapData.rect); 
     data.position = 0; 
     var encodeComplete:Function = function():void{ 
      completeHandler(encodedData); 
     }; 
     alchemyWrapper.encodeAsync(encodeComplete, data, encodedData, bitmapData.width, bitmapData.height, quality); 
    } 
} 
} 

檢查構造函數如何處理靜態CLib。這樣,您可以根據需要啓動儘可能多的JPGAlchemyEncoder,但CLib只會初始化一次,解決您的問題。

+0

啊,這很有道理。儘管我現在自己得出這個結論,但我仍然認爲這對於那些可能會遇到這個問題的人來說是一個有用的職位。謝謝 – Tw1tCh

1

沒有用過深港西部通道,但在的as3corelib非鍊金術JPEG編碼器將做的工作一樣好(沒有任何錯誤,我可能會添加) http://code.google.com/p/as3corelib/