Elsewhere

Flash Media Server Sending Images

One of my students in my live web class is developing an interesting application that sends screenshots to other people. I put together some sample code to help him along and thought this would be of general interest.

Using the Flash Media Server with Remote Shared Objects this can be built. Here is a walk through of the code:

First of all, this uses the JPGEncoder class from the AS3CoreLib so you probably want to grab that and import it.

import com.adobe.images.JPGEncoder;

This example uses a SharedObject, a NetConnection and NetStreams for sending the video as well as the screen shots. Once the NetConnection is established, the SharedObject can be setup:

// Listener for connection
private function connectionHandler (e:NetStatusEvent):void
{
// If we are connected
if (e.info.code == "NetConnection.Connect.Success")
{
// Set up the shared object
// We'll call it SimpleSO, pass in the app url and not make it persistent
sharedObject = SharedObject.getRemote("SimpleSO",nc.uri,false);
// Tell the shared object to call methods in this class if requested
sharedObject.client = this;
// Add a listener for when shared object is changed
sharedObject.addEventListener (SyncEvent.SYNC,syncEventCallBack);
// Connect the shared object to our netConnection
sharedObject.connect(nc);
// All of the video streaming setup
doEverything();
// Register mouseclicks, how we'll determine when to send a frame
stage.addEventListener(MouseEvent.MOUSE_DOWN, saveFrame);
}
}

Here is the method that is called when the mouse is clicked. It creates a bitmapdata object, encodes as a JPEG and sends that as a bytearray through the shared object:

The SharedObject.send method calls the function “newBitmap” on everyone who is connected and passes in the jpgByteArray. The newBitmap function uses the Loader to uncompress the JPG and when it is done calls “gotBitmapData”:

The difference would not be significant. Perhaps if the ByteArray was not already encoded as a JPEG which is compressed it would make a big difference but since in my example the data is already JPEG encoded doing a compress on the byte array wouldn’t change the size much under normal circumstances.

The best way to make the data smaller in the above example is the modify the:
var jpgEncoder:JPGEncoder = new JPGEncoder(100);
line and choose a smaller number. 100 represents full quality, 50 would be 50% and so on.