4

I'm building an App with actionscript 3.0 in my Flash builder. This is a followup question this question.

I need to upload the bytearray to my server, but the function i use to convert the bitmapdata to a ByteArray is super slow, so slow it freezes up my mobile device. My code is as follows:

var jpgenc:JPEGEncoder = new JPEGEncoder(50);
trace('encode');
//encode the bitmapdata object and keep the encoded ByteArray

    var imgByteArray:ByteArray = jpgenc.encode(bitmap);
temp2 = File.applicationStorageDirectory.resolvePath("snapshot.jpg");
    var fs:FileStream = new FileStream();
    trace('fs');
    try{     
     //open file in write mode     
     fs.open(temp2,FileMode.WRITE);
          //write bytes from the byte array

     fs.writeBytes(imgByteArray);
          //close the file

     fs.close();
         }catch(e:Error){

Is there a different way to convert it to a byteArray? Is there a better way?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Myy
  • 18,107
  • 11
  • 37
  • 57
  • Regardless of the performance of the code, why are you not running the work in a background thread along with the upload? Unless you can limit the input image size you will always find users who select big images and will dislike a non responsive app. – Grzegorz Adam Hankiewicz Feb 21 '13 at 19:33
  • @GrzegorzAdamHankiewicz In AS3, multithreading is very new and not yet supported on mobile. – Michael Brewer-Davis Feb 21 '13 at 23:42

5 Answers5

1

Try to use blooddy library: http://www.blooddy.by . But i didn't test it on mobile devices. Comment if you will have success.

Ilya Zaytsev
  • 1,055
  • 1
  • 8
  • 12
1

Use BitmapData.encode(), it's faster by orders of magnitude on mobile http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#encode%28%29

matthias_buehlmann
  • 4,641
  • 6
  • 34
  • 76
0

You should try to find a JPEG encoder that is capable of encoding asynchronously. That way the app can still be used while the image is being compressed. I haven't tried any of the libraries, but this one looks promising:

http://segfaultlabs.com/devlogs/alchemy-asynchronous-jpeg-encoding-2

It uses Alchemy, which should make it faster than the JPEGEncoder from as3corelib (which I guess is the one you're using at the moment.)

david.emilsson
  • 2,313
  • 1
  • 20
  • 24
  • 1
    Alchemy is not recommended for mobile - an ANE would be ideal –  Feb 21 '13 at 20:59
  • Encoding asynchronously only moves the performance problem around. BitmapData.encode() is a far better way of speeding up the JPEG encoding. – Ori Pessach May 28 '15 at 15:29
0

A native JPEG encoder is ideal, asynchronous would be good, but possibly still slow (just not blocking). Another option:

var pixels:ByteArray = bitmapData.getPixels(bitmapData.rect);
pixels.compress();

I'm not sure of native performance, and performance definitely depends on what kind of images you have.

Michael Brewer-Davis
  • 14,018
  • 5
  • 37
  • 49
0

The answer from Ilya was what did it for me. I downloaded the library and there is an example of how to use it inside. I have been working on getting the CameraUI in flashbuilder to take a picture, encode / compress it, then send it over via a web service to my server (the data was sent as a compressed byte array). I did this:

by.blooddy.crypto.image.JPEGEncoder.encode( bmp, 30 );

Where bmp is my bitmap data. The encode took under 3 seconds and was easily able to fit into my flow of control synchronously. I tried async methods but they ultimately took a really long time and were difficult to track for things like when a user moved from cell service to wifi or from tower to tower while an upload was going on.

Comment here if you need more details.

Jesse Rallo
  • 163
  • 1
  • 2
  • 9
  • Now I am finding that the blooddy library is SUPER fast on Android devices (less than 2 seconds for pictures taken at 8 Megapixels on teh highest resolution on the device). HOWEVER - it does NOT work on iOS. It causes my application to hang, and the iOS to kill the program. – Jesse Rallo Apr 26 '13 at 13:21
  • Is that still the case? Have you been able to resolve for iOS? – Tom Auger Oct 08 '15 at 20:34