求Silverlight4调用摄像头的代码
不要求多少功能,只要能捕捉摄像头数据就ok
[解决办法]
# CaptureSource source = new CaptureSource();
#
# private void UserControl_Loaded(object sender, RoutedEventArgs e)
# {
# VideoCaptureDevice vcd;
# //话筒就换成这个类 AudioCaptureDevice
# vcd = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
#
# source.VideoCaptureDevice = vcd;
#
# VideoBrush video = new VideoBrush();
# video.SetSource(source);
# rectangle1.Fill = video;
#
# //截图的回调
# source.CaptureImageCompleted += new EventHandler<CaptureImageCompletedEventArgs>(source_CaptureImageCompleted);
#
# }
#
# //截图调用 source.CaptureImageAsync();
# void source_CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e)
# {
# WriteableBitmap img = e.Result;
# }
# //此处开始启动
# private void button1_Click(object sender, RoutedEventArgs e)
# {
# if (CaptureDeviceConfiguration.AllowedDeviceAccess
[解决办法]
# CaptureDeviceConfiguration.RequestDeviceAccess())
# {
# //启动摄像头 /麦克风以及其他操作
# source.Start();
# }
# }
public partial class Camera : Page
{
public Camera()
{
InitializeComponent();
InitLoad();
}
private ReadOnlyCollection<VideoCaptureDevice> videoDeviceList;//系统视频列表
//驱动接管类
CaptureSource cap = null;
private void InitLoad()
{
//找到系统默认视频
videoDeviceList = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices();
//把找到的视频硬件给Combox显示
Device.ItemsSource = videoDeviceList.Select(o => o.FriendlyName);
Device.SelectedIndex = 0;
if (Device.Items.Count == 1) Device.IsEnabled = false;
CamaraBtnStar.Click += new RoutedEventHandler(CamaraBtnStar_Click);
CamaraBtnStop.Click += new RoutedEventHandler(CamaraBtnStop_Click);
CamaraBtnScreenshots.Click += new RoutedEventHandler(CamaraBtnScreenshots_Click);
}
void CamaraBtnStar_Click(object sender, RoutedEventArgs e)
{
//视频开始接收
cap = new CaptureSource();
//托管驱动得到当前选中的视频设备
VideoCaptureDevice thisVideo = null;
if (!string.IsNullOrEmpty(Device.SelectedValue.ToString()))
{
thisVideo = videoDeviceList[Device.SelectedIndex];
}
if (thisVideo != null)
{
cap.VideoCaptureDevice = thisVideo;
VideoBrush br = new VideoBrush();
//设置源
br.SetSource(cap);
//填充VideoBrush 视频源
webBrush.Fill = br;
if (CaptureDeviceConfiguration.RequestDeviceAccess()){
cap.Start();
if (cap.State == CaptureState.Started)
{
//视频状态正常就可以截屏了,当异步图像捕获请求返回时发生
cap.CaptureImageCompleted += new EventHandler<CaptureImageCompletedEventArgs>(cap_CaptureImageCompleted);
}
}
}
else { MessageBox.Show("您的视频出现了异常"); }
}
void CamaraBtnStop_Click(object sender, RoutedEventArgs e)
{
//如果托管cap不为空
if (cap.State == CaptureState.Started && cap != null){cap.Stop();}
else{MessageBox.Show("您没有打开的视频");}
}
void CamaraBtnScreenshots_Click(object sender, RoutedEventArgs e)
{
//点击截图按钮启动异步图像捕获请求
if (cap.State == CaptureState.Started && cap != null) {
cap.CaptureImageAsync();
}
}
void cap_CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e)
{
//具体图像截取事件处理
var bitmap = e.Result;
imgCapture.Source = bitmap;
}
// 当用户导航到此页面时执行。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
}