JPEG encoder super slow, how to Optimize it?

2020-04-10 23:16发布

问题:

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? thanks in advanced!

~Myy

回答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.



回答2:

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



回答3:

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.)



回答4:

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.



回答5:

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.