您的位置:首页 > 其它

p2p 视频的几点想法

2009-10-31 20:43 162 查看
我到看过一些系统,具体怎么实现却不知道,我在网上看到过视屏电话系统(adobe公司)。后来又看到了一些blog,猜想,是每秒从摄像图中届若干张图,在两边传递就可以了。

这只是我的初步想法,而且网上也有类型代码:

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” initialize=”vedioi()” >
<mx:Script>
<![CDATA[
import flash.media.Camera;
import mx.graphics.ImageSnapshot;
import mx.core.UIComponent;
private function vedioi():void {
var cam:Camera = Camera.getCamera();
txtw.text = cam.quality.toString();
cam.setQuality(320, 99);
vid.maintainAspectRatio = true;
vid.attachCamera(cam);

}
private function print(u:UIComponent):void{
var bmp:BitmapData = ImageSnapshot.captureBitmapData(u);
var i:Bitmap = new Bitmap(bmp);
img.source = i;
img.scaleContent = true;
}
]]>
</mx:Script>
<mx:Label x=”0″ y=”0″ width=”50″ height=”20″ id=”txtw”>
</mx:Label>
<mx:Panel x=”33″ y=”121″ width=”884″ height=”349″ layout=”absolute”>
<mx:VideoDisplay x=”10″ y=”10″ id=”vid” width=”324″ height=”289″/>
<mx:Image x=”355″ y=”10″ width=”333″ height=”289″ id=”img” />
<mx:Button x=”746″ y=”23″ label=”截图” click=”print(vid)” />
</mx:Panel>
</mx:Application>

ImageSnapshot类可以给可视的组件拍照
仔细修改希望能够把视频录制先来,搜见都是通过假设strp服务器完成的,用http的方法整了好久没弄好,下面是个半成品。 而且很耗性能,但是挺有意义。每秒钟截取10张图片发送到服务器。

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” >
<mx:Script>
<![CDATA[
import mx.messaging.AbstractConsumer;
import flash.media.Camera;
import mx.graphics.ImageSnapshot;
import mx.core.UIComponent;
import flash.utils.ByteArray;
import flash.net.*;
import flash.utils.Timer;
import flash.events.TimerEvent;

import flash.events.*;
import mx.controls.Alert;

import com.adobe.images.JPGEncoder;
private var cam:Camera;
private var havec:Boolean;
private var cap:Boolean;
private var mytime:Timer;
private var capArr:Array;
private var request:URLRequest;
private var loader:URLLoader;
private function vedioi():void {
cam = Camera.getCamera();
cam.setQuality(320, 99);
vid.maintainAspectRatio = true;
vid.attachCamera(cam);
cap = false;
mytime = new Timer(1000/15,0);
mytime.addEventListener(TimerEvent.TIMER, capturecam);
mytime.addEventListener(TimerEvent.TIMER_COMPLETE, okswf);
request = new URLRequest('a.php');
request.method = URLRequestMethod.POST;
request.contentType = "application/octet-stream";
loader = new URLLoader();
capArr = new Array();
}
private function print(u:UIComponent):void{
if(!cam) {
Alert.show('没有开启摄像头或者没有安装摄像头');
return ;
}
if(!cap) {
capture.label = '停止捕获';
mytime.start();
cap = true;
} else {
capture.label = '开始捕获';
mytime.stop();
cap = false;
var requestswf:URLRequest = new URLRequest('swf.php');

loader.load(requestswf);
Alert.show('ok');
}
return;
//var bmp:BitmapData = ImageSnapshot.captureBitmapData(u);
//var i:Bitmap = new Bitmap(bmp);
//img.alpha = 0.7;
//img.source = i;
//img.scaleContent = true;
//var request:URLRequest = new URLRequest(URL);
//var encoder:JPGEncoder = new JPGEncoder(90);
//var bytes:ByteArray = encoder.encode(i.bitmapData);
//Alert.show();
var request:URLRequest = new URLRequest('a.php');
//data值就为图片编码数据ByteArray;
//var variables:URLVariables = new URLVariables();
//variables.ids = new Date().getTime();
//variables.tt = "tss";
//variables.imgd = bytes;
//request.data = bytes;
request.method = URLRequestMethod.POST;
//这个是关键,内容类型必须是下面文件流形式;
request.contentType = "application/octet-stream";
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, upok);
// loader.addEventListener(IOErrorEvent.IO_ERROR,errorf);
loader.load(request);
}
private function upok(e:Event):void {
Alert.show('上传成功');
}
private function errorf(e:Event):void {
Alert.show('error');
}
private function uptest():void {
if(!cam) {
Alert.show('没有开启摄像头或者没有安装摄像头');
return;
}
var request:URLRequest = new URLRequest("a.php");
request.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
var bytes:ByteArray;
var str:String;
var temstr:String = new String();
var ar:Array;
bytes = capArr.pop();

while(bytes) {
str = new String(bytes);
//ar.push(str);
temstr += str;
bytes = capArr.pop();
}
//Alert.show(ar.join('x').toString());
var postvar:URLVariables = new URLVariables();
postvar.dat = temstr;

request.data = postvar;
//request.data = ar.join('xxxx').toString();
//request.data = img.source.
//var bmp:BitmapData = capArr.pop();
//var encoder:JPGEncoder = new JPGEncoder(90);
//var bytes:ByteArray = encoder.encode(bmp);
//这个是关键,内容类型必须是下面文件流形式;
// request.contentType = "application/octet-stream";
try {
loader.load(request);
}
catch (error:SecurityError)
{
Alert.show('ok');
}
loader.addEventListener(Event.COMPLETE, upok);
/*
loader.addEventListener(Event.COMPLETE, upok);
loader.addEventListener(IOErrorEvent.NETWORK_ERROR,errorf);
loader.load(request);
*/
}
private function capturecam(e:TimerEvent):void {
var bmp:BitmapData = ImageSnapshot.captureBitmapData(vid);
var encoder:JPGEncoder = new JPGEncoder(80);
var bytes:ByteArray = encoder.encode(bmp);
capArr.push(bytes);
var i:Bitmap = new Bitmap(bmp);
//img.alpha = 0.7;
img.source = i;
img.scaleContent = true;
request.data = bytes;
try {
loader.load(request);
}
catch (error:SecurityError)
{
trace('error');
}
}
]]>
</mx:Script>

<mx:Panel x=”110.5″ y=”173″ width=”520″ height=”315″ layout=”absolute”>
<mx:VideoDisplay x=”10″ y=”44″ id=”vid” width=”241″ height=”221″/>
<mx:Image x=”266″ y=”44″ width=”224″ height=”221″ id=”img” />
<mx:Button x=”10″ y=”10″ label=”打开视频” click=”vedioi()” />
<mx:Button x=”81″ y=”10″ label=”捕获视频” id=”capture” click=”print(vid)”/>
<!–<mx:Button x=”165″ y=”10″ label=”上传” click=”uptest()”/>–>
</mx:Panel>
<mx:Button x=”152″ y=”59″ click=”Alert.show(’dd’)” label=”Button”/>
</mx:Application>
有很多注释的冗余代码也懒得去了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: