RTMFP転送用カメラアプリ
Androidからカメラ画像転送テスト http://wonderfl.net/c/n1s4 でAndroidが無くてもテストできるように代替カメラアプリを作ってみました。
このページとAndroidからカメラ画像転送テスト http://wonderfl.net/c/n1s4 を同じLAN上の別々のPCで開けばテストできます。
/**
* Copyright mousepancyo ( http://wonderfl.net/user/mousepancyo )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/13aF
*/
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.media.Camera;
import flash.media.Video;
import com.bit101.components.*;
[SWF(width = 465, height = 465, frameRate = 30, backgroundColor = 0xF0F6F9)]
public class Main extends Sprite
{
private var _rtmfp:RTMFPConnect;
private var _msgID:int;
private var _msg:String = "";
private var _camera:Camera;
private var _videoContainer:Sprite = new Sprite();
private var _bt:PushButton;
public function Main()
{
_rtmfp = RTMFPConnect.getInstance();
_rtmfp.doConnect();
//
_rtmfp.addEventListener(RTMFPConnect.NC_CONNECTED, onNcConnect);
_rtmfp.addEventListener(RTMFPConnect.GROUP_CONNECTED, onGroupConnect);
_rtmfp.addEventListener(RTMFPConnect.GROUP_NEIGHBOR, onGroupNeighbor);
_rtmfp.addEventListener(RTMFPConnect.GROUP_POSTED, onGroupPosted);
//
_bt = new PushButton(this, 185, 400, "SHOT", onMsg);
var video:Video = new Video(640, 480);
_videoContainer.addChild(video); addChild(_videoContainer);
_videoContainer.scaleX = _videoContainer.scaleY = .5;
_videoContainer.x = (465 - 320) * .5; _videoContainer.y = (465 - 240) * .4;
setupCamera(video);
}
private function onNcConnect(e:Event):void
{
}
private function onGroupConnect(e:Event):void
{
}
private function onGroupNeighbor(e:Event):void
{
}
private function onGroupPosted(e:Event):void
{
}
private function onMsg(e:MouseEvent):void
{
var bmd:BitmapData = new BitmapData(640, 480);
bmd.draw(_videoContainer);
var vec:Vector.<uint> = bmd.getVector(bmd.rect);
var rect:Rectangle = bmd.rect;
_rtmfp.sendImage(rect, vec);
bmd.dispose();
}
//カメラ
private function setupCamera(video:Video, cX:int = 640, cY:int = 480, fps:Number = 15):void{
var camX:int = cX
var camY:int = cY
_camera = new Camera()
_camera = Camera.getCamera();
_camera.setMode(camX,camY,fps);
video.attachCamera(_camera);
}
private function removeCamera(video:Video):void{
video.attachCamera(null);
_camera = null
}
}
}
//
import flash.geom.Rectangle;
import flash.display.Bitmap;
import flash.display.BitmapData;
internal class BitmapImage extends Bitmap
{
public function BitmapImage(rect:Rectangle, vec:Vector.<uint>)
{
var bmd:BitmapData = new BitmapData(rect.width, rect.height, true, 0);
bmd.setVector(rect, vec);
super(bmd);
}
}
//
//package {
import flash.events.EventDispatcher;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.NetGroup;
import flash.net.GroupSpecifier;
import flash.events.NetStatusEvent;
import flash.events.Event;
import flash.geom.Rectangle;
internal class RTMFPConnect extends EventDispatcher {
// EventConst
public static const NC_CONNECTED:String = "nc_connected";
public static const NC_CONNECT_FAILED:String = "nc_connect_failed";
public static const GROUP_CONNECTED:String = "group_connected";
public static const GROUP_NEIGHBOR:String = "group_Neighbor";
public static const GROUP_DISCONNECT:String = "group_disconnect";
public static const GROUP_POSTED:String = "group_posted";
public static const NS_CONNECTED:String = "ns_connected";
public static const NS_START_PUBLISH:String = "ns_start_publish";
// GroupName
private const GROUP_NAME:String = "toppig_catch"
//
private var _nc:NetConnection;
private var _rns:NetStream;
private var _ng:NetGroup;
private var _gs:GroupSpecifier;
public var msg:String;
public var w:Number;
public var h:Number;
public var x:Number;
public var y:Number;
public var vec:Vector.<uint>;
private static var _instance:RTMFPConnect;
//
public function RTMFPConnect(enforcer:SingletonEnforcer) { }
public static function getInstance():RTMFPConnect {
if(RTMFPConnect._instance == null){
RTMFPConnect._instance = new RTMFPConnect(new SingletonEnforcer());
}
return RTMFPConnect._instance;
}
public function doConnect():void {
_nc = new NetConnection();
_nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
_nc.connect("rtmfp:");
}
private function netStatusHandler(e:NetStatusEvent):void {
trace(e.target, e.info.code)
switch(e.info.code){
case "NetConnection.Connect.Success":
dispatchEvent(new Event(NC_CONNECTED));
addNetGroup();
break;
case "NetConnection.Connect.Failed":
dispatchEvent(new Event(NC_CONNECT_FAILED));
break;
case "NetConnection.Connect.Rejected":
//
break;
case "NetGroup.Connect.Success":
dispatchEvent(new Event(GROUP_CONNECTED));
recavedMsg();
break;
case "NetGroup.Connect.Failed":
//
break;
case "NetGroup.Connect.Rejected":
//
break;
case "NetGroup.Neighbor.Connect":
dispatchEvent(new Event(GROUP_NEIGHBOR));
break;
case "NetGroup.Neighbor.Disconnect":
dispatchEvent(new Event(GROUP_DISCONNECT));
break;
case "NetGroup.Posting.Notify":
w = e.info.message.width;
h = e.info.message.height;
x = e.info.message.x;
y = e.info.message.y;
vec = e.info.message.img;
dispatchEvent(new Event(GROUP_POSTED));
break;
case "NetStream.Connect.Success":
dispatchEvent(new Event(NS_CONNECTED));
break;
case "NetStream.Publish.Start":
dispatchEvent(new Event(NS_START_PUBLISH));
break;
}
}
//NetGroup
public function addNetGroup():void{
_gs = new GroupSpecifier(GROUP_NAME);
_gs.postingEnabled = true;
_gs.ipMulticastMemberUpdatesEnabled = true;
_gs.multicastEnabled = true;
_gs.addIPMulticastAddress("225.225.0.1:30303");
// NetGroup
_ng = new NetGroup(_nc, _gs.groupspecWithAuthorizations());
_ng.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
}
//SendMsg
public function sendMsg(str:String = ""):void{
var message:Object = new Object();
message.text = str;
message.sender = _ng.convertPeerIDToGroupAddress(_nc.nearID);
_ng.post(message);
}
//SendImage
public function sendImage(rec:Rectangle, v:Vector.<uint>):void{
var message:Object = new Object();
message.img = v;
message.width = rec.width;
message.height = rec.height;
message.x = rec.x;
message.y = rec.y;
message.sender = _ng.convertPeerIDToGroupAddress(_nc.nearID);
_ng.post(message);
}
//recavedMsg
public function recavedMsg():void{
_rns = new NetStream(_nc, _gs.groupspecWithAuthorizations());
_rns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
}
}
//}
internal class SingletonEnforcer {}