1

I need to convert bitmapData grabbed from movie clip to jpeg or png. Is there some lib for that?

Eugeny89
  • 3,797
  • 7
  • 51
  • 98

3 Answers3

7

see this example in take portion that convert byte to image, i have given whole for your reference

package {
    import encoding.JPGEncoder;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.utils.ByteArray;
    import flash.events.Event

    public class Converter extends Sprite {

        [Embed(source = "image.jpg")]
        private var image:Class

        private const QUALITY:uint = 80;

        public function Converter():void {

            var bitmap:Bitmap = new image();
            var bitmapData:BitmapData = bitmap.bitmapData;

            //encode BitmapData to JPG 
            var encoder:JPGEncoder = new JPGEncoder(QUALITY);
            var rawBytes:ByteArray = encoder.encode(bitmap.bitmapData);

            //decode JPG ByteArray back to BitmapData
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, getBitmapData)
            loader.loadBytes(rawBytes); 
        }

        private function getBitmapData(e:Event):void  {
            var decodedBitmapData:BitmapData = Bitmap(e.target.content).bitmapData
            trace(decodedBitmapData);
        }
    }   
}
Nirmal- thInk beYond
  • 11,847
  • 8
  • 35
  • 46
6

You can now use the native jpeg encoding built into Flash Player 11.3 and AIR 3.3.

var quality:int = 50;
var bounds:Rectangle = bitmapData.rect;
var result:ByteArray = bitmapData.encode(bounds, new JPEGEncoderOptions(50));

This requires -swf-version=16 be passed to the compiler.

Sean Fujiwara
  • 4,506
  • 22
  • 34
1

This is not the simplest way, but depending on the dimensions of your image, may be worth a look. Jens Krause has compiled jpeglib with Alchemy, and it encodes much faster than as3corelib's version, or even Thibault Imbert's improved AS3 version:

http://www.websector.de/blog/2009/06/21/speed-up-jpeg-encoding-using-alchemy/

Adam Smith
  • 1,917
  • 12
  • 14