开发者

what is the simpliest way to get jpg from bitmapData

开发者 https://www.devze.com 2023-02-27 04:45 出处:网络
I need to convert bitmapData grabbed from movie clip开发者_如何学编程 to jpeg or png. Is there some lib for that?see this example in take portion that convert byte to image, i have given whole for you

I need to convert bitmapData grabbed from movie clip开发者_如何学编程 to jpeg or png. Is there some lib for that?


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);
        }
    }   
}


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.


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消