Archive for April, 2008

How to create an image button in actionscript

class CustomSimpleButton extends SimpleButton {
private var upColor:uint   = 0xFFCC00;
private var overColor:uint = 0xCCFF00;
private var downColor:uint = 0×00CCFF;
private var size:uint      = 1000;
[Embed(source="../images/start.gif")]
private var startImage:Class;
public function CustomSimpleButton() {

// down state will show the image

downState      =  new startImage();
//new ButtonDisplayState(downColor, size);
overState      = new ButtonDisplayState(overColor, size);
upState        = new ButtonDisplayState(upColor, size);
hitTestState   = new ButtonDisplayState(upColor, size * 2);
hitTestState.x = -(size / 4);
hitTestState.y = hitTestState.x;
useHandCursor  = true;
}
}

class ButtonDisplayState extends Shape {
private var bgColor:uint;
private var size:uint;
[Embed(source="../images/start.gif")]
private var startImage:Class;
//public var className:String;

public function ButtonDisplayState(bgColor:uint, size:uint) {
this.bgColor = bgColor;
this.size    = size;
draw();

}

private function draw():void {
//if(className == null) {
//      className = describeType(this).@name.toXMLString();
//   }
//var imgRef:Class = getDefinitionByName(“startImage”) as Class;

var img:Bitmap = new startImage();
//graphics.beginFill(bgColor);
// graphics.beginBitmapFill(img.bitmapData, null, true);

graphics.beginFill(bgColor);
graphics.drawRect(0, 0, size, size);
graphics.endFill();
}
}

Leave a Comment

Mapreduce simplified

Mapreduce is a process to find out number of similar banana in Iraq and Afganistan.

For example, you want to know how many green and white banana in Iraq and Afganistan. So, you hire a terrorist in these two countries to count green and white banana and ask them to sent back the result to you.
Counting algorithm is secret as it may disclose the location of terrorist. The result:

std::map<string, int> afganbanana;

afganbanana['white'] =100;

afganbanana['green'] =700;

std::map<string,int> iraqbanana;

iraqbanana['white'] =-900;

iraqbanana['green'] =-700;

You recieved the data and merge this two map by a really complicated reduce function:

map<string,int> reduce(map<string,int> banana1, map<string, int> banana2){

return merge(banana1, banana2);

}

reduce(iraqbanana, afganbanana)

cout<<banana['white']<<”:”<<banana['green]<<endl;

output:

-800 : 0
I think very few people are concern about mapreduce except FBI. FBI is investing huge in google to research about the

correlation between white banana and Osama bin laden.

Leave a Comment

Structure your data in c++ using stl container map, set

how to orgnanize structure in a c++ program.

map of a {string–> pointing to a set of integer } and traverse all the string and element of set

A dumb and obvious solution:

// be careful about > > , single space between this two bracket

std::map<string, std::set<int> > strToIntSet;

// declare an iterator

std::map<string, std::set<int> >::iterator it;

for (it=strToIntSet.begin(); it!=strToIntSet().end(); it++){

std::cout<<it->first<<” : “;

std::set<int>::iteator it1;

// Now print out each element of set

for (it1=it->second.begin(); it1!=it->second.end(); it1++){

std::cout<<*it;
}
std::cout<<std::endl;

}

Assuming

Pretty simple.

Leave a Comment

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)

assets using actionscript

http://www.senocular.com/flash/tutorials/as3withmxmlc/  package {
	import flash.display.Sprite;
	public class EmbedAssets extends Sprite {

		[Embed(source="images/trophy.png")]
		private var TrophyImage:Class;

		[Embed(source="swfs/satdish.swf")]
		private var SatelliteAnimation:Class;

		public function EmbedAssets() {
			addChild(new SatelliteAnimation());
			addChild(new TrophyImage());
		}

	}
}

Leave a Comment

Actionscript webcam player.

This is a simple actionscript3 web cam player.
The player do both subscribe and publish video.

You can compile the code using flexbuilder.

Enjoy! Let me know if you actionscript2 player

Main class :

package liveplayer {
import flash.display.Sprite;
import flash.media.Video;
import lib.StreamPlayer;

public class liveplayer extends Sprite
{

public function liveplayer()
{
// TODO: move this to UI class
var vid:Video = new Video(320,240);
this.addChild (vid);

var stPlayer:StreamPlayer = new StreamPlayer(vid);
stPlayer.initialize_pub();
stPlayer.initialize_subscribe();

}
}
}

package lib{
// Player class

import flash.media.Camera;
import flash.media.Microphone;
import flash.media.Video;
import flash.net.*;
import flash.events.*;

import lib.Config;
public class StreamPlayer{

private var play_nc:NetConnection;
private var play_ns:NetStream;
private var pub_nc:NetConnection;
private var pub_ns:NetStream;

private var stat:Number;
private var myCamera:Camera = new Camera();
private var myMic:Microphone = new Microphone();
private var vidObj:Video;
public function StreamPlayer(vid:Video){

this.vidObj=vid;

}

public function initialize_subscribe():void{
// Get the camera
//myMic=Microphone.get();
// Connect to oflaDemo demo application in red5 server
play_nc = new NetConnection();
// Should be configurable
trace(Config.streamingURL);
play_nc.addEventListener(NetStatusEvent.NET_STATUS, sub_netStatus);
play_nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);
play_nc.connect(Config.streamingURL);

}

function sub_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”) {

// Publish video from the camera to the red5 server
play_ns = new NetStream(play_nc);
this.subscribe();
}

else{
trace(“error”);
}

}

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

public function initialize_pub():void{
// Get the camera
myCamera = Camera.getCamera();
//vidObj.attachCamera(myCamera);
myMic=Microphone.getMicrophone();

// Connect to oflaDemo demo application in red5 server
pub_nc = new NetConnection();
NetConnection.prototype.onBWDone = function(p_bw) {
trace(“onBWDone: “);
}
pub_nc.addEventListener(NetStatusEvent.NET_STATUS, pub_netStatus);
pub_nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);

// Should be configurable
pub_nc.connect(Config.streamingURL);

trace(Config.streamingURL);

}

// publish connection status callback
function pub_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”) {
// Publish video from the camera to the red5 server
pub_ns = new NetStream(pub_nc);
this.publish();
}

else{
trace(“error”);
}

}

public function publish():void{
pub_ns.attachAudio(myMic);
pub_ns.attachCamera(myCamera);
pub_ns.publish(Config.stPubPoint, “live”);

}

public function subscribe():void{
play_ns.play(Config.stPubPoint);
vidObj.attachNetStream(play_ns);

}

public function muteAudio():void{
//myMic.muted=true;
}

public function startAudio():void{

}

public function start_stop_subscribe():void{
play_ns.pause();

}

public function start_stop_pub():void{
pub_ns.pause();

}

}// End of class

}

package lib {
import lib.QueryString;

public class Config{
//public function Config(){
// Default value;
/* streamingURL= _root.streamingURL ? _root.streamingURL : “rtmp://127.0.0.1/oflaDemo”;
messHost=_root.messHost ? _root.messHost : “127.0.0.1″;
messPort=_root.messPort ? _root.messPort : 8081;
stPubPoint=_root.stPubPoint ? _root.stPubPoint : “example”;
pubID=_root.pubID ? _root.pubID : “himu”;
pageURL=_root.pageURL ? _root.pageURL : “http://test.com”;
*/
// This should be intialized from the URL
public static var qs:QueryString= new QueryString;
//public static var streamingURL:String=qs.parameters(’streamingURL’);
//public static var stPubPoint:String=qs.parameters(’stPubPoint’);
public static var streamingURL:String=”rtmp://yourip/oflaDemo”;
public static var stPubPoint:String=”example”;

}

}

http://www.chakribazaar.com

, a good job site

http://www.mappedia.net

Comments (2)