what is the simpliest way to get jpg from bitmapData

坚强是说给别人听的谎言 提交于 2019-12-11 08:49:29

问题


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


回答1:


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



回答2:


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.




回答3:


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/



来源:https://stackoverflow.com/questions/5715934/what-is-the-simpliest-way-to-get-jpg-from-bitmapdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!