7 Replies Latest reply on Aug 29, 2008 3:11 AM by kiwi_ziesch

    reading image dimension

    kiwi_ziesch Level 1
      hey,

      I´ve found an example for AIR to get the image dimensions of a jpg-file.
      Is there any possibility to do this thing with any other format like gif or png?
      does anybody know what´s the specific "marker" for these files in the header information?

      here´s the code I´ve found:

      <html>
      <head>

      <title>Determining Image Dimensions</title>

      <style type="text/css">
      body {
      font-family: Verdana, Helvetica, Arial, sans-serif;
      font-size: 11px;
      color: #0B333C;
      }
      </style>

      <script type="text/javascript" src="AIRAliases.js"></script>

      <script type="text/javascript">
      // File reference used for system browsing
      var file = null;

      // Called when the page has finished loading
      function doLoad()
      {
      // Setup a file reference for browsing
      file = air.File.documentsDirectory;
      file.addEventListener( air.Event.SELECT, doSelect );

      // Add event listener to button to trigger browse
      document.getElementById( 'btnBrowse' ).addEventListener( 'click', doBrowse );
      }

      // Called when the user wants to browse for a file
      function doBrowse()
      {
      // Setup filters to restrict to JPG images
      var filters = new runtime.Array();

      filters.push( new air.FileFilter( 'Image Files', '*.jpg' ) );

      // Browse the file system
      file.browseForOpen( 'Select Image', filters );
      }

      function doSelect()
      {
      // For reading binary data
      var header = new air.ByteArray();
      var stream = new air.FileStream();

      // JPEG app marker
      var marker = null;

      // Significant bit manipulation
      var least = 0;
      var most = 0;

      // Result of app data size
      var offset = 0;

      // Image width and height
      var height = 0;
      var width = 0;

      // Document findings in HTML element (DIV)
      var elem = null;

      // Read the entire selected file
      // TODO: Change to async
      stream.open( file, air.FileMode.READ );
      stream.readBytes( header, 0, 0 );
      stream.close();

      // Confirm JPEG
      if( header.readUnsignedByte().toString( 16 ) +
      header.readUnsignedByte().toString( 16 ) == 'ffd8' )
      {
      // Look for the app marker containing dimensions
      while( marker != 'ffc0' )
      {
      // Read the current marker
      marker = header.readUnsignedByte().toString( 16 ) + header.readUnsignedByte().toString( 16 );

      // Calculate app data size
      most = header.readUnsignedByte();
      least = header.readUnsignedByte();
      offset = least + ( most * 256 );

      // Jump ahead if the marker is not the one we want
      if( marker != 'ffc0' )
      {
      // Move forward without the two data size bytes
      header.position = header.position + offset - 2;
      } else {
      // Frame marker bit precision (typically 0x08)
      // Irrelevant bit for this, so just advance past it
      header.position = header.position + 1;
      }
      }
      } else {
      // Oops!
      alert( 'Not a JPEG' );
      }

      // Determine height
      most = header.readUnsignedByte();
      least = header.readUnsignedByte();

      height = least + ( most * 256 );

      // Determine width
      most = header.readUnsignedByte();
      least = header.readUnsignedByte();

      width = least + ( most * 256 );

      // Document findings
      elem = document.createElement( 'div' );
      elem.innerText = width + ' x ' + height;

      document.body.appendChild( elem );
      }
      </script>

      </head>
      <body onLoad="doLoad();">

      <input id="btnBrowse" type="button" value="Browse" />

      </body>
      </html>

      thanks a lot!