Base64Helper.as 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package com.imt.intimamedia.helpers
  2. {
  3. import com.hurlant.util.Base64;
  4. import flash.display.BitmapData;
  5. import flash.geom.Rectangle;
  6. import flash.utils.ByteArray;
  7. import mx.utils.Base64Decoder;
  8. import mx.utils.Base64Encoder;
  9. public class Base64Helper
  10. {
  11. public function Base64Helper()
  12. {
  13. }
  14. public static function convertToBase64( value : BitmapData ) : String
  15. {
  16. if( !value )
  17. return null;
  18. var byteArray : ByteArray = value.getPixels(new Rectangle( 0, 0, value.width, value.height ));
  19. var encoder : Base64Encoder = new Base64Encoder();
  20. encoder.encodeBytes( byteArray, 0, byteArray.length );
  21. var base64data : String = encoder.flush();
  22. return base64data;
  23. }
  24. public static function convertFromBase64( value : String, width : int, height : int ) : BitmapData
  25. {
  26. if( !value )
  27. return null;
  28. var decoder : Base64Decoder = new Base64Decoder;
  29. decoder.decode( value );
  30. var byteArray : ByteArray = decoder.toByteArray();
  31. byteArray.position = 0;
  32. var bitmapData : BitmapData = new BitmapData( width, height, true );
  33. bitmapData.setPixels(new Rectangle( 0, 0, width, height ), byteArray );
  34. return bitmapData;
  35. }
  36. }
  37. }