Silverlight之摄像头使用

 摄像头使用:
摄像头捕获动态图形的基本设计过程:
   ①获取摄像设备
   ②设置捕获源对象
   ③设置视频刷
   ④填充矩形显示

添加引用
using System.Windows.Media.Imaging; 
using System.IO;

public VideoCaptureDevice camera;  //定义摄像设备
public CaptureSource capturesource=new CaptureSource();
public VideoBrush videoBrush=new VideoBrush();

//启动摄像头
private void Action(){
     StartCamera();
     //如果允许启动获取的摄像头
     if(CaptureDeviceConfiguration.RequestDeviceAccess()){
          //启动摄像头
          capturesource.Start();
     }
}

private void StartCamera(){
     VideoCaptureDevice cameradevice=CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
     //如果没有配置摄像头
     if(cameradevice==null){
         //信息提示
     }else{
        camera=CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
        //设置捕获源对象设置为摄像头
        capturesource.VideoCaptureDevice=camera;
        //设置视频刷源
        videoBrush.SetSource(capturesource);
        //利用视频刷填充矩形对象
        this.rectangle.Fill=videoBrush;   //xaml页面创建容器
     }
}

//截取静态图片
private void CutImage(){
    this.capturesource.CaptureImageAsync();
    this.capturesource.CaptureImageCompleted+=(ss,se)=>
    {
       if(se.Error==null){
          this.image.Source=se.Result;
          //定义位图对象
          WriteableBitmap bitmap=se.Result;
          Image image=new Image();
          image.Width=160;
          image.Stretch=Stretch.Fill;
          //设置Image对象源,显示图形
          image.Source=bitmap;
          this.listbox.Items.Add(image);
       }else{
          return;
       }
    }
}

//保存图片
private void SaveImage(){
     //创建对象,源来自image
     WriteableBitmap bitmap=new WriteableBitmap(image,null);
     //保存文件
     if(bitmap!=null){
         SaveFileDialog saveJPEG=new SaveFileDialog();
         saveJPEG.Filter="JPEG Files (*.jpeg)|*.jpeg";
         if(saveJPEG.ShowDialog().Value){
            using(Stream jpegstram=saveJPEG.OpenFile()){
                SaveToJPEGFile(bitmap,jpegstream);
           }
         }
     }
}

private void SaveToJPEGFile(WriteableBitmap bitmap,Stram jpegstream){
   //获取image信息
   int width=bitmap.PixelWidth;
   int height=bitmap.PixelHeight;
   int unitrow=3;
   byte[][,] lattice=new byte[unitrow][,];
   建立图形区
   for(int i=0; i<unitrow; i++){
       lattice[i]=new byte[width,height];
  }
   for(int row=0;row<height;row++){
      for(int column=0;column<width; column++){
         int pixel=bitmap.Pixels[width*row+column];
         lattice[0][column,row]=(byte(pixel>>16));
         lattice[1][column,row]=(byte(pixel>>8));
         lattice[2][column,row]=(byte)pixel;
     }
  }
  //应用FJ.Core.dll中的类处理  色彩处理
    FluxJpeg.Core.ColorModel colormodel=new FluxJpeg.Core.ColorModel{
       colorspace=FluxJpeg.Core.ColorSpace.RGB
    };
    FluxJpeg.Core.Image image2=new FluxJpeg.Core.Image(colormodel,lattice);
    //将image2编码为JPEG
    MemoryStream stram=new MemoryStram();
    FluxJpeg.Core.Encoder.JpegEncoder jpegencoder=new FluxJpeg.Core.Encoder.JpegEncoder(image2,100,stram);
    jpegencoder.Encode();
    //回到stram的初始位置
    stream.Seek(0,SeekOrgin.Begin);
    //定义缓存区
    byte[] buffer=new Byte[stream.Length];
    //获取jpeg数据流,读入缓存
    long bytesRead=stream.Read(buffer,0,(int)stream.Length);
    //写入文件流
    jpegstream.Write(buffer,0,buffer.Length); 
}

//关闭摄像头
private void Close(){
  capturesource.Stop();
  this.rectangle.Fill=null;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值