5 Replies Latest reply on Mar 24, 2008 10:32 AM by john isaacks

    Saving BitmapData as jpeg file

      I've been working on an AIR image editing application for a few weeks now and i'm suddenly realizing that there seems to be no easy way to save BitmapData as a JPEG file. I have found flash examples that call php scripts in order to save jpegs, but is this even possible when running only local code? How can I do this??? Will this functionality be available any time soon? Any suggests of ways to do this would be GREATLY appreciated.

        • 1. Re: Saving BitmapData as jpeg file
          anirudhs Level 2

          Check out as3corelib. It has an actionscript 3 JPEG and PNG encoder which does exactly what you want.
          • 2. Re: Saving BitmapData as jpeg file
            rrbrambley3 Level 1
            Thanks, I actually came across it a little while after I posted the question... what a relief! The next thing I'd like is a TIFF encoder, but I don't think there's anything for that yet...
            • 3. Re: Saving BitmapData as jpeg file
              john isaacks Level 1
              hi I am trying to save .png files from bitmap data for custom icons.

              I downloaded the as3corelib but I am confused by the contents. is there anyway someone can post a install/usage example?

              • 4. Re: Saving BitmapData as jpeg file
                rrbrambley3 Level 1
                Well I have only used the JPGEncoder class, but the PNGEncoder seems to work the same way... All I needed was something to take my BitmapData object and give me a ByteArray that can be written to meet the jpeg format specification. So basically, you'd do something like this:

                //assuming you already have some BitmapData called myBitmapData
                var stream:FileStream = new FileStream();
                stream.open(newFile, FileMode.WRITE);
                var bytes:ByteArray = new PNGEncoder().encode(myBitmapData);

                If you need to do something a little different, you might need to look through the functions in the PNGEncoder class a little more.

                Also, if you were implying you're actually confused as to how to import the corelib, you should be able to either import it as a new project or import the com.adobe.images package into your current project and use the classes that way.

                Hopefully this helps... I'm still sorta just getting the hang of this stuff myself.
                • 5. Re: Saving BitmapData as jpeg file
                  john isaacks Level 1
                  thank you very mush Emphysema, I just copied the single class to my air file directory and I had to change:

                  package com.adobe.images
                  // because the location of the class changed.
                  public static function encode(img:BitmapData):ByteArray {
                  public function encode(img:BitmapData):ByteArray {
                  // not sure why but it was giving me a error saying it was static so I removed the static keyword from the function and it worked.