How to convert component to Image in flex? - apache-flex

We know using file reference save method we can save the component as image in flex 3, but i am using flex 3.0 version, i did not get the save method in file reference class. Its available on flex 3.4.0 onwards.Do we have any other option to make the component to image in flex 3.0 compiler using flex builder IDE?
Thanks in advance

If this case allows to use HTML5, it could be possible.
public function capturePage(): void
{
var bmp:BitmapData = new BitmapData(this.width, this.height);
bmp.draw(this, new Matrix());
// convert to JPEG.
var fileData:ByteArray = new JPEGEncoder().encode(bmp);
var base64: Base64Encoder = new Base64Encoder();
base64.encodeBytes(fileData);
ExternalInterface.call("saveAsImage", "data:image/jpeg;base64,"+base64.toString());
}
index.template.html
<title>${title}</title>
<script language="JavaScript" type="text/javascript">
function saveAsImage(value)
{
// open new window. You will be able to save as image.
window.open(value);
// or use HTML5 FileWriter API here.
}
</script>
<script src="AC_OETags.js" language="javascript"></script>

private function doSave():void
{
var bmp:BitmapData = new BitmapData(canvas.width,canvas.height);
bmp.draw(canvas,new Matrix());
var filedate:ByteArray = new JPEGEncoder().encode(bmp);
var uploadURL:URLRequest = new URLRequest();
uploadURL.url = parentApplication.ServerUrl+"...../imageupload.php?fileName="+yourfilename;
uploadURL.contentType = 'application/octet-stream';
uploadURL.method = URLRequestMethod.POST;
uploadURL.data = filedate;
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
urlLoader.load(uploadURL);
}
PHP File : imageupload.php
<?php
$fileName = $_REQUEST['fileName'] . ".jpeg";
$fp = fopen( "../tempfolder/".$fileName, 'wb' );
fwrite( $fp, $GLOBALS[ 'HTTP_RAW_POST_DATA' ] );
fclose( $fp );
?>

Related

How to convert Image to ByteArray in Flex 4

I would like to write an image in pdf. I use AlivePDF and the method is pdf.addImageStream(image,...). The image is a ByteAray and I do not know how to convert my Image object in an ByteArray. I would appreciate if you can give me a suggestion.
1.load the image:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, beginDraw);
loader.load(new URLRequest(imgURL));
2.get the bitmapData and convert to ByteArray:
private function beginDraw(event:Event):void{
var bitmap:Bitmap = loader.content as Bitmap;
var rect:Rectangle = new Rectangle(0,0,bitmap.width. bitmap.height);
var result:ByteArray = bitmap.bitmapData.getPixels(rect);
}

Loading saved contents back in their places in flex air using flash builder 4.5

I have now gotten the idea of saving multiple components text into one file. But now when I open that file all the text in the file goes into the text of only one component. Here is the code,
<s:click>
var f:File = File.desktopDirectory;
f.browseForSave("Save As");
f.addEventListener(Event.SELECT, function (event:Event):void {
var stream:FileStream = new FileStream();
stream.open((event.target as File),FileMode.WRITE);
stream.writeUTFBytes(rte.htmlText);
stream.writeUTFBytes(ta.text);
stream.writeUTFBytes(rich.text);
stream.close();
});
</s:click>
</s:Button>
<s:click>
var f:File = File.desktopDirectory;
f.browseForOpen("Select file to open", []);
f.addEventListener(Event.SELECT, function (event:Event):void {
var fs:FileStream = new FileStream();
fs.open(event.target as File, FileMode.READ);
rte.htmlText = fs.readUTFBytes(fs.bytesAvailable);
ta.text = fs.readUTFBytes(fs.bytesAvailable);
fs.close();
});
</s:click>
</s:Button>
Some suggestions?
You already read all the stream from the fs.
As soon as you execute once fs.readUTFBytes(fs.bytesAvailable); the read header/pointer will be indicating to your end of your file, so in the next line the bytesAvailable will be zero.
I suggest you use the following code:
<s:click>
var f:File = File.desktopDirectory;
f.browseForOpen("Select file to open", []);
f.addEventListener(Event.SELECT, function (event:Event):void {
var fs:FileStream = new FileStream();
fs.open(event.target as File, FileMode.READ);
var dataStr:String = fs.readUTFBytes(fs.bytesAvailable);
rte.htmlText = dataStr;
ta.text = dataStr;
// if you want only the non formatted text use: ta.text = rte.text;
fs.close();
});
</s:click>

RSS feed in flex mobile project

I am working on accessing RSS feed using Flex mobile project. But i dont get image in that RSS feed,how to get image from RSS feed in Flex mobile project and how to convert an image into byte stream in Flex mobile project
But i dont get image in that RSS feed
RSS is xml. You need to looking for image elements, get url and load images separately
http://www.w3schools.com/rss/rss_tag_image.asp
how to convert an image into byte stream
In order to load image as ByteArray you need to use URLLoader for image loading:
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, loader_completeHandler);
function loader_completeHandler(event:Event):void
{
//here you can get loaded image as ByteArray
var imageData:ByteArray = loader.data;
}
If after this you need to show image in display list then:
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, loader_completeHandler);
var imageBytesLoader:Loader = new Loader();
imageBytesLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageBytesLoader_completeHandler);
function loader_completeHandler(event:Event):void
{
//here you can get loaded image as ByteArray
var imageData:ByteArray = loader.data;
imageBytesLoader.loadBytes(imageData);
}
function imageBytesLoader_completeHandler(event:Event):void
{
//here you can get loaded image as Bitmap
var bitmap:Bitmap = Bitmap(imageBytesLoader.content);
}

Simple file uploading using Flex only?

/Flex code/
private const UPLOAD_URL:String = "http://myhosting/upload/upload.php";
private var cer:FileFilter = new FileFilter("Archivos Cer", "*.cer");
private var key:FileFilter = new FileFilter("Archivos Key", "*.key");
private var fileref:FileReference = new FileReference();
private var fileref2:FileReference = new FileReference();
protected function button_clickHandler(event:MouseEvent):void
{
var request:URLRequest = new URLRequest();
request.method = URLRequestMethod.GET;
request.url = UPLOAD_URL;
fileref.upload(request);
fileref2.upload(request);
}
Well, that's the procedure I use to get my files up my server. The "upload.php" in my server is:
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
Is there a way to do this in FLEX only?
You mean, without PHP or some other server-side code? No, you can't. You need some code on the server which can accept the uploaded file.

PDF export to local using AlivePDF with flex

I need to export charts and data tables to pdf file in flex application.
For this we can user AlivePDF but i need to export to local not server.
Can we export to local system prompting user to select the location to export?
Thanks in advance.
Since FP10 the FileReference Class should support this via the save() function. The code to do this in Flash Player 10 or better is shown below:
var bytes:ByteArray = pdf.save(Method.LOCAL);
var file:FileReference = new FileReference();
file.save(bytes, "myPDF.pdf");
Try this
var pdfFile:PDF = new PDF();
var pdfByteArray:ByteArray = new ByteArray ();
pdfByteArray = pdfFile.save(Method.LOCAL);
With the latest version of AlivePDF (0.1.5 RC), you can do this:
var element:IBitmapDrawable; // Chart to export
var pdf:PDF = new UnicodePDF();
pdf.addPage();
var bitmapData:BitmapData = new BitmapData(element.width, element.height, false, 0xffffff);
try{
bitmapData.draw(element as IBitmapDrawable);
}catch(e:*)
{
throw new Error("bitmap draw failed");
}
var jpegencoder:JPEGEncoder = new JPEGEncoder(100);
var byteArray:ByteArray = jpegencoder.encode(bitmapData);
pdf.addImageStream(byteArray);
var file : FileReference = new FileReference()
file.save(pdf.save(Method.LOCAL),"my.pdf");

Resources