Reducing png file size in as3 - actionscript-3

I tried to reduce the size of the png file in AS3
I used mLoader to load the png file and got the png's bitmap
public function savePng(bitmap:Bitmap,scale:Number):void
{
var bitmapData:BitmapData = new BitmapData(bitmap.width*scale,bitmap.height*scale);
bitmapData.drawWithQuality(mLoader,new Matrix(scale,0,0,scale), null, null, null , true, StageQuality.BEST);
var pngEncorder:PNGEncoder = new PNGEncoder();
var bytes:ByteArray = pngEncorder.encode(bitmapData);
}
But when I saved the image,it appeared jagged.
I also used nid.image.encoder.PNGEncoder and PNGEncoder2,they were all useless.

Related

change the color of flash cs6 createjs exported lib object

i've testing some code in using createjs + box2dweb, i exported a vector ball in blue color drawn by flash cs 6, here is part of the code.
var birds = (function() {
var spawn = function() {
//circle = new lib.ball1();
//var birdBMP = new createjs.Bitmap("images/bird.png");
var birdBMP = new lib.ball1();
birdBMP.x = Math.round(Math.random()*500);
birdBMP.y = -30;
birdBMP.regX = 60.75; // important to set origin point to center of your bitmap
birdBMP.regY = 60.75;
birdBMP.snapToPixel = true;
birdBMP.mouseEnabled = false;
stage.addChild(birdBMP);
box2d.createBird(birdBMP);
}
return {
spawn: spawn
}
})();
here i want to do change the color of the blue ball, how can i do that?
******updated*******
inclucde js
change code
birdBMP.cache(0, 0, 121.5, 121.5);
colorRan1 = Math.round(Math.random()*255);
colorRan2 = Math.round(Math.random()*255);
colorRan3 = Math.round(Math.random()*255);
birdBMP.filters = [
new createjs.ColorFilter(0,0,0,1, colorRan1,colorRan2,colorRan3,0)
];
birdBMP.updateCache();
I don't know too much about the createjs Flash Extension, but I highly suspect, that from the Flash Export there is also no way to simply "change" a color - you have probably three options:
1) ColorMatrixFilter - this involves caching ect. and I wouldn't recommend this for your purpose.
2) Draw another ball in the desired color and change the Bitmap to that other (differently colored) ball.
3) Or if it is a simple shape you can use the exported lib.ball1() method and extend it with a color-parameter. - In that case you would also have to change the references bitmap/shape, if you want to change the color post-creation.

How to import picture in flash with transaprent background

How do I make transparent background to imported image in flash, because now i imported it and image has white box around it.
code for adding images to stage
var imageBD = (Math.floor(Math.random()*2))? new Trees() : new Rocks;
var bitmap:Bitmap = new Bitmap();
var BD:BitmapData = new BitmapData(imageBD.width, imageBD.height);
BD.draw(imageBD);
bitmap.bitmapData = BD;
bitmap.width = mRadius * 2 * mToPx;
bitmap.height = mRadius * 2 * mToPx;
bitmap.x = pxStartX;
bitmap.y = pxStartY;
this.addChild(bitmap);
obstacleImages.push(bitmap)
Since i'm mew i cannot post images so i'm giving you a link to image:http://prntscr.com/pugdl
You need to specifically tell the BitmapData (docs) object to be transparent.
In your case, replace this line:
var BD:BitmapData = new BitmapData(imageBD.width, imageBD.height);
...with this:
var BD:BitmapData = new BitmapData(imageBD.width, imageBD.height, true, 0x00000000);
Make sure the image itself has transparency. General file types for these kinds of images are PNG or GIF.
You shouldn't have to do anything for Flash to find the transparency.

Flex PNGEncoder lose transparent quality

I've got problem with transparent using PNGEncoder class. When I encode BitmapData to png and use it as source of my Image, it looks terrible. I attach example. There are two images - first colorful and above him white with alpha gradient.
Image before save
Image after save
I've used some other libraries like AsPngEncoder, but it didn't help. It's code I use:
var bd:BitmapData = new BitmapData(container.width, container.height, true, 0xffffff);
bd.draw(container);
var pngenc:PNGEncoder = new PNGEncoder();
var pngByteArray:ByteArray = pngenc.encode(bd);
container.source = pngByteArray;
var fl:File = File.applicationStorageDirectory.resolvePath("./images/file.png");
var fs:FileStream = new FileStream();
fs.open(fl, FileMode.WRITE);
fs.writeBytes(pngByteArray);
fs.close();
Try to use new Flash Player 11.3 feature
http://help.adobe.com/en_US/as3/dev/WS4768145595f94108-17913eb4136eaab51c7-8000.html

Save JPG from CDROM to PC

I am using following function to Save the output from SWF and send it to a PHP page so that it creates JPG, my problem if there is not INTERNET connection and the SWF is in a CDROM can the Save function be used in FLASH so that it outputs JPG and save it on a computer.
In short can we Save movieclip output as an JPG
/**
Screenshot and jpg output
**/
import flash.display.BitmapData;
import flash.geom.Matrix;
//Buttons handlers. Should add an extra function because delegate doesn't allow to pass parameters
shaF.onPress = mx.utils.Delegate.create(this,makeShadow);
//Helper functions to pass parameters
function makeShadow() { capture(0) }
/*
create a function that takes a snapshot of the Video object whenever it is called
and shows in different clips
*/
function capture(nr){
this["snapshot"+nr] = new BitmapData(abc._width,abc._height);
//the bitmap object with no transformations applied
this["snapshot"+nr].draw(abc,new Matrix());
var t:MovieClip = createEmptyMovieClip("bitmap_mc"+nr,nr);
//positions clip in correct place
//t._x = 350; t._y = 10+(nr*130); t._xscale = t._yscale = 50
//display the specified bitmap object inside the movie clip
t.attachBitmap(this["snapshot"+nr],1);
output(nr);
//attachMovie("print_but", "bot"+nr, 100+nr, {_x:t._x+t._width+50, _y:t._y+t._height/2})
}
//Create a new bitmapdata, resize it 50 %, pass image data to a server script
// using a LoadVars object (large packet)
function output(nr){
//Here we will copy pixels data
var pixels:Array = new Array()
//Create a new BitmapData
var snap = new BitmapData(this["snapshot"+nr].width, this["snapshot"+nr].height);
//Matrix to scale the new image
myMatrix = new Matrix();
myMatrix.scale(1, 1)
//Copy image
snap.draw(this["snapshot"+nr], myMatrix);
var w:Number = snap.width, tmp
var h:Number = snap.height
//Build pixels array
for(var a=0; a<=w; a++){
for(var b=0; b<=h; b++){
tmp = snap.getPixel32(a, b).toString(16)
//if(tmp == "-fcffff")
//{
//tmp="-ff0000";
//}
pixels.push(tmp.substr(1))
}
}
//Create the LoadVars object and pass data to PHP script
var output:LoadVars = new LoadVars()
output.img = pixels.toString()
output.height = h
output.width = w
//The page (and this movie itself) should be in a server to work
output.send("show.php", "output", "POST")
}
stop()
Since you already have the BitmapData, this is fairly easy. Modifying your output function:
//Copy image
snap.draw(this["snapshot"+nr], myMatrix);
//Now check if we want to save as JPG instead of sending data to the server
if (runningFromCdrom) {
var encoder:JPEGEncoder = new JPEGEncoder();
var bytes:ByteArray = encoder.encode(snap);
var file:FileReference = new FileReference();
file.save(bytes);
} else {
// ...the rest of your output method...
}
How to determine the runningFromCdrom value is up to you. If the SWF is being run inside an HTML document, the best way to tell if the program is being run from a CD-ROM would be to specify it in the FlashVars.
Yes, you can save JPEG's directly from Flash. There is no need for the intermediate step to PHP.
You can use one of these JPEG encoders:
https://github.com/mikechambers/as3corelib
http://www.switchonthecode.com/tutorials/flex-tutorial-an-asynchronous-jpeg-encoder
I would recommend the second as it can work asynchronously (kind of) which means your UI shouldn't lock up while encoding the JPEG.

How to load an image in memory with Flex/ActionScript?

So I'm trying to load an embedded image this way:
[Bindable]
[Embed(source="path")]
private var cls_img:Class;
var img:Image = new Image();
img.source = cls_img;
Now I'm trying to copy pixel chunks from it however I get an error that img.bitmapData is null and the error goes away when I add it to the application with addElement(img);
Isn't it possible to force flex to load the image in memory so I can manipulate it without adding it to the stage?
Yes - you can use cls_img as a BitmapAsset.
[Bindable]
[Embed(source="path")]
private var cls_img:Class;
...
var asset:BitmapAsset = new cls_img() as BitmapAsset;
// The asset has a bitmapData property that gives you access to the image data
img.source = asset;
For more information, check out the documentation:
http://livedocs.adobe.com/flex/3/html/help.html?content=embed_4.html