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
Related
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.
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.
i use LocalConnection between two swf in the same page.
What i'm trying to do is to use a String sent from one swf to the other, as variable into my URLRequest...
So i can trace "myVar" into the function chemin, but i didn't find how to use it into URLRequest
thank's for your help
swf that receive the var :
var lc:LocalConnection=new LocalConnection();
lc.client=this;
lc.connect("callBig");
function chemin(myVar:String){
trace(myVar)
}
var chargementXML:URLLoader = new URLLoader();
var fichier:URLRequest = new URLRequest(myVar);
Some references to help you out.
URLRequest.data: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequest.html?filter_flex=4.1&filter_flashplayer=10.2&filter_air=2.6#data
URLVariables: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLVariables.html
So, if you want to pass some variables in your request, you do the following:
Create new URLVariables() object
Assign it's reference to URLRequest.data field.
var fichier:URLRequest = new URLRequest();
var urlVars:URLVariables = new URLVariables();
urlVars["myVarName"] = myVar;
fichier.data = urlVars;
This way your server side app will recieve a variable myVarName with value of myVar.
var lc:LocalConnection=new LocalConnection();
lc.client=this;
lc.connect("callBig");
var receivedVar:String = "";
// somewhere else
function chemin(myVar:String){
receivedVar = myVar;
}
// later
var chargementXML:URLLoader = new URLLoader();
var vars:URLVariables = new URLVariables();
vars.myVar = receivedVar;
chargementXML.data = vars;
var fichier:URLRequest = new URLRequest(myVar);
I am trying to save a Sprite object as a file on the device I'm working on and it seems to work. the problem I'm having is reading the saved file back and placing it back on stage as a sprite. Below is the code I have so far, could someone tell me what it is I'm doing wrong? I have a suspicion that the saved isn't what I expect it to be since the file sizes have been under a kilobyte.
public function save_album(e:Event):void
{
var outFile:File = File.documentsDirectory; // dest folder is desktop
outFile = outFile.resolvePath("canvas3.bin");
var fs:FileStream = new FileStream();
var bytes:ByteArray = new ByteArray();
//trace (File.documentsDirectory.url + "/canvas2.bin");
fs.open(outFile, FileMode.WRITE);
bytes.writeObject(graffitiContainer) //graffitiContainer is a Sprite
bytes.position = 0;
fs.writeBytes(bytes, 0, bytes.length);
fs.close();
}
public function open_album(e:Event):void
{
var inBytes:ByteArray = new ByteArray();
var inFile:File = File.documentsDirectory;
inFile = inFile.resolvePath("canvas3.bin"); // name of file to read
var inStream:FileStream = new FileStream();
inStream.open(inFile, FileMode.READ);
inStream.readBytes(inBytes, 0, inBytes.length);
inStream.close();
inBytes.position = 0;
ui.removeChild(graffitiContainer);
var obj:Sprite = inBytes.readObject() as Sprite; //returns a null
graffitiContainer = obj;
ui = new UIComponent();
graffitiContainer.x = 0;
graffitiContainer.y = 100;
ui.addChild(graffitiContainer);
}
Not fully sure I understand what you're trying to accomplish; however, this implementation doesn't do what you're thinking - writeObject could only serialize general public properties, and not the graphics member.
You could render it to a Bitmap.
Saw a blog post about this:
http://jacwright.com/201/serializing-display-objects/
I am loading a set of thumbnail images from an array [hard coded] into a movieclip symbol on the stage. I have two arrays with the thumbnail and the full size image having the same index number. In many examples, "event.currentTarget.contentLoaderInfo.url" returns the full path to the image selected. i just want the index number.
Adobe does not make is easy to figure out what other properties are available to me from the contentLoaderInfo. Is 'SelectedIndex' or something like that available?
Where does an inspiring AS programmer find the contentLoaderInfo properties and or methods available? Is url the only thing that us usable here?
Is there a better approach?
Thanks in advance.
Edit:
var thumbnails:Array = ["tn_2010OpenHouse_00.jpg","tn_2010OpenHouse_01.jpg"];
var images:Array = ["2010OpenHouse_00.jpg","2010OpenHouse_01.jpg"];
var thumbX:Number = 10;
var thumbY:Number = 623;
var loader:Loader = new Loader();
loader.load(new URLRequest("images/" + images[0]));
addChild(loader);
loadThumbs();
function loadThumbs():void
{
var thumbLoader:Loader;
var container:Sprite = new Sprite();
container.width = 100;
addChild(container);
container.buttonMode = true;
for (var i:uint = 0; i < thumbnails.length; i++)
{
thumbLoader = new Loader();
thumbLoader.load(new URLRequest("images/" + thumbnails[i]));
thumbLoader.x = thumbX;
thumbLoader.y = thumbY;
thumbX += 100;
container.addChild(thumbLoader);
thumbLoader.addEventListener(MouseEvent.CLICK, thumbClicked);
container.width += 100;
addChild(thumbLoader);
}
stop();
}
function thumbClicked(ev:MouseEvent):void
{
//weltraumpirat's example
var index:int = thumbnails.indexOf ( ev.target.contentLoaderInfo.url );
trace("Index= "+ index);
//trying a different approach as well
index = thumbnails.indexOf ( ev.currentTarget.contentLoaderInfo.url );
trace("Index= "+ index);
loader.load(new URLRequest("images/" + images[index]));
}
Output:
Index= -1
Index= -1
Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.
The contentLoaderInfo property returns a LoaderInfo class. You can view its properties here:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/LoaderInfo.html
You can use array.indexOf() to return an object's index. Since I don't know the rest of your code, here's an approximate example:
function thumbClicked (ev:MouseEvent) : void {
var index:int = thumbnails.indexOf ( ev.target.contentLoaderInfo.url );
loader.load ( new URLRequest (fullSizeImages[index]) ) ;
}
Edit:
Since I didn't know the exact code you were using, I assumed you store the entire path to the picture in your array. In your code, you prepend "images/", so the code should be:
function thumbClicked (ev:MouseEvent) : void {
var index:int = thumbnails.indexOf ( ev.target.contentLoaderInfo.url.substring (7));
// 7 is the length of "images/", so substring returns only the filename part.
loader.load ( new URLRequest (fullSizeImages[index]) ) ;
}