I tried to capture the screenshot of a rendered SWF file(chart) and convert it to image by using the following code. But, the output image contains only some part of the actual chart rendered. It is not capturing entire rendered chart...please help me out...Thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
minWidth="955" minHeight="600"
backgroundColor="white" viewSourceURL="srcview/index.html" initialize="initApp()">
<mx:Script>
<![CDATA[
import mx.core.IUIComponent;
import mx.graphics.ImageSnapshot;
import mx.graphics.codec.PNGEncoder;
import flash.net.FileReference;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.controls.Alert;
private var image:ImageSnapshot = new ImageSnapshot;
private var dataXML:String="<chart labelDisplay='Normal' xAxisName='' yAxisName='' pyAxisName='' syAxisName='' rotateYAxisName='' caption='' subCaption='' baseFont='Regular' baseFontSize='10' baseFontColor='' decimalPrecision='' thousandSeparator=',' decimalSeparator='.' numberPrefix='' numberSuffix='' sNumberPrefix='' sNumberSuffix='' bgColor='' showBorder='1' YAxisMinValue='' YAxisMaxValue='' pyAxisMinValue='' pyAxisMaxValue='' syAxisMinValue='' syAxisMaxValue='' showLabels='1' showValues='1' showLegend='Y' legendPosition='Right'><categories><category label='Unmapped'/><category label='FY 2002 - 03'/><category label='FY 2003 - 04'/><category label='FY 2004 - 05'/><category label='FY 2005 - 06'/><category label='FY 2006 - 07'/><category label='FY 2007 - 08'/><category label='FY 2008 - 09'/><category label='FY 2009 - 10'/><category label='FY 2010 - 11'/></categories><dataset seriesName='Base Amount'><set value='-65770'/><set value='71461203'/><set value='66548822'/><set value='87063456'/><set value='261797187'/><set value='282962118'/><set value='3830823027'/><set value='16001588683'/><set value='22514728943'/><set value='23822586701'/></dataset></chart>";
private function initApp():void
{
chart_1.source="MSCombi2D.swf?chartWidth=100&chartHeight=100®isterWithJS=1&dataXML="+dataXML;
}
private function takeSnapshot(source:IBitmapDrawable):void {
// var imageSnap:ImageSnapshot = ImageSnapshot.captureImage(source);
// var imageByteArray:ByteArray = imageSnap.data as ByteArray;
// var str:String = imageByteArray.
// swfLoader.load(imageByteArray);
var imageBitmapData:BitmapData = ImageSnapshot.captureBitmapData(source);
swfLoader.source = new Bitmap(imageBitmapData);
// take a screen capture
// image = ImageSnapshot.captureImage(source, 72, new PNGEncoder(), false);
// var fileSave:FileReference = new FileReference();
// fileSave.save(image.data, "saveImage.png");
}
]]>
</mx:Script>
<mx:ApplicationControlBar dock="true">
<mx:Button label="Take snapshot of DataGrid"
click="takeSnapshot(chart_1);" />
</mx:ApplicationControlBar>
<mx:HBox>
<mx:SWFLoader id="chart_1" />
<mx:SWFLoader id="swfLoader">
<mx:filters>
<mx:DropShadowFilter />
</mx:filters>
</mx:SWFLoader>
</mx:HBox>
I got the answer...i changed the code for the function "takeSnapshot" as follows:
private function takeSnapshot(source:IBitmapDrawable):void
{
var bitmapData:BitmapData = new BitmapData( 1350, 700 );
bitmapData.draw( IBitmapDrawable( chart_1 ));
var swfBitmap:Bitmap = new Bitmap( bitmapData );
var jpg:JPEGEncoder = new JPEGEncoder(100);
var ba:ByteArray = jpg.encode(swfBitmap.bitmapData);
var fileSave:FileReference = new FileReference();
fileSave.save(ba, "saveImage.jpg");
}
Related
I want to get motherboard ID in flex builder. This is my code. I follow this code to build my own Retrieve manufacturer information from device that AIR app is running on
But I got this error TypeError: Error #1009: Cannot access a property or method of a null object reference.
give me some solution please.
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" height="100%" showStatusBar="false" verticalAlign="middle"
name="Login" windowComplete="init()">
<mx:Script>
<![CDATA[
import flash.desktop.NativeProcess;
import flash.desktop.NativeProcessStartupInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.filesystem.File;
private var process:NativeProcess;
private function init():void
{
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var file:File = new File("C:/Windows/System32/cmd.exe");
nativeProcessStartupInfo.executable = file;
var processArgs:Vector.<String> = new Vector.<String>();
processArgs.push("wmic baseboard get serialnumber");
nativeProcessStartupInfo.arguments = processArgs;
process.start(nativeProcessStartupInfo);
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
process.exit();
}
private function onOutputData(event:ProgressEvent):void
{
var data:String = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable);
txtreq.text = data;
}
]]>
</mx:Script>
<mx:Panel id="panel1" width="368" height="84" borderAlpha="0.47" dropShadowVisible="true"
horizontalCenter="-10" layout="absolute" title="Activation" verticalCenter="-10">
<mx:TextInput id="txtreq" x="10" y="10" width="293" editable="true"/>
</mx:Panel>
</mx:WindowedApplication>
this is error..
http://i.stack.imgur.com/QoTfb.png
I would like to ask how to load variables from XML to flash builder. Is it possible? I think that I load variables, but my AS3 is done. I mean that variables from XML are loaded after doing script. Any idea how to do it? Thank for advices
Asumming that you will have this xml (this code works perfectly):
<root>
<child id="1">
<detail>
<detailchild description="detail 1 child"/>
<detailchild description="detail 2 child"/>
</detail>
</child>
OpenXml.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com
/flex/spark" firstView="views.OpenXmlHomeView" applicationDPI="160">
</s:ViewNavigatorApplication>
OpenXmlHomeView
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView" viewActivate="view_activateHandler()">
<fx:Script>
<![CDATA[
import mx.collections.XMLListCollection;
public function view_activateHandler():void{
//Open a XML file
var ldr:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("../source.xml");
ldr.addEventListener(Event.COMPLETE, onLoad);
ldr.load(request);
}
private function onLoad(e:Event):void
{
var arr:Array = new Array();
var ldr:URLLoader = URLLoader(e.target);
//XML was loaded then read
var myxml:XML = new XML(ldr.data);
var xmlList:XMLListCollection = new XMLListCollection(myxml.children());
//Loop over XML childs to load a Question Array
for each(var obj:Object in xmlList){
arr.push(obj.#id);
var xmlChildren:XMLList = obj.detail;
for each (var qt:Object in new XMLListCollection(xmlChildren.children())){
arr.push(qt.#description);
}
}
lbl.text = arr.toString();
}
]]>
</fx:Script>
<s:Label id="lbl" />
</s:View>
This code shows:
1, detail 1 child, detail 2 child
You can download this project here.
Note: This is a Flex Mobile project.
Sample XML (message.xml)
<?xml version="1.0" encoding="utf-8" ?>
<category>
<movie date = "13/07/31" genre = "Action">
<title>foo</title>
<rate>4.5</rate>
</movie>
</category>
How to parsing XML to variables?
try this:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class XML_Parsing_Tester extends Sprite
{
private var movieDate:String;
private var movieGenre:String;
private var movieTitle:String;
private var movieRate:Number;
public function XML_Parsing_Tester()
{
var loader:URLLoader = new URLLoader();
loader.load(new URLRequest("message.xml"));
loader.addEventListener(Event.COMPLETE, onLoaded);
}
private function onLoaded(e:Event):void
{
var xml:XML = new XML(e.target.data);
movieDate = xml.movie.#date;
movieGenre = xml.movie.#genre;
movieTitle = xml.movie.title;
movieRate = xml.movie.rate;
}
}
}
yesterday me and my friend Ketan thakkar were working on an issue related with drag drop in flex.
Image can dragged easily if it is has direct your or embedded image.
But if we try to drag image which has a source from flex loader, original image loos the parent and then never go back to its original place. We tried to find the solution and with a good luck we got success.
the code is below. If anyone know why this problem exist please help us.
For now we have managed it this way.
<?xml version="1.0" encoding="utf-8"?>
<mx:Script>
<![CDATA[
import mx.controls.Image;
import mx.core.DragSource;
import mx.core.FlexLoader;
import mx.core.UIComponent;
import mx.events.DragEvent;
import mx.events.FlexEvent;
import mx.graphics.ImageSnapshot;
import mx.managers.DragManager;
private var fl:FlexLoader = new FlexLoader();
private var img1:Image = new Image();
private function doDragEnter(event:DragEvent):void
{
DragManager.acceptDragDrop(UIComponent(event.target));
}
private function doDragDrop(event:DragEvent):void
{
var img:Image;
if (event.dragInitiator.parent == dropCanvas)
img = event.dragInitiator as Image;
else
{
img = new Image();
img.width = img.height = 120;
img.source = img1.source;
img.addEventListener(MouseEvent.MOUSE_DOWN, doDragStart);
dropCanvas.addChild(img);
}
img.x = event.localX - (event.dragSource.dataForFormat("localX") as Number);
img.y = event.localY - (event.dragSource.dataForFormat("localY") as Number);
}
private function doDragStart(event:MouseEvent):void
{
var imageSnap:ImageSnapshot = ImageSnapshot.captureImage(event.currentTarget as IBitmapDrawable);
var imageByteArray:ByteArray = imageSnap.data as ByteArray;
img1.load(imageByteArray);
var dragInitiator:Image = event.currentTarget as Image;
var dragSource:DragSource = new DragSource();
var dragProxy:Image = new Image();
dragProxy.source = img1.source;
dragProxy.x = mouseX-25;
dragProxy.y = mouseY-25;
dragProxy.width = 80;
dragProxy.height= 80;
DragManager.doDrag(dragInitiator, dragSource, event, dragProxy,0,0,1,false);
}
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
fl.contentLoaderInfo.addEventListener(Event.COMPLETE, oncomplete);
fl.load( new URLRequest('assets/1.swf'));
}
private function oncomplete(event:Event):void
{
img.source = fl;
}
]]>
</mx:Script>
<controls:FlexBook width="400" height="200"
itemSize="halfPage">
<controls:content>
<mx:Image id="img" mouseDown="doDragStart(event)" /><!--source="#Embed('assets/Hawk.jpg')"-->
<mx:Image id="img11" mouseDown="doDragStart(event)" /><!--source="#Embed('assets/Hawk.jpg')"-->
<mx:Image id="img2" mouseDown="doDragStart(event)" /><!--source="#Embed('assets/Hawk.jpg')"-->
</controls:content>
</controls:FlexBook>
<mx:Canvas id="dropCanvas" width="100%" height="100%" borderColor="#1C5CC7" dragEnter="doDragEnter(event)" dragDrop="doDragDrop(event)" borderStyle="solid" cornerRadius="20" borderThickness="6" backgroundColor="#7E92FC"/>
<!--<mx:Image id="dropImage" source="assets/1.swf" />-->
I had a similar problem, the problem is in this line:
dragProxy.source = img1.source;
you should to copy bitmap img1 like here instead linking it.
In the following code i am trying to save the microphone contents to a file.The saved file doesn't play and Every time the file is saved i see that the size is only of 10 bytes only.What am i doing wrong in the code.Can someone please show me the correct code to save it .And the saved file should play the recorded contents accordingly.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import flash.events.SampleDataEvent;
import flash.media.Microphone;
import flash.net.FileReference;
import mx.controls.Alert;
import flash.net.FileReference;
import flash.display.Sprite;
import flash.media.Sound;
import flash.utils.ByteArray;
import flash.external.ExternalInterface;
public var _file:FileReference = new FileReference();
[Bindable] private var micList:Array;
public var mic:Microphone = Microphone.getMicrophone();
protected var isRecording:Boolean = false;
protected function startMicRecording():void
{
//var mic:Microphone = Microphone.getMicrophone();
mic.gain = 60;
mic.rate = 11;
mic.setUseEchoSuppression(true);
mic.setLoopBack(true);
mic.setSilenceLevel(5, 1000);
Alert.show("In recording");
isRecording = true;
mic.addEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
}
protected function stopMicRecording():void
{
//isRecording = false;
try{
//_file.save( SampleDataEvent.SAMPLE_DATA, "recorded.wav" );
_file.save(SampleDataEvent.SAMPLE_DATA , "recorded.flv" );
}
catch(e:Error)
{
Alert.show("In Stopmicrecording"+e);
}
}
private function gotMicData(micData:SampleDataEvent):void
{
//mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
}
protected var soundRecording:ByteArray;
protected var soundOutput:Sound;
protected function playbackData():void
{
}
private function playSound(soundOutput:SampleDataEvent):void
{
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:ComboBox x="150" id="comboMicList" dataProvider="{micList}" />
<mx:Button x="250" id="startmicrec" label="Start Rec" click="startMicRecording()"/>
<mx:Button x="350" id="stopmicrec" label="Stop Rec" click="stopMicRecording()"/>
<!--<mx:Button x="50" id="setupmic" label="Select Mic" click="setupMicrophone()"/>-->
<mx:Button x="450" id="playrecsound" label="Play sound" click="playbackData()"/>
</s:Application>
You need to store the data that is handed to you in gotMicData into a ByteArray and then save that ByteArray. You are saving the event name, which is a string (10 characters long).
Once you do that, you need to load the file and hand sample data to the sound. You play the sound back 8 times... because you sampled at 11 KHz but the sound plays back at 44 KHz (4x writing) and the sound is Stereo but the mic is mono (2x again).
You can't save the data as a WAV file directly... you recorded raw data. If you want to go through the trouble of writing a proper WAV header, then you don't have to play the games of handing sample data and hand the file to the Sound object. That is an exercise outside of the scope of this question.
Good luck!
import mx.controls.Alert;
public var mic:Microphone = Microphone.getMicrophone();
public var recordedData:ByteArray;
protected function startMicRecording():void
{
mic.gain = 60;
mic.rate = 11;
mic.setUseEchoSuppression(true);
mic.setLoopBack(false);
mic.setSilenceLevel(5, 1000);
recordedData = new ByteArray();
mic.addEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
}
protected function stopMicRecording():void
{
mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
try{
var file:FileReference = new FileReference();
file.save(recordedData, "recorded.dat" );
}
catch(e:Error)
{
Alert.show("In Stopmicrecording"+e);
}
}
private function gotMicData(sample:SampleDataEvent):void
{
recordedData.writeBytes(sample.data, 0, sample.data.bytesAvailable);
}
protected var playbackFile:FileReference;
protected var soundRecording:ByteArray;
protected var soundOutput:Sound;
protected function playbackData():void
{
playbackFile = new FileReference();
playbackFile.addEventListener(Event.SELECT, playbackFileSelected);
playbackFile.browse();
}
private function playbackFileSelected(event:Event):void {
playbackFile.addEventListener(Event.COMPLETE, playbackFileLoaded);
playbackFile.load();
}
private function playbackFileLoaded(event:Event):void {
soundRecording = playbackFile.data;
soundOutput = new Sound();
soundOutput.addEventListener(SampleDataEvent.SAMPLE_DATA, moreInput);
soundOutput.play();
}
private function moreInput(event:SampleDataEvent):void {
var sample:Number;
for (var i:int = 0; i < 1024; i++) {
if (soundRecording.bytesAvailable > 0) {
sample = soundRecording.readFloat();
// write the same byte 8 times:
// Upsample from 11 KHz to 44 KHz (x4)
// Mono to Stereo (x2)
for(var x:int = 0; x < 8; x++)
event.data.writeFloat(sample);
}
}
}
I am trying to run the simple code as following:
<fx:Script>
<![CDATA[
import flash.display.*;
import flash.net.URLRequest;
var url2:String = "image2.jpg";
var urlRequest:URLRequest = new URLRequest(url2); //problem code
var loader:Loader = new Loader(); //problem code
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete); //problem code
loader.load(urlRequest);
addChild(loader);
function loader_complete(evt:Event):void {
var target_mc:Loader = evt.currentTarget.loader as Loader;
target_mc.x = (stage.stageWidth - target_mc.width) / 2;
target_mc.y = (stage.stageHeight - target_mc.height) / 2;
}
]]>
</fx:Script>
However, I got errors saying
'access of undefined property 'loader_complete'
'access of undefined property 'loader'
'access of undefined property 'loader'
'access of undefined property 'loader'
'access of undefined property 'urlReguest'
I haven't used Flash builder for a while and need some assistants. Any thoughts? Thanks for the help.
You are attempting to execute code in the class definition.
Executable code should be called from a function, such as creation complete in the Flex component lifecycle:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import flash.display.*;
import flash.net.URLRequest;
import mx.events.FlexEvent;
public var url2:String = "image2.jpg";
public var urlRequest:URLRequest = new URLRequest(url2); //problem code
public var loader:Loader = new Loader(); //problem code
protected function creationCompleteHandler(event:FlexEvent):void
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete); //problem code
loader.load(urlRequest);
addChild(loader);
}
protected function loader_complete(evt:Event):void
{
var target_mc:Loader = evt.currentTarget.loader as Loader;
target_mc.x = (stage.stageWidth - target_mc.width) / 2;
target_mc.y = (stage.stageHeight - target_mc.height) / 2;
}
]]>
</fx:Script>
</s:Application>