Skip to content

Commit

Permalink
Functionalities added to the client and server. Some AS3 library impr…
Browse files Browse the repository at this point in the history
…ovements.

Signed-off-by: Juan Carlos del Valle <[email protected]> (imekinox)
  • Loading branch information
imekinox committed Jan 4, 2011
1 parent f453c9d commit 8bdfe1d
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 123 deletions.
10 changes: 6 additions & 4 deletions wrappers/actionscript/org/as3kinect/as3kinect.as
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
public static const GET_VIDEO:int = 1;
public static const MIRROR_DEPTH:int = 2;
public static const MIRROR_VIDEO:int = 3;
public static const MIN_DEPTH:int = 4;
public static const MAX_DEPTH:int = 5;

public static const MOVE_MOTOR:int = 0;
public static const LED_COLOR:int = 1;
Expand All @@ -62,9 +64,9 @@
public static const BLOB_COLOR:uint = 0xFFFFFFFF;
public static const BLOB_FILL_COLOR:uint = 0xFF0000FF;
public static const BLOB_PROCESSED_COLOR:uint = 0x00FF00FF;
public static const BLOB_MIN_WIDTH:uint = 30;
public static const BLOB_MAX_WIDTH:uint = 120;
public static const BLOB_MIN_HEIGHT:uint = 30;
public static const BLOB_MAX_HEIGHT:uint = 120;
public static const BLOB_MIN_WIDTH:uint = 15;
public static const BLOB_MAX_WIDTH:uint = 30;
public static const BLOB_MIN_HEIGHT:uint = 15;
public static const BLOB_MAX_HEIGHT:uint = 80;
}
}
81 changes: 0 additions & 81 deletions wrappers/actionscript/org/as3kinect/as3kinectBlobs.as

This file was deleted.

42 changes: 42 additions & 0 deletions wrappers/actionscript/org/as3kinect/as3kinectDepth.as
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,25 @@ package org.as3kinect {
import org.as3kinect.as3kinectSocket;

import flash.utils.ByteArray;
import flash.display.BitmapData;

public class as3kinectDepth {
private var _socket:as3kinectSocket;
private var _data:ByteArray;
private var _depth_busy:Boolean;
private var _is_mirrored:Boolean;
private var _min_distance:int;
private var _max_distance:int;
public var bitmap:BitmapData;

public function as3kinectDepth(){
_socket = as3kinectSocket.instance;
_data = new ByteArray;
_depth_busy = false;
_is_mirrored = false;
_min_distance = 600;
_max_distance = 800;
bitmap = new BitmapData(as3kinect.IMG_WIDTH, as3kinect.IMG_HEIGHT, false, 0xFF000000);
}

/*
Expand Down Expand Up @@ -90,5 +97,40 @@ package org.as3kinect {
return _is_mirrored;
}

public function set minDistance(distance:int):void
{
_data.clear();
_data.writeByte(as3kinect.CAMERA_ID);
_data.writeByte(as3kinect.MIN_DEPTH);
_data.writeInt(int(distance));
if(_socket.sendCommand(_data) != as3kinect.SUCCESS){
throw new Error('Depth: Cannot change mirror state');
} else {
_min_distance = distance;
}
}

public function get minDistance():int
{
return _min_distance;
}

public function set maxDistance(distance:int):void
{
_data.clear();
_data.writeByte(as3kinect.CAMERA_ID);
_data.writeByte(as3kinect.MAX_DEPTH);
_data.writeInt(int(distance));
if(_socket.sendCommand(_data) != as3kinect.SUCCESS){
throw new Error('Depth: Cannot change mirror state');
} else {
_max_distance = distance;
}
}

public function get maxDistance():int
{
return _max_distance;
}
}
}
Empty file modified wrappers/actionscript/org/as3kinect/as3kinectMotor.as
100644 → 100755
Empty file.
183 changes: 183 additions & 0 deletions wrappers/actionscript/org/as3kinect/as3kinectUtils.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
*
* This file is part of the OpenKinect Project. http://www.openkinect.org
*
* Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file
* for details.
*
* This code is licensed to you under the terms of the Apache License, version
* 2.0, or, at your option, the terms of the GNU General Public License,
* version 2.0. See the APACHE20 and GPL20 files for the text of the licenses,
* or the following URLs:
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.gnu.org/licenses/gpl-2.0.txt
*
* If you redistribute this file in source form, modified or unmodified,
* you may:
* 1) Leave this header intact and distribute it under the same terms,
* accompanying it with the APACHE20 and GPL20 files, or
* 2) Delete the Apache 2.0 clause and accompany it with the GPL20 file, or
* 3) Delete the GPL v2.0 clause and accompany it with the APACHE20 file
* In all cases you must keep the copyright notice intact and include a copy
* of the CONTRIB file.
* Binary distributions must follow the binary distribution requirements of
* either License.
*
*/

package org.as3kinect
{
import org.as3kinect.as3kinect;

import flash.display.BitmapData;
import flash.display.Stage;
import flash.display.DisplayObject;

import flash.geom.Rectangle;
import flash.geom.Point;

import flash.filters.ColorMatrixFilter;
import flash.events.TouchEvent;
import flash.utils.ByteArray;

public class as3kinectUtils
{

/*
* Draw ARGB from ByteArray to BitmapData object
*/
public static function byteArrayToBitmapData(bytes:ByteArray, _canvas:BitmapData):void{
_canvas.lock();
_canvas.setPixels(new Rectangle(0,0, as3kinect.IMG_WIDTH, as3kinect.IMG_HEIGHT), bytes);
_canvas.unlock();
}

/*
* Process blobs from BitmapData, if _w and _h set they will be returned in that resoluton
*/
public static function getBlobs(r:BitmapData, _w:Number = 0, _h:Number = 0):Array
{
//if _w and _h not specified use as3kinect constants
if(_w == 0) _w = as3kinect.IMG_WIDTH;
if(_h == 0) _h = as3kinect.IMG_HEIGHT;

var i:int;
var blobs:Array = new Array();

//Looking fot the blobs
while (i < as3kinect.MAX_BLOBS)
{
//Look for BLOB_COLOR in the BitmapData and genrate a rectanglo enclosing the Blobs of that color
var mainRect:Rectangle = r.getColorBoundsRect(as3kinect.BLOB_MASK, as3kinect.BLOB_COLOR);
//No blobs found (Exit)
if (mainRect.isEmpty()) break;
var xx:int = mainRect.x;
//Looking in mainRect for a pixel with BLOB_COLOR
for (var yy:uint = mainRect.y; yy < mainRect.y + mainRect.height; yy++)
{
if (r.getPixel32(xx, yy) == as3kinect.BLOB_COLOR)
{
//Use floodFill to paint that blob to BLOB_FILL_COLOR (works like paint fill tool)
r.floodFill(xx, yy, as3kinect.BLOB_FILL_COLOR);
//Now our blob is not BLOB_COLOR we get the rect of our recently painted blob (this is our i blob)
var blobRect:Rectangle = r.getColorBoundsRect(as3kinect.BLOB_MASK, as3kinect.BLOB_FILL_COLOR);
//Looking if our blob fits our desired min/max width and height
if (blobRect.width > as3kinect.BLOB_MIN_WIDTH
&& blobRect.width < as3kinect.BLOB_MAX_WIDTH
&& blobRect.height > as3kinect.BLOB_MIN_HEIGHT
&& blobRect.height < as3kinect.BLOB_MAX_HEIGHT)
{
//Create the blob object
var blob:Object = {};
//Add a rect to the object
blob.rect = blobRect;
//Convert blob positions to float and then multiply per requested width and height
blob.rect.x = ((blob.rect.x / as3kinect.IMG_WIDTH) * _w);
blob.rect.y = ((blob.rect.y / as3kinect.IMG_HEIGHT) * _h);
blob.rect.width = (blob.rect.width / as3kinect.IMG_WIDTH) * _w;
blob.rect.height = (blob.rect.height / as3kinect.IMG_HEIGHT) * _h;
//The point is the center of the rect
var _x:int = blob.rect.x + (blob.rect.width / 2);
var _y:int = blob.rect.y + (blob.rect.height / 2);
//Add a point to the object
blob.point = new Point(_x, _y);
blobs.push(blob);
}
//Finally we paint our blob to a BLOB_PROCESSED_COLOR so we can discard it in the next pass
r.floodFill(xx, yy, as3kinect.BLOB_PROCESSED_COLOR);
}
}
i++;
}
return blobs;
}

/*
* Convert a BitmapData into black and white BitmapData with a ColorMatrixFilter
*/
public static function setBlackWhiteFilter(obj:BitmapData, threshold:int = 128):void
{
var rLum:Number = 0.2225;
var gLum:Number = 0.7169;
var bLum:Number = 0.0606;
var matrix:Array = [rLum * 256, gLum * 256, bLum * 256, 0, -256 * threshold,
rLum * 256, gLum * 256, bLum * 256, 0, -256 * threshold,
rLum * 256, gLum * 256, bLum * 256, 0, -256 * threshold,
0, 0, 0, 1, 0 ];
var filter:ColorMatrixFilter = new ColorMatrixFilter(matrix);
obj.applyFilter(obj, new Rectangle(0,0,obj.width,obj.height), new Point(0,0), filter);
}

/*
* Fire a touch event in the desired point
* Id is used so we can detect the TouchOut of that specific point
* _lastTouched Array is used to store whether this point has already been fired or not
* _stage is needed to have access to getObjectsUnderPoint
*/
public static function fireTouchEvent(id:int, point:Point,_lastTouched:Array, _stage:Stage):void
{
if(_lastTouched[id] == undefined) _lastTouched[id] = new Array;
var targets:Array = _stage.getObjectsUnderPoint(point);
var i:int;
var max:int;
var found:Boolean;
var local:Point;
//We look in the lastTouched arrar for this point id
for (var key in _lastTouched[id])
{
//If TOUCH_OVER was already fired we look for it in the new targets object
if (_lastTouched[id][key].bool != false)
{
found = false;
for (i = 0,max = targets.length; i < max; i++)
{
if (targets[i].name == _lastTouched[id][key].obj.name)
{
found = true;
break;
}
}
//If is not in the targets object we fire the TOUCH_OUT event
if (found == false)
{
_lastTouched[id][key] = {bool:false,obj:_lastTouched[id][key].obj};
local = _lastTouched[id][key].obj.parent.globalToLocal(new Point(point.x,point.y));
_lastTouched[id][key].obj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_OUT,true,false,0,false,local.x,local.y));
}
}
}
//We look now into the targets object
for (i = 0,max = targets.length; i < max; i++)
{
var item:DisplayObject = targets[i];
local = item.parent.globalToLocal(new Point(point.x,point.y));
//if the object is new (was not in the lastTouched array OR the TOUCH_OVER is not already fired we fire TOUCH_OVER event
if (! _lastTouched[id][item.name] || _lastTouched[id][item.name].bool !== true)
{
_lastTouched[id][item.name] = {bool:true,obj:item};
item.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_OVER,true,false,0,false,local.x,local.y));
}
}
}
}
}
9 changes: 6 additions & 3 deletions wrappers/actionscript/org/as3kinect/as3kinectVideo.as
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
*
* This file is part of the OpenKinect Project. http://www.openkinect.org
*
Expand Down Expand Up @@ -31,24 +31,27 @@ package org.as3kinect {
import org.as3kinect.as3kinectSocket;

import flash.utils.ByteArray;
import flash.display.BitmapData;

public class as3kinectVideo {
private var _socket:as3kinectSocket;
private var _data:ByteArray;
private var _video_busy:Boolean;
private var _is_mirrored:Boolean;
public var bitmap:BitmapData;

public function as3kinectVideo(){
_socket = as3kinectSocket.instance;
_data = new ByteArray;
_video_busy = false;
_is_mirrored = false;
bitmap = new BitmapData(as3kinect.IMG_WIDTH, as3kinect.IMG_HEIGHT, false, 0xFF000000);
}

/*
* Tell server to send the latest video frame
* Note: We should lock the command while we are waiting for the data to avoid lag
*/
* Note: We should lock the command while we are waiting for the data to avoid lag
*/
public function getBuffer():void {
if(!_video_busy) {
_video_busy = true;
Expand Down
Loading

0 comments on commit 8bdfe1d

Please sign in to comment.