0

我正在使用以下函数来保存 SWF 的输出并将其发送到 PHP 页面以便它创建 JPG,如果没有 INTERNET 连接并且 SWF 在 CDROM 中,我的问题可以在 FLASH 中使用 Save 函数以便它输出JPG并将其保存在计算机上。

简而言之,我们可以将影片剪辑输出另存为 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()
4

2 回答 2

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 中指定它。

于 2012-06-29T15:44:03.530 回答
1

是的,您可以直接从 Flash 保存 JPEG。不需要 PHP 的中间步骤。

您可以使用以下 JPEG 编码器之一:

https://github.com/mikechambers/as3corelib

http://www.switchonthecode.com/tutorials/flex-tutorial-an-asynchronous-jpeg-encoder

我会推荐第二个,因为它可以异步工作(有点),这意味着您的 UI 在编码 JPEG 时不应锁定。

于 2012-06-29T15:46:34.523 回答