Flash AS3 copyPixels from BitmapData into jpeg screenshot - actionscript-3

I have code to create a live screenshot of the stage in my swf.
After that it saves as a jpeg. All good.
Only, I don't need the whole stage, I only need a cutout:
x,y: 357,341
widt,height: 319,483
My code looks like this..
Where and how do I insert the copypixels function?
(I'm a Flash novice, so go easy on me :-)
function mouseReleaseSave(event:MouseEvent):void {
import com.adobe.images.JPGEncoder;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequestHeader;
import flash.net.URLRequest;
var jpgSource:BitmapData = new BitmapData(this.stage.stageWidth, this.stage.stageHeight);
jpgSource.draw(this.stage);
var jpgEncoder:JPGEncoder = new JPGEncoder(70);
var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
//set the request's header,method and data
var header:URLRequestHeader = new URLRequestHeader("Content-type","application/octet-stream");
var loader:URLLoader = new URLLoader();
//sends jpg bytes to saveJPG.php script
var myRequest:URLRequest = new URLRequest("saveJPG.php");
myRequest.requestHeaders.push(header);
myRequest.method = URLRequestMethod.POST;
myRequest.data = jpgStream;
loader.load(myRequest);
//fire complete event;
loader.addEventListener(Event.COMPLETE,saved);
function saved(e:Event){
//trace the image file name
trace(loader.data);
}
}

This should help:
var subArea:Rectangle = new Rectangle(0,0, 319,483 );
var newBmp:Bitmap = new BitmapData( 319,483 );
var cutoutBmp:Bitmap = new Bitmap( newBmp, PixelSnapping.ALWAYS, true );
cutoutBmp.bitmapData.draw( jpgSource, new Matrix(1, 0, 0, 1, -357, -341) , null, null, subArea, true );

Related

Smooth image on load in as3

I am new here and a complete noob when it comes to as3. Somehow I have managed to put this together with some help from different places. And now I turn to you guys :)
I need to put smoothing on my images and thumbs that Im loading from an XML file. I have tried a lot of things but can't get any of it to work and I get this error:
Scene 1, Layer 'as3', Frame 1, Line 27 1120: Access of undefined property e. -> So I know var bitmapContent:Bitmap = Bitmap( e.target.content ); is the problem. but I have no idea what to use instead of e. I
this i what I have so far:
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.easing.None;
import flash.display.Bitmap;
// Loads the first image//
var i =new Loader();
i.load(new URLRequest("images/1.jpg"));
mainLoader.addChild(i)
//Loads the XML file//
var picsXML:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE , xmlLoaded);
xmlLoader.load(new URLRequest("imagelist.xml"));
//Loads images into thumbs//
function xmlLoaded(event:Event):void{
picsXML = new XML(xmlLoader.data);
//trace(picsXML);
var bitmapContent:Bitmap = Bitmap( e.target.content );
bitmapContent.smoothing = true;
var thumbLoader:UILoader;
for (var i:uint=0; i<picsXML.image.length(); i++)
{
thumbLoader=UILoader(getChildByName("thumb"+i));
thumbLoader.load(new URLRequest("thumbs/"+picsXML.image[i].#file));
thumbLoader.buttonMode = true;
thumbLoader.addEventListener(MouseEvent.CLICK, thumbClicked);
thumbLoader.addEventListener(MouseEvent.CLICK, tester);
}
}
//Loads large image when thumb is clicked//
function thumbClicked(event:MouseEvent){
//var bitmapImage:Bitmap = event.target.content;
//bitmapImage.smoothing = true;
var thumbName:String = event.currentTarget.name;
var thumbIndex:uint = uint(thumbName.substr(5));
var fullPath:String = "images/"+picsXML.image[thumbIndex].#file;
mainLoader.load(new URLRequest(fullPath));
var myTween:Tween = new Tween(mainLoader,"alpha", None.easeNone, .3,1,18,false);
}
//Removes the first image when thumbs is clicked//
function tester(event:MouseEvent){
if (mainLoader.contains(i)) {
trace("hej")
mainLoader.removeChild(i);
}
}
If i get your code true problem is here: u import an external xml file which is probably contain your url's and name of pic's. U can't turn ur xml file into a bitmap.
If u want smoothing on your pictures u need to it after load ur pics or thumb's.
Probably ur problem will fix like this :) Hope this will help
Functions with arguments work like this: function Name ( input arg Name : input arg Type )
Firstly, your function xmlLoaded(event:Event) cannot have an input called event
but then you try to do a Bitmap( e.target.content ); so for that bitmap line to work you'd have to change your input name to e like so... xmlLoaded( e : Event );
Secondly, var bitmapContent:Bitmap = Bitmap( e.target.content ); is incorrect.
This is more correct var bitmapContent:Bitmap = img_Bytes_Loader.content as Bitmap; set a Loader's content as Bitmap not XML content (text) as Bitmap (pixels).
Anyways assuming your XML file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<mp3>
<url> track1.mp3 </url>
<image> image1.jpg </image>
</mp3>
<mp3>
<url> track2.mp3 </url>
<image> image2.jpg </image>
</mp3>
</Items>
Then your code should look something like this below :
var imgLoader_XML : URLRequest;
var picLoader : Loader = new Loader();
function xmlLoaded(event:Event):void
{
picsXML = new XML(xmlLoader.data);
//trace(picsXML);
imgLoader_XML = new URLRequest( picsXML.mp3[ index ].image ); //[index] could be replaced with [i] if FOR loop
picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, picloadComplete);
picLoader.load (imgLoader_2);
}
public function picloadComplete(evt:Event)
{
yourContainer.addChild(picLoader);
yourContainer.width = 100;
yourContainer.height = 100;
yourContainer.alpha = 1;
}
Thanks for the inputs guys. It is greatly appreciated.
I figured it out. You were right, of course I can't put smoothing on the UILoader, so I had to target the content of the UILoader and run thumbLoader.addEventListener(Event.COMPLETE, smoothing); each time an image is importet.
Here is the entire working code (maybe it can help someone else :) :
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.easing.None;
import flash.display.Bitmap;
import flash.display.Loader;
// Loads the first image//
var i =new Loader();
i.load(new URLRequest("images/1.jpg"));
mainLoader.addChild(i)
//Loads the XML file//
var picsXML:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE , xmlLoaded);
xmlLoader.load(new URLRequest("imagelist.xml"));
//Loads images into thumbs//
function xmlLoaded(event:Event):void{
picsXML = new XML(xmlLoader.data);
var thumbLoader :UILoader = new UILoader();
for (var i:uint=0; i<picsXML.image.length(); i++)
{
thumbLoader=UILoader(getChildByName("thumb"+i));
thumbLoader.load(new URLRequest("thumbs/"+picsXML.image[i].#file));
thumbLoader.buttonMode = true;
thumbLoader.addEventListener(MouseEvent.CLICK, thumbClicked);
thumbLoader.addEventListener(MouseEvent.CLICK, tester);
thumbLoader.addEventListener(Event.COMPLETE, smoothing);
}
}
function smoothing(e:Event):void{
if(e.currentTarget.content is Bitmap)
{
Bitmap(e.currentTarget.content).smoothing = true;
}
}
//Loads large image when thumb is clicked//
function thumbClicked(event:MouseEvent){
var thumbName:String = event.currentTarget.name;
var thumbIndex:uint = uint(thumbName.substr(5));
var fullPath:String = "images/"+picsXML.image[thumbIndex].#file;
mainLoader.load(new URLRequest(fullPath));
mainLoader.addEventListener(Event.COMPLETE, smoothing);
var myTween:Tween = new Tween(mainLoader,"alpha", None.easeNone, .3,1,18,false);
}
//Removes the first image when thumbs is clicked//
function tester(event:MouseEvent){
if (mainLoader.contains(i)) {
trace("hej")
mainLoader.removeChild(i);
}
}

Get a ByteArray from BitmapData in AS3

I would like to get a ByteArray from a BitmapData in AS3. I tried the following:
var ba:ByteArray = myBitmapData.getPixels(0,0);
It didn't work and I got this error ArgumentError: Error #1063: Argument count mismatch on flash.display::BitmapData/getPixels(). Expected 1, got 2.:
As Adobe said about BitmapData.getPixels : "Generates a byte array from a rectangular region of pixel data...", so your parameter should be a Rectangle object.
Take this example from Adobe.com :
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.utils.ByteArray;
var bmd:BitmapData = new BitmapData(80, 40, true);
var seed:int = int(Math.random() * int.MAX_VALUE);
bmd.noise(seed);
var bounds:Rectangle = new Rectangle(0, 0, bmd.width, bmd.height);
var pixels:ByteArray = bmd.getPixels(bounds);
As was mentioned in the other answer, getpixels expects a .rect meaning rectangular area of some displayObject (like Sprite, MovieClip, Shape or other already exisiting BitmapData.
getPixels(). Expected 1, got 2. is becuase it should have been:
var ba:ByteArray = myBitmapData.getPixels( myBitmapData.rect);
Study this code and see if it helps you :
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.BitmapData;
var ba:ByteArray = new ByteArray;
var picBMP:Bitmap; var picBMD:BitmapData;
var pic_canvas:Sprite = new Sprite(); //container for image
var loader:Loader = new Loader(); //image loader
loader.load(new URLRequest("pic1.jpg")); //load some JPG/PNG/GIF/SWF
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, img_load_Complete);
function img_load_Complete(evt:Event):void
{
picBMP = loader.content as Bitmap;
pic_canvas.addChild(picBMP);
stage.addChild(pic_canvas); //container now added to stage (screen)
BMP_toBytes(); //do function to convert to bytes
}
function BMP_toBytes():void
{
var PC = pic_canvas; //shortcut reference to pic_canvas (less typing)
picBMD = new BitmapData(PC.width, PC.height, true, 0xFFFFFF);
picBMD.draw(PC); //make bitmapdata snapshot of container
ba = picBMD.getPixels(picBMD.rect);
trace("BA length : " + ba.length); //check how many bytes in there
ba.position = 0; //reset position to avoid "End of File error"
bytes_toPixels(); //do function for bytes as pixels to some new container
}
function bytes_toPixels():void
{
var new_BMP:Bitmap = new Bitmap; var new_BMD:BitmapData;
var new_canvas:Sprite = new Sprite(); //another new container for pixels
ba.position = 0; //reset position to avoid "End of File error"
//we can reuse picBMD W/H sizes since we have copy of same pixels
new_BMD = new BitmapData(picBMD.width, picBMD.height, true, 0xFFFFFFFF);
new_BMD.setPixels(new_BMD.rect, ba); //paste from BA bytes
new_BMP.bitmapData = new_BMD; //update Bitmap to hold new data
new_canvas.x = 150; new_canvas.y = 0; new_canvas.addChild(new_BMP);
stage.addChild(new_canvas); //add to screen
}
Hope it helps..

action script 3.0 import fla with xml

i want import fla movie from xml. i can't make it. Thank for help.
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.engine.TabAlignment;
import flash.net.URLRequest;
import flash.media.Sound;
//i create timer for sound.
var tmr:Timer=new Timer(1000,1);
tmr.addEventListener(TimerEvent.TIMER, sesiBaslat);
function sesiBaslat(evt:TimerEvent){
var yol:URLRequest=new URLRequest("../../../sound/aaa/1/1/1.mp3");
var ses:Sound=new Sound();
ses.load(yol);
ses.play();
}
tmr.start();
//i was use this code in old times
/*var vid:Video = new Video(1600, 910);
flvPlaceHolder.addChild(vid);
addChild(flvPlaceHolder);
flvPlaceHolder.x = stage.stageWidth/2-vid.width/2;
flvPlaceHolder.y = stage.stageHeight/2-vid.height/2;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
vid.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData = function(evt:Object):void {};
ns.client = listener;
ns.play("fla/683-bak.flv");
*/
//now i want to write this format now,because i want to make it dynamic
var veriler:XML;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, XMLokundu);
loader.load(new URLRequest("../../../xml/aaa/1/1/1.xml"));
function XMLokundu(e:Event):void{
veriler= new XML(e.target.data);
var loader1:Loader = new Loader();
loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
loader1.load(new URLRequest(veriler.animasyonlar.animasyon[1].yolu));
//This blog was made a import sound,image etc. but doesnt work from fla
function imageLoaded(e:Event):void {
var flvPlaceHolder:MovieClip = new MovieClip();
var vid:Video = new Video();
e.target.content.smoothing = true;
addChild(loader1);
loader1.x = veriler.animasyonlar.animasyon[1].xkor;
loader1.y = veriler.animasyonlar.animasyon[1].ykor;
loader1.width = veriler.animasyonlar.animasyon[1].width;
loader1.height = veriler.animasyonlar.animasyon[1].height;
}
}
/*
//XML FİLE
//This is my xml file
<animasyonlar>
<animasyon>
<first>sound/16/1153.mp3</first>
<ikincises>sound/16/1154.mp3</ikincises>
<ucuncuses>sound/16/1151.mp3</ucuncuses>
<dorduncuses>sound/16/1152.mp3</dorduncuses>
<yolu>../../../../bbb/7/fla/683-look.flv</yolu> 1.fla way
<xkor>800</xkor> 2.x value
<ykor>100</ykor> 3.y value
<width>50%</width>
<height>50%</height>
</animasyon>
</animsyonlar>
Your XML is not formatted properly because your closing tag is missing the 2nd "a".
<animasyonlar>
...
</animsyonlar>

AIR BitmapData.draw slow

I am working with large bitmapdata that I need to resize, doing some test I realized that the same code runs tooooo slow in AIR.
This is the code:
import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.display.Bitmap;
import flash.geom.Point;
var MAX_BITMAP_SIZE:Point = new Point( 596, 842 );
var bitmapdata:BitmapData = new MyBitmap();
var sc:Number = Math.min( MAX_BITMAP_SIZE.x / bitmapdata.width, MAX_BITMAP_SIZE.y / bitmapdata.height );
var outBitmap:BitmapData = new BitmapData( bitmapdata.width >> 1, bitmapdata.height >> 1, false, 0x0 );
var bitmap:Bitmap = new Bitmap();
addChild( bitmap );
var scaleMatrix:Matrix = new Matrix();
scaleMatrix.scale(sc,sc);
var time:Number = new Date().getTime();
outBitmap.draw( bitmapdata, scaleMatrix, null,null,null,true );
trace( new Date().getTime()-time );
bitmap.bitmapData = outBitmap;
The same code tests:
Publish as flash 11.4 -> 1ms
Publish as AIR 3.4 ->62ms
Anyone?

As3 Convert ByteArray to Bitmap

Noob to As3 Bitmap stuff...
when i try to doing the following code it fails
bmd.setPixels(bmd.rect, decodeValue);
and the error message is:
Error: Error #2030: End of file was encountered.
The situation is i have store the image as binary into the database by convert the byteArray and now i would like to retrieve it and convert back to image.
Just to clear this up ByteArray Need to Place into Bitmap and then you can add to the movie Clip right?
import flash.display.Loader;
import flash.net.URLRequest;
import flash.display.Bitmap;
import flash.display.BitmapData;
import com.dynamicflash.util.Base64;
var loader:Loader;
var req:URLRequest;
var orig_mc:MovieClip;
var copy_mc:MovieClip;
function loaderCompleteHandler(evt:Event) {
//swap data
var ldr:Loader = evt.currentTarget.loader as Loader;
var origImg:Bitmap = (ldr.content as Bitmap);
var origBmd:BitmapData = origImg.bitmapData;
trace(origImg.bitmapData);
trace(origImg.width);
trace(origImg.height);
//Convert image byteData into Base64 String
var byteArray:ByteArray = new ByteArray();
byteArray.writeObject(origBmd);
var encoded:String = Base64.encodeByteArray(byteArray);
trace("\nENCODED:\n" + encoded);
var decoded:ByteArray = Base64.decodeToByteArray(encoded);
trace("\nDECODED:\n" + decoded.toString());
// convert base64 string back into movieclip
var newBmd:BitmapData = new BitmapData(origImg.width,origImg.height,"true",0xFFFFFFFF);
newBmd.setPixels(origBmd.rect, decoded);**// THIS THROWS AN " Error #2030: End of file was encountered."**
var image:Bitmap = new Bitmap(newBmd, "auto", true);
copy_mc.addChild(image);
copy_mc.x = origImg.width;
}
loader = new Loader();
req = new URLRequest("C:\\Data\\cutcord.jpg");
loader.load(req);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
// movieclip to display original image
orig_mc = new MovieClip();
orig_mc.addChild(loader);
addChild(orig_mc);
// movieclip to display image copy
copy_mc = new MovieClip();
addChild(copy_mc);
Anyone can help would be very appreciated :)
try this code :
var rect:Rectangle = new Rectangle(0,0,img.widht,img.height);
var byteArray:ByteArray = bitmapData.getPixels(rect);
var encoded:String = Base64.encodeByteArray(byteArray);
var decoded:ByteArray = Base64.decodeToByteArray(encoded);
decoded.position = 0;
var newBmd:BitmapData = new BitmapData(rect.width,rect.height,true,0xFFFFFFFF);
newBmd.setPixels(rect, decoded);
var image:Bitmap = new Bitmap(newBmd, "auto", true);
addChild(image);
anyway , Your idea of store string in server isnt too good ... Why You dont send ByteArray , not String ?
You can also encode bitmapData to JPG or PNG , and then send bytes to php.
since it is an image, you have it in a BitmapData, let's say "myBmp" ... then use the following to extract all the data from BitmapData:
var bytes:ByteArray = myBmp.getPixels(myBmp.rect);
and the following to write:
myBmp.setPixels(myBmp.rect, bytes);
note that only the raw 32 bit pixel data is stored in the ByteArray, without compression, nor the dimensions of the original image.
for compression, you should refer to the corelib.