将JPG从CDROM保存到PC

时间:2012-06-29 14:46:34

标签: actionscript-3

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

简而言之,我们可以将movieclip输出保存为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()

2 个答案:

答案 0 :(得分: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中指定它。

答案 1 :(得分:1)

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

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

https://github.com/mikechambers/as3corelib

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

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