2012-01-10 184 views
2

入門到AS3位圖的東西...AS3轉換ByteArray的位圖

當我嘗試做下面的代碼失敗

bmd.setPixels(bmd.rect,decodeValue);

和錯誤消息是: 錯誤:錯誤#2030:遇到文件結尾。

這種情況是我通過轉換byteArray將圖像作爲二進制存儲到數據庫中,現在我想檢索它並將其轉換回圖像。

只是要清除這個ByteArray需要放置到位圖,然後你可以添加到電影剪輯吧?

import flash.display.Loader; 
import flash.net.URLRequest; 
import flash.display.Bitmap; 
import flash.display.BitmapData; 
import com.dynamicflash.util.Base64; 

var loader:Loader; 
var req:URLRequest; 
var orig_mc:MovieClip; 
var copy_mc:MovieClip; 

function loaderCompleteHandler(evt:Event) { 
    //swap data 
    var ldr:Loader = evt.currentTarget.loader as Loader; 
    var origImg:Bitmap = (ldr.content as Bitmap); 
    var origBmd:BitmapData = origImg.bitmapData; 
    trace(origImg.bitmapData); 
    trace(origImg.width); 
    trace(origImg.height); 

    //Convert image byteData into Base64 String 
    var byteArray:ByteArray = new ByteArray(); 
    byteArray.writeObject(origBmd); 
    var encoded:String = Base64.encodeByteArray(byteArray); 
    trace("\nENCODED:\n" + encoded); 
    var decoded:ByteArray = Base64.decodeToByteArray(encoded); 
    trace("\nDECODED:\n" + decoded.toString()); 

    // convert base64 string back into movieclip 
    var newBmd:BitmapData = new BitmapData(origImg.width,origImg.height,"true",0xFFFFFFFF); 
    newBmd.setPixels(origBmd.rect, decoded);**// THIS THROWS AN " Error #2030: End of file was encountered."** 
    var image:Bitmap = new Bitmap(newBmd, "auto", true); 
    copy_mc.addChild(image); 
    copy_mc.x = origImg.width; 
} 

loader = new Loader(); 
req = new URLRequest("C:\\Data\\cutcord.jpg"); 
loader.load(req); 
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler); 
// movieclip to display original image 
orig_mc = new MovieClip(); 
orig_mc.addChild(loader); 
addChild(orig_mc); 
// movieclip to display image copy 
copy_mc = new MovieClip(); 
addChild(copy_mc); 

Anyone can help would be very appreciated :) 

回答

5

試試這個代碼:

var rect:Rectangle = new Rectangle(0,0,img.widht,img.height); 
    var byteArray:ByteArray = bitmapData.getPixels(rect); 
    var encoded:String = Base64.encodeByteArray(byteArray); 

    var decoded:ByteArray = Base64.decodeToByteArray(encoded); 
    decoded.position = 0; 
    var newBmd:BitmapData = new BitmapData(rect.width,rect.height,true,0xFFFFFFFF); 

    newBmd.setPixels(rect, decoded); 


    var image:Bitmap = new Bitmap(newBmd, "auto", true); 
    addChild(image); 

反正你在服務器心不是太好的存儲串的想法...爲什麼你不發送的ByteArray,不串? 您也可以將bitmapData編碼爲JPG或PNG,然後將字節發送到php。

3

,因爲它是一個形象,你有它在一個BitmapData,讓我們說「myBmp」 ......然後使用以下從的BitmapData提取所有數據:

var bytes:ByteArray = myBmp.getPixels(myBmp.rect); 

及以下寫:

myBmp.setPixels(myBmp.rect, bytes); 

注意,只有原始32比特的像素數據被存儲在所述的ByteArray,無壓縮,也不是原始圖像的尺寸。

對於壓縮,你應該參考corelib。

+0

我正在做你準備好的東西,但我一直在拋出這個錯誤:Error:Error#2030:End of file was encountered。是否因爲我的byteArray太大? – Bruce 2012-01-10 04:47:39

+0

90068是長度,當我做一個跟蹤btw – Bruce 2012-01-10 05:23:49

+0

有沒有一種方法來計算/從這樣的bytearray獲得原始尺寸? – SzRaPnEL 2013-07-15 23:06:45