2012-06-29 33 views
0

我正在使用以下功能來保存SWF的輸出並將其發送到PHP頁面,以便它創建JPG,如果沒有INTERNET連接並且SWF位於CDROM中,保存功能在FLASH中使用,以便輸出JPG並保存到電腦上。將JPG從CDROM保存到PC

總之,我們可以保存影片剪輯輸出爲JPG

/** 
    Screenshot and jpg output 
    **/ 
    import flash.display.BitmapData; 
    import flash.geom.Matrix; 

    //Buttons handlers. Should add an extra function because delegate doesn't allow to pass parameters 
    shaF.onPress = mx.utils.Delegate.create(this,makeShadow); 

    //Helper functions to pass parameters 
    function makeShadow() { capture(0) } 

    /* 
    create a function that takes a snapshot of the Video object whenever it is called 
    and shows in different clips 
    */ 
    function capture(nr){ 
    this["snapshot"+nr] = new BitmapData(abc._width,abc._height); 

    //the bitmap object with no transformations applied 
    this["snapshot"+nr].draw(abc,new Matrix()); 
    var t:MovieClip = createEmptyMovieClip("bitmap_mc"+nr,nr); 

    //positions clip in correct place 
    //t._x = 350; t._y = 10+(nr*130); t._xscale = t._yscale = 50 

    //display the specified bitmap object inside the movie clip 
    t.attachBitmap(this["snapshot"+nr],1); 
    output(nr); 
    //attachMovie("print_but", "bot"+nr, 100+nr, {_x:t._x+t._width+50, _y:t._y+t._height/2}) 
    } 
    //Create a new bitmapdata, resize it 50 %, pass image data to a server script 
    // using a LoadVars object (large packet) 
    function output(nr){ 
     //Here we will copy pixels data 
     var pixels:Array = new Array() 
     //Create a new BitmapData 
     var snap = new BitmapData(this["snapshot"+nr].width, this["snapshot"+nr].height); 
     //Matrix to scale the new image 
     myMatrix = new Matrix(); 
     myMatrix.scale(1, 1) 


     //Copy image 
     snap.draw(this["snapshot"+nr], myMatrix); 

     var w:Number = snap.width, tmp 
     var h:Number = snap.height 
     //Build pixels array 
     for(var a=0; a<=w; a++){ 
     for(var b=0; b<=h; b++){ 
     tmp = snap.getPixel32(a, b).toString(16) 
     //if(tmp == "-fcffff") 
     //{ 
     //tmp="-ff0000"; 
     //} 

     pixels.push(tmp.substr(1)) 
     } 
     } 
     //Create the LoadVars object and pass data to PHP script 
     var output:LoadVars = new LoadVars() 
     output.img = pixels.toString() 
     output.height = h 
     output.width = w 
     //The page (and this movie itself) should be in a server to work 
     output.send("show.php", "output", "POST")  
    } 
    stop() 
+0

爲FP 10及更高版本啓用了客戶端保存功能,以前版本的FP需要往返服務器。下面的答案省略了這些信息,否則兩者都是有效的。 – shaunhusain

回答

1

既然你已經有BitmapData,這是相當容易的。修改您的output功能:

//Copy image 
snap.draw(this["snapshot"+nr], myMatrix); 

//Now check if we want to save as JPG instead of sending data to the server 
if (runningFromCdrom) { 
    var encoder:JPEGEncoder = new JPEGEncoder(); 
    var bytes:ByteArray = encoder.encode(snap); 
    var file:FileReference = new FileReference(); 
    file.save(bytes); 
} else { 
    // ...the rest of your output method... 
} 

如何確定runningFromCdrom值是由你。如果SWF在HTML文檔中運行,判斷程序是否從CD-ROM運行的最好方法是在FlashVars中指定它。

+0

使用代碼後出現此錯誤 stop() Total ActionScript Errors:5 Reported Errors:5 – user580950

+0

這無助於識別錯誤。當你編譯代碼或者運行它時會發生嗎?爲什麼錯誤提到'stop()'?如果調用它之前它仍然可以工作。 – CheeseWarlock