What is wrong with my JPGEncoder - actionscript-3

Here is my code
if (event.target.content is Bitmap)
{
infotext.text = "got something";
var image:Bitmap = Bitmap(event.target.content);
var bitmapData:BitmapData = image.bitmapData;
this.addChild(image);
var j:JPGEncoder = new JPGEncoder(100);
var bytes:ByteArray = new ByteArray();
bytes=j.encode(bitmapData);
}
else
{
throw new Error("What the heck bob?");
}
When I run a debug session everything works fine till it reaches to the line
bytes=j.encode(bitmapData);
after that nothing happens and my program just goes into limbo Please help
I made changes to the code as per your suggestion
var myImage:Bitmap =Bitmap(e.target.content);
var bitmapData:BitmapData = new BitmapData(myImage.width,myImage.height,true,0xffffffff);
bitmapData.draw(myImage);
var encoder:JPGEncoder = new JPGEncoder();
var bytes:ByteArray = encoder.encode(bitmapData);
this.addChild(myImage);
but it gets stuck again after
var bytes:ByteArray = encoder.encode(bitmapData);
What am i doing wrong here?

Pretty sure I ran into this same issue a long time ago so I pulled up my code from then that I got to work.
After reviewing the code the only thing I see different is I construct the bitMapData first and assign the image via the load function.
So I think your issue is with the construction of the bitmapData var.
The following code block was cut down from a function I created that did a lot of other image manipulation.
So basically it is a cut down version and untested but it should work.
var myImage:Image = new Image();
myImage.load( Bitmap(event.target.content) );
var bitmapData:BitmapData = new BitmapData(myImage.width, myImage.height, true, 0xffffffff );
bitmapData.draw(myImage);
var encoder:JPEGEncoder = new JPEGEncoder();
var data:ByteArray = encoder.encode(bitmapData);
this.addChild(myImage);

Just in case anyone runs into this, Make sure you are using the bytearray JPEGEncoder class, it's faster: http://www.bytearray.org/?p=775
the as3core is JPGEncoder not JPEGEncoder. JPG vs JPEG... this got me also.

Related

Loading more than one child image AS3

I'm trying to load 2 images in to my flash file but getting an error - new to this AS3 malarkey any help would be appreciated!
Getting error: 5 1151: A conflict exists with definition request in namespace internal.
var myImage:String = dynamicContent.Profile[0].propImage.Url;
var myImage2:String = dynamicContent.Profile[0].prop2Image.Url;
var myImageLoader:StudioLoader = new StudioLoader();
var request:URLRequest = new URLRequest(enabler.getUrl(myImage));
myImageLoader.load(request);
myImageLoader.x =17;
myImageLoader.y = 0;
var myImageLoader2:StudioLoader = new StudioLoader();
var request:URLRequest = new URLRequest(enabler.getUrl(myImage2));
myImageLoader.load(request);
myImageLoader.x =17;
myImageLoader.y = 0;
if(this.currentFrame == 1) {
addChildAt(myImageLoader, 2);
}
if(this.currentFrame == 2) {
addChildAt(myImageLoader2, 2);
}
It's usually a good idea to move duplicate code into its own function instead of doing copy + paste:
function loadImage(file:String, x:Number, y:Number):StudioLoader{
var myImageLoader:StudioLoader = new StudioLoader();
var request:URLRequest = new URLRequest(file);
myImageLoader.load(request);
myImageLoader.x = x;
myImageLoader.y = y;
return myImageLoader;
}
addChild(loadImage(enabler.getUrl(myImage1),17,0));
addChild(loadImage(enabler.getUrl(myImage2),17,0));
That's not just giving you better structured code but also fixes your duplicate variable definition issue because what's defined locally inside a function stays inside the function.
This might provide some insight:
http://www.adobe.com/devnet/actionscript/learning/as3-fundamentals/functions.html
You can't create a new variable with the same name as an existing one, in your case I'm speaking about your URLRequest request, so to avoid this type of error you can do like this :
var myImageLoader2:StudioLoader = new StudioLoader();
// assing a new URLRequest to an existing var
request = new URLRequest(enabler.getUrl(myImage2));
// here you want use the myImageLoader2, not myImageLoader
myImageLoader2.load(request);
myImageLoader2.x =17;
myImageLoader2.y = 0;
Or :
var myImageLoader2:StudioLoader = new StudioLoader();
// create a new URLRequest var, so the name should be different
var request2:URLRequest = new URLRequest(enabler.getUrl(myImage2));
// here you want use the myImageLoader2, not myImageLoader
myImageLoader2.load(request2);
myImageLoader2.x =17;
myImageLoader2.y = 0;
Additionally, I notice in the second block that you declare myImageLoader2 but you then use the original myImageLoader to do the load request. So even if you do declare a new URLRequest, you wont get both images loaded.
Akmozo's solution has this corrected.

As3 loading blank image

I'm having problems loading an image from an externall source (this was working but we changed the server to a https and made some adjustments).
Now, when I loaded the image, there are no errors, but the width, height are all 0. Also the image is blank.
I tried loading with this script some random internet image and it worked fine. However with the original - (https://www.lovemarks.co/images/be50fe37eac192fb7c0d17353f5ef993a.jpg) - it doesn't work.
var somethingLoaded:Boolean = true;
var actualPic:*;
var pictLdrX:Loader = new Loader();
var pictURLX:String = 'https://www.lovemarks.co/images/be50fe37eac192fb7c0d17353f5ef993a.jpg';
var pictURLReqX:URLRequest = new URLRequest(pictURLX);
var loaderContext:LoaderContext = new LoaderContext();
pictLdrX.load(pictURLReqX);
pictLdrX.contentLoaderInfo.addEventListener(Event.INIT , loadedRemember)
function loadedRemember(event:Event){
var targetLoader:Loader = Loader(event.target.loader);
var newmc:MovieClip = new MovieClip();
addChild(newmc);
newmc.addChild(targetLoader.content);
newmc.x = -targetLoader.width/2;
newmc.y = -targetLoader.height/2;
trace ('w+'+targetLoader.width);
trace ('h+'+targetLoader.height);
trace ('x+'+newmc.x);
trace ('y+'+newmc.y);
}
The way would do it is to first listen for Event.COMPLETE instead of Event.INIT:
pictLdrX.contentLoaderInfo.addEventListener(Event.COMPLETE , loadedRemember);
Then, instead of referencing the actual loader in your handler, just reference the image directly by first casting the Event.target.content as a Bitmap:
function loadedRemember(event:Event)
{
trace("loadedRemember()");
var newmc:Bitmap = Bitmap(event.target.content);
addChild(newmc);
// Your math to center may need to be adjusted
// to account for such a big image size
newmc.x = -newmc.width/2;
newmc.y = -newmc.height/2;
trace ('w+'+newmc.width + '\nh+'+newmc.height + '\nx+'+newmc.x + '\ny+'+newmc.y);
}
Additionally check for any security errors related to cross domain within your output window.

as3 loop loading urls - continue loop on complete

I am trying to load a bunch of files in order. I want the other to start downloading once the previous has downloaded. I thought the best way to do this would be thru a for loop.
TheURL = is a bunch of urls in a ARRAY
for(var i:int=0;i<TheURL.length;i++)
{
var urlString:String = TheURL[i];
var urlReq:URLRequest = new URLRequest(urlString);
var urlStream:URLStream = new URLStream();
var fileData:ByteArray = new ByteArray();
urlStream.addEventListener(Event.COMPLETE, loaded);
urlStream.load(urlReq);
function loaded(event:Event):void
{
/// code to continue loop
}
}
It is important that the others do not start downloading until the previous has completed. Any suggestions on how to do that? Thanks
function downloadFiles():void
{
downloadNextFile();
}
function downloadNextFile():void
{
var urlString:String = TheURL.shift();
var urlReq:URLRequest = new URLRequest(urlString);
var urlStream:URLStream = new URLStream();
var fileData:ByteArray = new ByteArray();
urlStream.addEventListener(Event.COMPLETE, loaded);
urlStream.load(urlReq);
}
function loaded(event:Event):void
{
downloadNextFile();
}
Using a for-loop does not solve what you want to do.
In my opinion the easiest way should be to use the array of URLs as a queue. This can be done by using Array.shift(). But you should make a copy of your array if you need the original set of URLs when finished, because shift() makes an inline modification of the array.
The solution could be the following:
function loadQueue(urlQueue:Array):void
{
var url:String = urlQueue.shift();
var request:URLRequest = new URLRequest(url);
var stream:URLStream = new URLStream();
var data:ByteArray = new ByteArray();
var completeHandler:Function = function(event:Event)
{
// remove listener from stream to be a clean coder ;)
stream.removeEventListener(Event.COMPLETE, completeHandler);
// handle completion in the way you need ...
// continue with the next element
if (urlQueue.length > 0)
loadQueue(urls);
}
urlStream.addEventListener(Event.COMPLETE, loaded);
urlStream.load(urlReq);
}
Loading your queue will then look like:
loadQueue(TheURL.concat()); // concat() will clone your array
You should use a library like greensock LoaderMax http://www.greensock.com/loadermax/.
It's much more difficult than it seems to create your own loading queue. There are plenty of bugs, traps and special cases to deal with. It seems to work fine, and one day a customer call you back because of a bug on this particular flash version on this computer behind this firewall... I've started to do mine library, but finally I used LoadManager.

JPEGencode issue in as3

Here is my code. What I m trying to achieve is to be able to capture an image from the the camera and upload it onto a media server but so far I have not been able to encode it succesfully .Can someone please point me in the right direction.
Here is the code
var imagePromise:MediaPromise = event.data;
imageLoader = new Loader();
imageLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, asyncImageLoaded );
imageLoader.addEventListener( IOErrorEvent.IO_ERROR, cameraError );
imageLoader.loadFilePromise( imagePromise );
function asyncImageLoaded(event:Event):void
{
var destination:String = "upload.php";
var now:Date = new Date();
var fileName = "IMG" + now.fullYear + now.month + ".jpg";
var image:Bitmap = Bitmap(imageLoader.content);
var bitmapData:BitmapData = image.bitmapData;
var j = new JPGEncoder(80);
var bytes:ByteArray = j.encode(bitmapData);
}
This is the error I get when i try to encode the image
TypeError: Error #1009: Cannot access a property or method of a null object reference.
which line? 1009 means you're trying to access a variable of something that is NULL. I'm guessing in this case, it's probably the line:
var image:Bitmap = Bitmap(imageLoader.content);
Try adding this:
if (imageLoader.content is Bitmap) {
var image:Bitmap = Bitmap(imageLoader.content);
} else {
throw new Error("What the heck bob?");
}
If it's an error, I bet the content was not decoded properly (which could mean a mime-type that wasn't image/jpg)
Additionally, you could probably use the native jpeg encoder for speed: (flash 11 i believe?)
var byteArray:ByteArray = new ByteArray();
bitmapData.encode(new Rectangle(0,0,640,480), new flash.display.JPEGEncoderOptions(), byteArray);
http://help.adobe.com/en_US/as3/dev/WS4768145595f94108-17913eb4136eaab51c7-8000.html

Actionscript 3 does not work .. it didn't give errors

I am creating a clock using this code:
var date:Date = new Date();
var time:Timer = new Timer(1000);
time.addEventListener(TimerEvent.TIMER, actualiser);
time.start();
function actualiser(e:TimerEvent){
date = new Date();
var s:uint = date.seconds;
var m:uint = date.minutes;
var h:uint = date.hours;
sec_mc.rotation =(s * 6);
min_mc.rotation =(m * 6);
heur_mc.rotation =(h * 30) +m/2;
}
But, it seems that the code is not executing, I can’t even trace anything written in constructor of my document class. When I run it nothing happens and when I try to debug I get the alert message saying:
You cannot debug this SWF because it does not contain ActionScript
What can be wrong?
Try to check the code in a new fla. Write the following in your 1st frame action panel. This does give proper output and you can see the traces in the output panel. There is no issue with the code logic. Might be some issue with the movieclip being used.
var date:Date = new Date();
var time:Timer = new Timer(1000);
time.addEventListener(TimerEvent.TIMER, actualiser);
time.start();
function actualiser(e:TimerEvent){
date = new Date();
var s:uint = date.seconds;
var m:uint = date.minutes;
var h:uint = date.hours;
trace(h+":"+m+":"+s);
}
In case of CS5 , try the following steps :
WINDOWS:
1. Quit Flash
2. In a text editor, open the jvm.ini file from the following location:
2.1. Windows XP: System Hard Disk\Documents and Settings\user\Local Settings\Application Data\Adobe\Flash CS5\language\Configuration\ActionScript 3.0\jvm.ini
2.2. *Windows Vista or Windows7:* System Hard Disk\Users\user\AppData\Local\Adobe\Flash CS5\language\Configuration\ActionScript 3.0\jvm.ini
(You might need to turn on “show hidden files”)
3. Change -Xmx128m to -Xmx256m and save the file.
EDIT: Uhm. Is that all you have? Because you need to import the needed packages, etc. Also, are you sure you linked everything up?