6 Replies Latest reply on Jun 3, 2011 10:33 AM by Scott R. Hamilton

    Loading, cropping and saving jpg files in Flex

    Scott R. Hamilton Level 1

      I'm attempting to apply the code for selecting, cropping and saving image data (.jpg, .png) as shown on the web site:


      http://www.adobe.com/devnet/flash/quickstart/filereference_class_as3.html#articlecontentAd obe_numberedheader_1

       

      The sample application is contained within a class called Crop contained in the action script file Crop.as which begins as follows:

       

      package
      {
          import com.adobe.images.JPGEncoder;
          import com.adobe.images.PNGEncoder;
         
          import flash.display.BitmapData;
          import flash.display.DisplayObject;
          import flash.display.DisplayObjectContainer;
          import flash.display.Loader;
          import flash.display.LoaderInfo;
          import flash.display.Sprite;
          import flash.events.Event;
          import flash.events.IOErrorEvent;
          import flash.events.MouseEvent;
          import flash.geom.Matrix;
          import flash.geom.Rectangle;
          import flash.net.FileFilter;
          import flash.net.FileReference;
          import flash.utils.ByteArray;
          
          public class Crop extends Sprite
        


      My application attempts to make use of the Crop class by including the Crop.as file:
        
           <mx:Script source="Crop.as"/> 

       

      in the Flex Component which needs it, and then calling the Crop constructor to perform the methods of the class

       

         var localImage:Crop = new Crop();
         localImage.Crop();

       

      My application does not compile. The package statement (or the curly bracket on the following line) is flagged with the error "Packages may not be nested".

       

      I have no idea why this should be as I cannot see any nested definitions anywhere in my application. The component includes the Crop.as file which contains the package and class definition.

       

      Can anyone give my some guidance as to why this error is occurring?

       

      Any help would be greatly appreciated.  

        • 1. Re: Loading, cropping and saving jpg files in Flex
          UbuntuPenguin Level 4

          These lines looks kind of funny

           

          var localImage:Crop = new Crop()

          localImage.Crop();

          • 2. Re: Loading, cropping and saving jpg files in Flex
            Scott R. Hamilton Level 1

            Hi Ubuntu,

             

            I was trying to instanciate a Crop object and get the Crop process running with those two lines.

             

            But the more pressing question is why I get that compile error... "nested definitions not allowed"

             

             

            Scott

            • 3. Re: Loading, cropping and saving jpg files in Flex
              blazejewicz Level 4

              Hi Scott,

               

              #1

              that error is because "fx:Script" (or mx:Script) are intended to include part of Actionscript code - but not class defitions or blocks.

              So if you are trying to include e.g. such class:

               

               

               

              package

              {

                   import flash.display.Sprite;

                  

                   public class Crop extends Sprite

                   {

                        public function Crop()

                        {

                             super();

                        };

                        public function processImage():void

                        {

                             trace("processImage()");

                        }

                   }

              }

               

               

              compiler will immediately complain as you are trying to incorporate nested class within your application code.

               

              #2

              here is correct way (I think) to incorporate some class via mxml tags (here using fx:Script):

              Using class sample above:

               

               

               

              <?xml version="1.0" encoding="utf-8"?>

              <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                                xmlns:s="library://ns.adobe.com/flex/spark"

                                xmlns:mx="library://ns.adobe.com/flex/mx"

                                xmlns:utils="*"

                                applicationComplete="completeHandler(event)">

                   <!-- util Crop will be initialized here as instance "cropUtil" -->

                   <fx:Declarations>

                        <utils:Crop id="cropUtil"/>

                   </fx:Declarations>

                   <!-- script where "cropUtil" will be used -->

                   <fx:Script>

                        <![CDATA[

                             import mx.events.FlexEvent;

                             //

                             protected function completeHandler(event:FlexEvent):void

                             {

                                  cropUtil.processImage();

                             };

                        ]]>

                   </fx:Script>

              </s:Application>

               

               

               

              But note that you could simply create instance of your class by direct import in fx:Script:

               

               

                   <fx:Script>

                        <![CDATA[

                             import mx.events.FlexEvent;

                             //

                             private var cropUtil:Crop = null;

                             protected function completeHandler(event:FlexEvent):void

                             {

                                  cropUtil = new Crop();

                                  cropUtil.processImage();

                             };

                        ]]>

                   </fx:Script>

               

               

              hth,

              kind regards,

              Peter Blazejewicz

              • 4. Re: Loading, cropping and saving jpg files in Flex
                Scott R. Hamilton Level 1

                Hi Peter,

                 

                I tried adding

                 

                 

                  <mx:Declarations>
                      <utils:Crop id="cropUtil"/>
                  </mx:Declarations>

                 

                directly above the scipt section

                 

                <mx:Script>
                    <![CDATA[

                    .

                    .

                    .

                 

                But now I get the compiler error:

                 

                The prefix "utils" for element "utils:Crop" is not bound.

                 

                 

                If I use     import Crop; as the documentation says it should be done.

                 

                Another error occurs down the line in a class related to Crop called CropControl  where the following function (method)

                is defined:

                 

                        private function initButton(btn:Button):void
                        {
                            btn.label = "";
                            btn.width = BUTTON_SIZE;
                            btn.height = BUTTON_SIZE;
                            btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
                            addChild(btn);
                        }

                 

                The compiler doesn't like the Button keyword:

                 

                "Type not found or is not a compile time constant: Button"

                 

                This looks to me like the way to define and instantiate a button without using mxml code which should work. Could it be that the entire Crop utility is not usable in the Flex mxml world?

                 

                 

                Regards,

                 

                Scott

                • 5. Re: Loading, cropping and saving jpg files in Flex
                  blazejewicz Level 4

                  hi Scott,

                   

                  #1

                  "utils" is my own namespace introduced in "Application declaration:

                  xmlns:utils="*"

                  so it could be later reference directly in mxml code as:

                  <utils:CLASS_NAME_HERE />

                   

                  #2

                  yes, usually all content referenced in code should be imported. So - if your code does not already contains that - add:

                  import mx.controls.Button;

                  at top of your code.

                  see more on subject:

                  http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf61c8a-7ff4.html

                  ("Including versus importing ActionScript code");

                   

                  regards,

                  Peter

                  • 6. Re: Loading, cropping and saving jpg files in Flex
                    Scott R. Hamilton Level 1

                    Thanks Peter,

                     

                    sorry for the late reply  -  my account got mixed up somehow and I could not enter any discussions for the last two days.

                     

                    I'm busy converting all of the import statements into their AS 3 equivalents. One is not so easy to find:

                     

                         import fl.managers.StyleManager;

                     

                    Simply changing this to: import mx.managers.StyleManager; doesn't work.

                     

                    I'm wondering if it really makes any sense trying to modernize an application written at such an old release level and think pehaps Adobe should either re-cast it in AS3 or shut down this web page to keep learners like myself from spending countless hours trying to make use of it.

                     

                    Regards,

                     

                    Scott