Flex博文
捕获Camera并保存图片到本地(照相功能) -samhy
作者:admin 日期:2010-07-12
字体大小: 小 中 大
捕获Camera并保存图片到本地(照相功能)
这几天对Camera类进行了一下规整,并利用JPGEncoder类实现了照相的功能,代码如下:
XML/HTML代码
import flash.media.Camera;
import flash.system.Security;
import flash.events.StatusEvent;
import flash.text.TextField;
import flash.events.ActivityEvent;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.net.FileReference;
import com.adobe.images.JPGEncoder;
//Security.showSettings(SecurityPanel.PRIVACY);
//打开安全设置
var timer:Timer = new Timer(100);
timer.addEventListener(TimerEvent.TIMER,gettimer);
var txt:TextField = new TextField();
addChild(txt);
btn.visible = false;
var vid:Video;
var camera:Camera = Camera.getCamera();
var bitdata:BitmapData;
var bitmap:Bitmap;
if(camera == null){ txt.text = ("没有摄像头");
btn.visible = false; } else { txt.text = ("PC上面有摄像头");
Connectting();
}
function Connectting(){
vid = new Video(320,240);
vid.attachCamera(camera);
vid.x = 0;
vid.y = 50;
addChild(vid);
camera.addEventListener(StatusEvent.STATUS,getStatus);//查看用户是否允许访问摄像头
}
function getStatus(e:StatusEvent):void{
trace(e.code);
if(e.code == "Camera.Muted"){
txt.text = ("用户拒绝访问摄像头");
btn.visible = false;
} else{
txt.text = ("用户允许访问摄像头");
timer.start();
camera.addEventListener(ActivityEvent.ACTIVITY,getactivity);//查看摄像头的活动
camera.setQuality(0,100);
camera.setMode(320,240,24,true);//保证画质
btn.visible = true;
btn.addEventListener(MouseEvent.CLICK,saveImg);
}
}
function gettimer(e:TimerEvent):void{
SXTSL.text = String(camera.currentFPS);
WZZP.text = String(camera.keyFrameInterval);
SPYS.text = String(camera.loopback);
IMGQ.text = String(camera.quality);
}
function getactivity(e:ActivityEvent):void{
ML.text = String(camera.motionLevel);
XYL.text = String(camera.activityLevel);
}
function saveImg(e:MouseEvent):void{
bitdata= new BitmapData(320,240,true,0x000000);
bitdata.draw(vid);
var jpg:JPGEncoder = new JPGEncoder(100);
var file:FileReference = new FileReference();
var byteArray:ByteArray = jpg.encode(bitdata);
file.save(byteArray,"libufan.jpg");//如果要改文件名,请在修改的文件名后面加.jpg
}
来源于:http://www.as3hope.com/read.php?3