Archive for web development

screen sharing flash, java

Here is a simple code for screen sharing( ofcourse, it just sends one image):Capture screen and send to red5:

import org.red5.server.net.rtmp.*;
import org.red5.server.api.service.*;
import java.io.*;

public class Red5Client {
static byte [] imgbyte = new byte[1000000];

public static void main(String args[]) throws Exception{
final CaptureScreen capture = new CaptureScreen();
final RTMPClient client = new RTMPClient();
client.connect(“localhost”, 1935, “echo”, new IPendingServiceCallback() {
public void resultReceived(IPendingServiceCall call) {
Object[] param = new Object[1];
try{
capture.capture();
}
catch(Exception e){

e.printStackTrace();
}
param[0]=Red5Client.imgbyte;

System.err.println(“Connected: ” + call.getResult());
client.invoke(“echo”, param,
new IPendingServiceCallback() {
public void resultReceived(IPendingServiceCall call) {
System.err.println(“Received: ” + call.getResult());
}
});
}
});

}

}
import java.net.URLConnection;
import java.net.URL;
import java.awt.image.BufferedImage;

import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;

import javax.imageio.*;
public class CaptureScreen {

private Date startDate;
public static final int interval = 10;
public CaptureScreen(){

}

public void capture() throws Exception {

// wait for a user-specified time
try {

Thread.sleep(interval);

} catch(NumberFormatException nfe) {

}
// determine current screen size
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
Rectangle screenRect = new Rectangle(screenSize);
// create screen shot
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRect);
ByteArrayOutputStream out = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam encpar = encoder.getDefaultJPEGEncodeParam(image);

encoder.setJPEGEncodeParam(encpar);
encoder.encode(image);
System.out.println(out.size());
Viewer.image = Toolkit.getDefaultToolkit().createImage(out.toByteArray());
Red5Client.imgbyte = out.toByteArray();
System.out.println(“Length of image”+out.toByteArray().length);

// use System.exit if the program hangs after writing the file;
// that’s an old bug which got fixed only recently
// System.exit(0);
}

}

Flash Client:

import flash.display.*;
var so:SharedObject = null;
var ldr:Loader = new Loader();
addChild(ldr);

// Current release of FMS only understands AMF0 so tell Flex to
// use AMF0 for all NetConnection objects.
NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
init();

function init():void{
// Create the NetConnection and listen for NetStatusEvent and SecurityErrorEvent events
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);
nc.connect(“rtmp://127.0.0.1/echo”);
}

function shared_sync(event:SyncEvent) :void {
trace(“test”);
var list:Object=event.changeList;
for (var n in list) {
so_data=so.data[list[n].name];
var byteArr:ByteArray = new ByteArray();
for(i=0;i<so_data.length;i++){
byteArr[i]=so_data[i];

}
ldr.loadBytes(byteArr);

trace(“Length of shared Object”+so.data[list[n].name].length);
}

};

function netStatus(event:NetStatusEvent):void {
trace(“netStatus: ” + event);
var info:Object = event.info;
trace(info.code);
// lots more code here…
if (info.code == “NetConnection.Connect.Success”) {
trace(“Connect success”);
so = SharedObject.getRemote
(“test”, nc.uri, true);
so.addEventListener(SyncEvent.SYNC, shared_sync);

so.connect(nc);
}
else{
trace(“error”);
}

}

function netSecurityError(event:SecurityErrorEvent):void {
trace(“netSecurityError: ” + event);
}

//nc = new NetConnection();

//nc.onStatus = function(info) {

//}

//nc.connect(“rtmp://127.0.0.1/echo”);

/*
nc.onResult = function(obj) {
trace(“The result is ” + obj);
}
nc.call(“add”, nc, 1, 2);
*/

Comments (10)

Django Simplified

1. Download django trunk

2. Run:

#django_admin. py startproject projectname

# cd projectname

# python manage.py startapp appname

dont give too much though about project and app. At the the end of the they are just a python package

# Add the following lines in your projectname/url.conf for image files. Its required only for development.

(r’^resource/(?P<path>.*)$’, ‘django.views.static.serve’, {‘document_root’: ‘/home/aaaa/django/django-trunk/projectname/media/’}),
Dont use “media” as pattern here. It was already consumed by admin site.

Leave a Comment