[ActionScript3.0] Use jpegencoderoptions or pngencoderoptions to save images to local

Source: Internet
Author: User

Before Flash Player 11.3 and air3.3, we can take advantage of these third-party classes (Jpegencoder), which are easy to handle. Now, with encode and jpegencoderoptions, it's much easier and faster to process bitmap data.

The Flash.display.BitmapData.encode () method allows you to compress the bitmap data natively into one of the compression formats as:

  • PNG -Using PNG compression, you can choose to use fast compression, which emphasizes compression speed rather than file size. To use PNG compression, pass a new flash.display.PNGEncoderOptions object as BitmapData.encode() The second argument to the method.

  • JPEG -with JPEG compression, you can choose to specify image quality. To use JPEG compression, pass a new flash.display.JPEGEncoderOptions object as BitmapData.encode() The second argument to the method.

  • JPEGXR -with JPEG Extended range (XR) compression, you can choose to specify the color channel, loss, and entropy (entropy) encoding settings. To use JPEGXR compression, pass a new flash.display.JPEGXREncoderOptions object as BitmapData.encode() the second argument to the method.

You can use this feature of image processing as part of a server upload or download workflow.

The following sample code fragment uses a JPEGEncoderOptions compressed BitmapData object:

var New BitmapData (640,480,false, 0x00FF00);   var New ByteArray (); Bitmapdata.encode (newnew

For jpegencoderoptions parameters, the value is between 1 and 100. When the value is 1, the quality is the lowest and 100 is the highest quality. The default value is 80, and at this point the quality of the bitmap processed is already good.

Importflash.net.FileReference;ImportFlash.utils.ByteArray;ImportFlash.display.Bitmap;ImportFlash.display.BitmapData;Importflash.display.JPEGEncoderOptions;ImportFlash.filesystem.File;ImportFlash.filesystem.FileMode;ImportFlash.filesystem.FileStream;Importflash.system.Security;ImportFlash.geom.Rectangle; var_filerefer:filereference =Newfilereference ();var_bytearr:bytearray =NewByteArray ();functionSave ():void{    varRect:rectangle =NewRectangle (0,0,724,640); varBmpd:bitmapdata =NewBitmapData (Rect.width,rect.height,true, 0);    Bmpd.draw (_ Picture container); varJpegencoder:jpegencoderoptions =NewJpegencoderoptions (80);    Bmpd.encode (Bmpd.rect,jpegencoder,_bytearr); if(_bytearr! =NULL)    {        varDate:date =NewDate (); varstr:string = date.fullYear.toString () + (Date.month + 1). toString () + date.date.toString () + date.hours + date.minutes + D Ate.seconds +". jpg"; if(Security.sandboxType.toString () = ="Application")        {            varFile:file =File.userDirectory.resolvePath (str); varFilestream:filestream =NewFileStream ();            Filestream.openasync (file, filemode.write);            Filestream.writebytes (_bytearr); Alertpanel.getinstance (). Show ("Picture saved successfully, save address: \ n"+file.nativepath,true, stage);        Trace (File.nativepath); }        Else{_filerefer.save (_BYTEARR,STR); }    }}

[ActionScript3.0] Use jpegencoderoptions or pngencoderoptions to save images to local

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.