启网、虚拟主机、域名注册、服务器合租
精致合租、5人、10人、15人服务器合租、freebsd合租
当前位置:站长中国 > flex教程 > Flex图片另存相关

Flex图片另存相关

2009 - 02 - 04  作者:  来源:  浏览:618  评论: 发布评论 问高手
推荐:启网 - 专业的主机、服务器合租提供商 17hz.net - 5年服务器合租精品服务
    

阿辉的技术博客


Flex图片另存为的解决方法-类似可以解决另存为excel


我们知道Flex对于本地的限制比AIR要大,当我们想保存一个由Flex生成的文件必须借由服务器来完成,现在有一个需求就是,用户想保存Flex生成的图片在本地,我们要完成这个过程,必须先将Flex生成的图片转换为通用的数据格式,即ByteArray,然后由后台程序帮助写文件,形式上类似先上传,再下载,只不过中间不用保存实际的物理文件。


好了废话这么多把代码贴上来吧

Flex端


[复制到剪贴板]CODE::

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

<mx:Script>

<![CDATA[

import mx.graphics.codec.JPEGEncoder;

import mx.graphics.ImageSnapshot;

private function saveAs(){

var en:JPEGEncoder = new JPEGEncoder(100); //压缩图片,100是指质量

var ba:ByteArray=en.encode(ImageSnapshot.captureBitmapData(img));//将控件转为BitmapData后再转为ByteArray

var request:URLRequest = new URLRequest("/TestForLCDS/servlet/UploadServlet");

request.method="POST";

request.data=ba;

request.contentType = "application/octet-stream"; //这个很重要,设置成流数据

navigateToURL(request,"_blank"); //因为要浏览器触发下载事件,所以就不用异步方式打开连接了

}

]]>

</mx:Script>

<mx:Button x="228" y="10" label="另存为本地图片" click="saveAs()"/>

<mx:Image id="img" x="10" y="10" source="img.jpg" width="200" height="200" scaleContent="false"/>

</mx:Application>


 


沙漠的孤鱼


原文地址:


Save a local image file (JPEG or PNG) from an image snapshot


如果你想保存从视频,图表或表格中获取的图片数据到本地,可以使用BitmapData类。


摘要:


使用BitmapData类来创建一个包含了从组件中获取的图片数据的对象,使用mx.graphics.codec包提供的方法编码为JPEG或PNG格式,然后使用AIR API提供的File和FileStream类保存到本地。


具体方法:


首先我们我们需要得到屏幕的截图,要做到这一点,我们要使用BitmapData类。比如我们想从一个命名为myChart的线状图表上获取截图:


import flash.display.BitmapData;

var bmpd:BitmapData = new BitmapData(myChart.width,myChart.height);

bmpd.draw(myChart);


然后我们需要把bitmapdata对象编译为ByteArray对象,这样我们就可以保存为文件了。这个ByteArray对象需要被格式化,我们可以使用mx.graphics.codec包中的JPEGEncoder和PNGEncoder类来实现它。


编码为JPEG格式:


import mx.graphics.codec.JPEGEncoder;

//create a new instance of the encoder, and set the jpeg compression level from 0 to 100

var jpgenc:JPEGEncoder = new JPEGEncoder(80);

//encode the bitmapdata object and keep the encoded ByteArray

var imgByteArray:ByteArray = jpgenc.encode(bmpd);


编码为PNG格式:


import mx.graphics.codec.JPEGEncoder;

//create a new instance of the encoder

var pngenc:PNGEncoder = new PNGEncoder();

//encode the bitmapdata object and keep the encoded ByteArray

var imgByteArray:ByteArray = pngenc.encode(bmpd);


现在我们已经准备好了ByteArray数据,我们只需要把它保存到本地就可以了。我们可以用File和File Stream类来实现。


建立一个JPEG文件参照:


//gets a reference to a new empty jpg image file in user desktop

var fl:File = File.desktopDirectory.resolvePath(”snapshot.jpg”);


建立一个PNG文件参照:


//gets a reference to a new empty jpg image file in user desktop

var fl:File = File.desktopDirectory.resolvePath(”snapshot.png”);


现在我们可以把ByteArray用File Stream保存到文件中。


//Use a FileStream to save the bytearray as bytes to the new file

var fs:FileStream = new FileStream();

try{

//open file in write mode

fs.open(fl,FileMode.WRITE);

//write bytes from the byte array

fs.writeBytes(imgByteArray);

//close the file

fs.close();

}catch(e:Error){

trace(e.message);

}


参加以下的示例:


Sample applications

save_img.zip


Tags: AIR, AIR教


 


/*******************************************************************/


 


SWF to PNG with Actionscript 3.0 - ByteArray class

Greetings to all!


Today, I am proud of this very interesting article.

Not that the other ones are not interesting but this one beats them all from my point of view.


Have you ever thought on how to extract an image of format .png from a SWF?


With Actionscript 2.0, I personally had done it for an application using the BitmapData class and the library GD of PHP. It was certainly not very performing but it was the only way to do it at the time.


With Actionscript 3.0, we have the ByteArray class available that allows us to script a BitmapData into a ByteArray.

As my knowledge of binary programming are not sufficient to realise all of it on my own, I found on the web a script from Adobe, which scripts a BitmapData and returns a ByteArray.

I then realised that the ByteArray was not ready to be passed to a server side script and so I asked help to a friend of mine, an engineer named Daniele Lazzara of OrigamiStudio.it with a real virtue in informatics and a big passion for Flash. He used a codification created by Steve Webster & dynamicflash.com to convert the ByteArray to a String and implemented a PHP script to complete the job.

Finally, I implemented a call to the PHP script from Flash and?it works wonderfully!


The main steps of Main.as, the Document Class of this project are as follow:


Properties

a BitmapData

private var bitmap_data:BitmapData;

a ByteArray

private var byte_array:ByteArray;

I instance the Adobe PNGEncoder class

var encoding:PNGEncoder;

I create a new BitmapData

bitmap_data=new BitmapData(stage.stageWidth,stage.stageHeight,fals e,0xDEDEDE);

Using the method ?draw? of the BitmapData, I take a ?screenshot? of the MovieClip that realise the drawing

bitmap_data.draw(clip);

I assign to my ByteArray a value returned by the method ?encode? of the Adobe PNGEncoder class, passing to it the BitmapData

byte_array=PNGEncoder.encode(bitmap_data);

I create a String variable and assign to it the value returned by the static method ?encodeByteArray? of the Base64 class by Steve Webster, passing to it the ByteArray

var encoded:String=Base64.encodeByteArray(byte_array);

By now, you should understand easily the rest as I send the data to the PHP script

var variables:URLVariables=new URLVariables();

variables.png=encoded;

var richiesta:URLRequest=new URLRequest();

richiesta.url='http://www.flepstudio.org/swf/mix/SWF_to_PNG/prova2.php';

richiesta.method=URLRequestMethod.POST;

richiesta.data=variables;

var loader:URLLoader=new URLLoader();

loader.dataFormat=URLLoaderDataFormat.BINARY;



推荐教程