1 Reply Latest reply on Apr 9, 2010 10:31 AM by Joe ... Ward

    Resizing selected image and making it a File

    DaanLy

      Hey,

       

      I'm working on a CMS but I can figure out this problem.
      Idea:
      Client chooses image on his/her HDD -> Adobe AIR makes a resized version of the selected File.

      Code:

      ---- My File Class ----

      private var _localImage:File;

       

      ---- Client clicks on the browse button ------
                private function onBrowseBtnClick( event:MouseEvent ):void
              {
                  var fileFilter:FileFilter = new FileFilter( "Images", "*.jpg;*.gif;*.png" );
                 
                  try
                  {
                      _localImage.browse( [fileFilter] );
                      _localImage.addEventListener( Event.SELECT, onFileBrowseSelect );
                  }
                  catch( error:Error )
                  {
                      trace("Failed:", error.message);
                  }
              }
             
      ---- Client selects image ------
              private function onFileBrowseSelect( event:Event ):void
              {
                  imageInput.text = _localImage.nativePath;
                  imgLoader.load( "file:///" + _localImage.nativePath );
                  saveBtn.enabled = revertBtn.enabled = true;
              }

       

      At this stage the client can upload images but always on a 1x1 scale.

       

      Is there a way to manipulate the File so that it has a fixed width of 500?

      I got all the uploading codes etc. just need to find a way to alter the selected image (give the right height and width), make it a File and upload it.

      I tried BitmapArrays and BitmapDatas but I cant figure it out.

      Any ideas?

        • 1. Re: Resizing selected image and making it a File
          Joe ... Ward Level 4

          There are a few ways to do this.

           

          You could use the built-in image support of AIR. You can use the Loader class to load JPG and PNG images as bitmaps. You can then use the BitmapData API to resize the images. Finally, you reencode the altered bitmap using the PNGEncoder or JPEGEncoder classes available in Flex and also in http://code.google.com/p/as3corelib/.Gif images aren't supported.

           

          You could load the file, decode the image into some kind of data that is amenable to the manipulations you want, and then re-encode the image into the target data format. In other words, write all the image decoding, manipulation and encoding logic yourself.

           

          You could use the Alchemy project to convert an existing C library that does what you want (ImageMagick http://www.imagemagick.org/script/index.php comes to mind). Its helpful if you have some knowledge of C or C++. See http://labs.adobe.com/technologies/alchemy/

           

          In AIR 2, you could also use something like ImageMagick through the NativeProcess API, which lets you run native applications from you AIR app. You do have to give up the easy, cross-platform distribution of the .air file format, though. (You must create a native installer for each platform you support.)