Find motherboard ID with flex builder? - actionscript-3

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

Related

Flash builder variable from xml

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;
}
}
}

TextFormat working in raw AS3 but not working in Flex

This problem is driving me bananas because it seems to be so simple. I'm trying to apply a text format to a text field created with code and have the formatting remain applied to the textfield if the text changes. The following code works as expected in raw AS3. I've broken down these examples to be as simplistic as possible.
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
public class Testing extends Sprite
{
public function Testing()
{
var tf:TextField = new TextField();
addChild(tf);
tf.border = true;
var tfor:TextFormat = new TextFormat();
tfor.align = 'right';
tfor.size = 30;
tf.defaultTextFormat = tfor;
tf.text = 'Testing';
}
}
}
However, similar code in Flex does not behave the same way. The following code results in the text not being formatted correctly.
<?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"
creationComplete="_create(event)">
<fx:Script>
<![CDATA[
import mx.core.UITextField;
import mx.events.FlexEvent;
protected function _create(event:FlexEvent):void
{
var tf:UITextField = new UITextField();
ui.addChild(tf);
tf.border = true;
var tfor:TextFormat = new TextFormat();
tfor.align = 'right';
tfor.size = 30;
tf.defaultTextFormat = tfor;
tf.text = 'Testing';
}
]]>
</fx:Script>
<mx:UIComponent id="ui" width="100%" height="100%" />
</s:Application>
I realize that I could just use a Flex component as the text field and stick formatting on it that way, but this code needs to play nicely with previously written code. Thanks for your help in advance.
Below code may help you: - instead of adding it to UIComponent add it to SpriteVisualElement it will work.
<?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"
creationComplete="_create(event)">
<fx:Script>
<![CDATA[
import mx.core.UITextField;
import mx.events.FlexEvent;
protected function _create(event:FlexEvent):void
{
var tf:UITextField = new UITextField();
ui.addChild(tf);
tf.border = true;
var tfor:TextFormat = new TextFormat();
tfor.align = 'right';
tfor.size = 30;
tf.defaultTextFormat = tfor;
tf.text = 'Testing';
}
]]>
</fx:Script>
<s:SpriteVisualElement id="ui" width="100%" height="100%" />
</s:Application>

AS3 - ImageSnapshot - not taking snapshot of entire component

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&registerWithJS=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");
}

Flex: Why are TileList images disappearing on drag?

I'm developing a Flex Air (desktop) application that loads images from the local filesystem into a TileList. The user will then be able to drag (copy) these images out of the list onto another control.
I've finally got the images showing up correctly (and not disappearing after scrolling the TileList) but they seem to disappear from the TileList at the start of a drag operation.
I come from a .NET background and am just learning AS3/Flex, so if you see me using any anti-patterns here, feel free to point them out!
Sample code follows (I've tried to make this as minimal as possible).
Test.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
[Bindable]
protected var _pics:ArrayCollection = new ArrayCollection();
protected function picsList_creationCompleteHandler(event:FlexEvent):void
{
const imageFolderPath:String = "c:\\users\\bbrooks\\Pictures";
var imageDir:File = new File(imageFolderPath);
var imageFiles:Array = imageDir.getDirectoryListing();
for each(var imageFile:File in imageFiles)
{
_pics.addItem(new PictureObject(imageFile.nativePath));
}
// give images a chance to load
var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, onTimerExpired);
timer.start();
}
protected function onTimerExpired(event:TimerEvent):void
{
picsList.dataProvider = _pics;
}
]]>
</fx:Script>
<mx:TileList id="picsList" x="0" y="0" width="100%" height="100%" dragEnabled="true" dragMoveEnabled="false"
creationComplete="picsList_creationCompleteHandler(event)" >
<mx:itemRenderer>
<fx:Component>
<mx:Image width="75" height="75" source="{data.image}" />
</fx:Component>
</mx:itemRenderer>
</mx:TileList>
</s:WindowedApplication>
PictureObject.as:
package
{
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.net.URLRequest;
import mx.controls.Image;
[Bindable]
[RemoteClass]
public class PictureObject extends Object
{
protected var _image:Image = null;
public function get image():Image { return _image; }
public function set image(newValue:Image):void { _image = newValue; }
public function PictureObject(path:String)
{
var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
imageLoader.load(new URLRequest(path));
}
protected function onImageLoaded(e:Event):void
{
var imageLoader:Loader = LoaderInfo(e.target).loader;
var bmp:Bitmap = Bitmap(imageLoader.content);
_image = new Image();
_image.smoothBitmapContent = true;
_image.source = new Bitmap(bmp.bitmapData);
_image.width = imageLoader.width;
_image.height = imageLoader.height;
}
}
}
I will mostly reply to your secondary question (guessing it will resolve the primary in one fell swoop): as anti-patterns go, this is not a bad example ;)
You are manually loading the images while the Image class has the loading built-in; just give a URL to its source property and it will automatically load it.
You are setting an Image instance as the source of another Image instance; the source property expects URLs or ByteArrays; I'm surprised it doesn't throw an error; the Image class is probably smart enough to extract the source of the other Image instance.
The Timer is redundant. As said, an Image automatically takes care of loading its content.
The s:Image tag isn't wrapped in an ItemRenderer and hence should have no access to a data property: this code shouldn't even compile.
There's no point in having a Bindable property _pics if you don't plan on binding it.
You use the mx TileList. Why not use the more "modern" Spark version? (This doesn't mean the mx class won't work in a Spark application though).
So you have a lot of deleting to do. You can scrap the PictureObject class altogether; remove the Timer code; and just add the URL Strings to the _pics collection. As a plus you could also replace the mx TileList with a Spark List with TileLayout; something like this:
<s:List id="picsList">
<s:layout>
<s:TileLayout />
</s:layout>
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer>
<s:Image source="{data}" />
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
The ActionScript part could be reduced to this:
const imageFolderPath:String = "c:\\users\\bbrooks\\Pictures";
var imageDir:File = new File(imageFolderPath);
var imageFiles:Array = imageDir.getDirectoryListing();
picsList.dataProvider = new ArrayCollection(imageFiles);
Thanks RIAstar. Your answer put me on the track to solving the problem. The new sample code appears below.
The original problem seems to have been with the mx:Image control. Not sure why, but using the s:Image control seems to work.
Granted, this could be accomplished without the PictureObject class at all by simply setting the data source to a list of file paths, but I'm using the image data later and I wanted to get it working by supplying the image data to the custom renderer dynamically.
Test.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import spark.components.Image;
protected var _pics:ArrayCollection = new ArrayCollection();
protected function picsList_creationCompleteHandler(event:FlexEvent):void
{
const imageFolderPath:String = "c:\\users\\bbbrooks\\Pictures";
var imageDir:File = new File(imageFolderPath);
var imageFiles:Array = imageDir.getDirectoryListing();
for each(var imageFile:File in imageFiles)
{
if (imageFile.extension == "jpg")
{
_pics.addItem(new PictureObject(imageFile.nativePath));
}
}
// give images a chance to load
var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, onTimerExpired);
timer.start();
}
protected function onTimerExpired(event:TimerEvent):void
{
picsList.dataProvider = _pics;
}
]]>
</fx:Script>
<s:List id="picsList" x="0" y="0" width="100%" height="100%"
creationComplete="picsList_creationCompleteHandler(event)"
dragEnabled="true" dragMoveEnabled="false">
<s:layout>
<s:TileLayout />
</s:layout>
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer>
<s:Image id="imageDisplay"
width="75" height="75" source="{data.bmp}" />
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
</s:WindowedApplication>
PictureObject.as
package
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.net.URLRequest;
import mx.controls.Image;
[RemoteClass]
[Bindable]
public class PictureObject extends Object
{
protected var _bmp:Bitmap = null;
public function get bmp():Bitmap { return _bmp; }
public function set bmp(newValue:Bitmap):void { _bmp = newValue; }
public function PictureObject(path:String)
{
var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
imageLoader.load(new URLRequest(path));
}
protected function onImageLoaded(e:Event):void
{
var imageLoader:Loader = LoaderInfo(e.target).loader;
// create our own copy of the bits in the Loader
var bmp:Bitmap = Bitmap(imageLoader.content);
_bmp = new Bitmap( Bitmap(imageLoader.content).bitmapData );
}
}
}

how to unzip the files using base64 encoding in actionscript?

I have a issue here with unzipping the .zip files that i have in combobox.i select 1 .zip file and after selecting the particular .zip file Button should unzip it and put the contents in another combobox.Can someone help me here?
here is a code:
// ActionScript file
import flash.display.*;
import flash.events.*;
import flash.utils.ByteArray;
import com.Base64;
import mx.controls.Alert;
[Bindable] private var sfile:FileReference;
[Bindable] private var zipdataProvdr:ArrayCollection = new ArrayCollection([{label: "test", file: "test"},{label: "elm34001", file: "elm34001"}, {label: "elm34003", file: "elm34003"}, {label: "elm34005", file: "elm34005"}, {label: "elm34009", file: "elm34009"},{label: "elm34011", file: "elm34011"}, {label: "elm34013", file: "elm34013"}]);
//private var zip:FZip;
//private var flag:Boolean;
private function init(event:Event):void
{
var file:ByteArray = new ByteArray();
file = "test.zip";
var encode:String = Base64.encodeByteArray(file);
Alert.show("Encoded file is " + encode);
for (var i:int = 0; i < zipdataProvdr.length; i++)
{
file = zipdataProvdr[i];
Alert.show("file is " + file);
}
//var encoded:String = Base64.encodeByteArray(file);
//Alert.show("encoded file is " + encoded.toString());
}
I used Nochump lib to unzip the files. Following is the code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" layout="absolute">
<mx:Script>
<![CDATA[
import flash.net.URLStream;
import flash.net.URLRequest;
import mx.controls.Alert;
import mx.collections.ArrayCollection;
import flash.events.Event;
import flash.events.ErrorEvent;
import flash.events.ProgressEvent;
import nochump.util.zip.ZipFile;
[Bindable] private var _files:Array = ["/MyMapFolder/elm34001.zip", "/MyMapFolder/elm34003.zip", "/MyMapFolder/elm34005.zip", "/MyMapFolder/elm34009.zip", "/MyMapFolder/elm34011.zip", "/MyMapFolder/elm34013.zip"];
private var zpfls:ZipFile;
[Bindable] private var arr:Array;
private var arrcoll:ArrayCollection;
private function loadZipFile():void {
currentState = "loading";
var urlStream:URLStream = new URLStream();
loadProgress.source = urlStream;
urlStream.addEventListener(Event.COMPLETE,completeHandler);
urlStream.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
//urlStream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
urlStream.load(new URLRequest(String(cbFiles.selectedItem)));
//Alert.show("Testing........" + urlStream.endian);
//Alert.show("Testing........" + urlStream.objectEncoding);
//Alert.show("Testing........" + urlStream);
}
private function completeHandler(event:Event):void
{
var data:URLStream = URLStream(event.target);
arr = new Array();
//Alert.show("***Length of file is " + data.bytesAvailable);
zpfls = new ZipFile(data);
arr = new Array(zpfls.entries[1]);
Alert.show("Show only Shape File: " + arr);
}
private function errorHandler(event:ErrorEvent):void {
currentState = "error";
errorLabel.text = event.text;
}
]]>
</mx:Script>
<mx:states>
<mx:State name="loading">
<mx:AddChild relativeTo="{this}" position="lastChild">
<mx:ProgressBar id="loadProgress" width="100%" />
</mx:AddChild>
</mx:State>
<mx:State name="error">
<mx:AddChild relativeTo="{this}" position="lastChild">
<mx:Label id="errorLabel" />
</mx:AddChild>
</mx:State>
</mx:states>
<mx:Label text="Select a zip file and click "Load"." x="67" y="10"/>
<mx:ComboBox id="cbFiles" dataProvider="{_files}" width="300" x="10" y="36"/>
<mx:Button label="Load" click="loadZipFile()" x="333" y="36"/>
<mx:ComboBox x="394" y="36" id="cbobxs" dataProvider="{arr}" width="169"> </mx:ComboBox>
</mx:Application>
I'm not sure you can. No need to reinvent the wheel though, try using a zip library like this one instead: http://nochump.com/blog/archives/15