bytearray to bitmap scenario - actionscript-3

I just have a ByteArray, not the bitmap. I am using the below code to convert to Bitmap, but I end up getting end of file error.
//rstream is the ByteArray I have
var bytes:ByteArray = rstream;
var rect:Rectangle = new Rectangle(0,0,myPuzzle.width - 20,myPuzzle.height - 20);
var newBmd:BitmapData = new BitmapData(rect.width,rect.height,true,0xFFFFFFFF);
bytes.position = 0;
newBmd.setPixels(rect, bytes);
var image:Bitmap = new Bitmap(newBmd);

I got it working by using a Loader.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loading1);
loader.loadBytes(rstream);
function loading1(e:Event):void
{
var image:Bitmap = Bitmap(e.target.loader.content);
myPuzzle.source = image;
}

Related

Load external swf into bytearray with adobe flash

How can I load external swf into bytearray with adobe flash AS3..
Here is my code..
var my_url1:URLRequest = new URLRequest("SWF/Lesson1.swf");
my_url1.method = URLRequestMethod.GET;
my_url1.contentType = "application/x-shockwave-flash";
var urlloader:URLLoader = new URLLoader(my_url1);
var myByteArray:ByteArray = new ByteArray();
urlloader.data = myByteArray;
It not works well. But it isn't give any error. How can I fix this problem?
You should listen for COMPLETE event and set the data format to BINARY:
var my_url1:URLRequest = new URLRequest("SWF/Lesson1.swf");
var urlloader:URLLoader = new URLLoader(my_url1);
urlloader.dataFormat = URLLoaderDataFormat.BINARY;
urlloader.addEventListener(Event.COMPLETE, function(event:Event):void
{
var myByteArray:ByteArray = URLLoader(event.target).data as ByteArray;
trace(myByteArray.length);
});

AS3/AIR: IOStream Error when trying to upload files

I'm making an AIR app in which the user drags and drops image files onto the stage, the app then uploads those dropped images to the site imgur.com, and then returns with links to the images (in XML format).
I am having two issues:
the program works successfully with a very small file, however anything larger (1mb or more), I get an IOStream error 2023. Imgur supports uploads of up to 10mb so I know it must be something on my end.
I would like to be able to drop multiple image files at once into the program, and have it upload all of them (either synchronously or asynchronously, it does not matter). Right now the program gives errors when I attempt to do this (something along the lines of the byteArray having to be greater than 0).
Below is the code in question which is giving me issues. (thank you for your help in advanced)
private function doDragDrop(e:NativeDragEvent):void
{
trace("doDragDrop() called.");
var dropFiles:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
for each (var file:File in dropFiles)
{
switch (file.extension.toLowerCase())
{
case "jpg" :
case "jpeg" :
case "gif" :
case "png" :
case "apng" :
case "tiff" :
case "bmp" :
case "pdf" :
case "xcf" :
trace("file Extension Check = passed");
addImage(file.nativePath);
break;
//error handling for non-supported filetypes
default :
trace("ERROR: Unsupported File Type Detected!");
}
}
}
private function addImage(nativePath:String):void
{
trace("addImage() called.");
trace("NativePath is: " + nativePath);
//var file:File = new File(nativePath);
var file:File = File.desktopDirectory.resolvePath(nativePath);
var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
loader.loadBytes(ba);
}
private function fileLoaded(e:Event):void
{
trace("fileLoaded() called.");
var bitmap:Bitmap = Bitmap(e.target.content);
var bitmapData:BitmapData = bitmap.bitmapData;
var png:ByteArray = PNGEncoder.encode(bitmapData);
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.addEventListener(Event.COMPLETE, onCookieSent);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
var vars:String = "?key=" + API_KEY + "&name=name&title=title";
var request:URLRequest = new URLRequest(UPLOAD_URL + vars);
request.contentType = "application/octet-stream";
request.method = URLRequestMethod.POST;
request.data = png;
urlLoader.load(request);
}
private function onCookieSent(e:Event):void
{
trace("onCookieSent() called.");
var res:XML = new XML(unescape(urlLoader.data));
var resultsList:XMLList = res.links;
trace(resultsList);
}
private function onIOError(e:IOErrorEvent):void
{
trace("ioErrorHandler: " + e);
TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
// Handle error
}
private function onSecurityError(e:SecurityErrorEvent):void
{
trace("securityErrorHandler: " + e);
TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
// handle error
}
//When the dragged object leaves the drop point, do this
private function doDragExit(e:NativeDragEvent):void
{
trace("doDragExit() called.");
TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
}
after doing this:
var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();
you already have a ByteArray of the file/image. can't you just send this one...
private function addImage(nativePath:String):void
{
trace("addImage() called.");
trace("NativePath is: " + nativePath);
//var file:File = new File(nativePath);
var file:File = File.desktopDirectory.resolvePath(nativePath);
var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.addEventListener(Event.COMPLETE, onCookieSent);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
var vars:String = "?key=" + API_KEY + "&name=name&title=title";
var request:URLRequest = new URLRequest(UPLOAD_URL + vars);
request.contentType = "application/octet-stream";
request.method = URLRequestMethod.POST;
request.data = ba;
urlLoader.load(request);
}

Actionscript 3: Soundspectrum animation playing but sound muted. Is this possible?

Kinda of an oxy-moron question but I thought I would go ahead and ask anyhow.... I am looking for my soundspectrum animation to play but my sound will be muted...This will be inserted in a banner ad, so their are specific rsestrictrictions with vendors where the sound has to be user initiated...
Any ideas would be appreciated...
var SpectrumLineWidth:Number;
var SpectrumLineColor:Number;
var SpectrumGlowDirection:String;
var SGD:String;
var ss:ByteArray = new ByteArray();
var bmpData:BitmapData;
var bmp:Bitmap;
var lsp:Sprite = new Sprite();
var blur:BlurFilter;
// Main sounds object
var _sound:Sound = new Sound();
var _soundChannel:SoundChannel;
var _soundTransform:SoundTransform=new SoundTransform(1);
var _soundLoaderContext:SoundLoaderContext = new SoundLoaderContext();
bmpData = new BitmapData(10,10,true,0xcccccc);
bmp = new Bitmap(bmpData);
spectrumArea.specArea.addChild(lsp);
spectrumArea.specArea.addChild(bmp);
// filter;
blur = new BlurFilter(8,8,4);
// save configuration
SpectrumLineWidth = 1;
SpectrumLineColor = 0x0066FF;
var s:Sound = new Sound();
//s.setVolume(0);
var sc:SoundChannel;
var ba:ByteArray = new ByteArray();
var array:Array;
s.load(new URLRequest("test.mp3"));
sc = s.play(0,1000);
sc.soundTransform = _soundTransform;
this.addEventListener(Event.ENTER_FRAME, spectrum);
function spectrum(event:Event){
lsp.graphics.clear();
lsp.graphics.lineStyle(SpectrumLineWidth, SpectrumLineColor);
lsp.graphics.moveTo(-1, 50);
SoundMixer.computeSpectrum(ss);
for (var i:uint = 0; i<350; i++)
{
var num:Number = - ss.readFloat() * 50 + 50;
lsp.graphics.lineTo(i, num);
}
bmpData.draw(lsp);
bmpData.applyFilter(bmpData, bmpData.rect,new Point(), blur);
//SGD = SpectrumGlowDirection;
//lsp.graphics.clear();
//lsp.alpha=.5
bmpData.draw(lsp);
}
//var sd:SoundTransform = new SoundTransform();
//sd.volume=0;
//SoundMixer.soundTransform = new SoundTransform(1);
You cant do it with SoundMixer.computeSpectrum(ss);
Look into Sound.extract

AS3 - Remove Child Problem

I'm using the following code to display an advertisement in a mobile game that I'm working on. It works fine but I don't know what to put in the clickStart function to remove the advertisement from the stage before the game plays. I've been playing around with removeChild but can't seem to get it to work.
stop();
startButton.addEventListener(MouseEvent.CLICK,clickStart);
function clickStart(event:MouseEvent) {
gotoAndStop("play");
}
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp");
var variables:URLVariables = new URLVariables();
variables.adspace = "0";
variables.pub = "0";
variables.devip = "127.0.0.1";
variables.format = "IMG";
variables.adcount = "1";
variables.response = "XML";
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(request);
function onComplete(e:Event):void
{
var data:XML = new XML(loader.data as String);
var status:String = data.*::status.toString();
if(status == "success")
{
var ad:XMLList = data.*::ads.*::ad;
var link:String = ad.*::link.toString();
var l:Loader = new Loader();
l.load(new URLRequest(link));
addChild(l);
l.x = 135;
l.y = 265;
var clickurl:String = ad.*::action.#target.toString();
l.addEventListener(MouseEvent.CLICK, onAdClick);
function onAdClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(clickurl);
navigateToURL(request);
}
}
}
You need to move the declaration of the Loader outside of the onComplete function, since the variable runs out of scope.
var l:Loader = new Loader();
You could try placing it on the same scope as these:
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp");
var variables:URLVariables = new URLVariables();
then you should be able to update your clickStart function to something like this:
function clickStart(event:MouseEvent) {
removeChild(l);
gotoAndStop("play");
}

How to use the method setPixels(rectangle, byteArray) of bitmapData in Adobe Flex

Error #2030: End of file was encountered.
This method is not working
I am using it like this
var ba:ByteArray = new ByteArray();
ba = bmd.getPixels(bmd.rect);
bmd.setPixels(srcRect, ba);
bm.bitmapData = bmd;
img.source = bm;
You need to reset the possition of your Byte Array (ba.position = 0) before re-using it
var ba:ByteArray = new ByteArray();
ba = bmd.getPixels(bmd.rect);
ba.position = 0;
bmd.setPixels(srcRect, ba);
bm.bitmapData = bmd;
img.source = bm;