最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
wpf调用摄像头/切换摄像头/保存图片及像素问题
时间:2016-08-23 编辑:简简单单 来源:一聚教程网
一篇说一下AForge.net具体使用和注意的地方。
1、获取设备上的所有摄像头:
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// 设定初始视频设备
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
xcount = videoDevices.Count;
if (videoDevices.Count > 0)
{ // 默认设备
sourcePlayer.VideoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
}
sourcePlayer.Start();
// 设置图片框初始图像
BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
Properties.Resources.noimage.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
fingerPictureBox1.InitialImage = bs;
if (sourcePlayer.IsRunning)
{
button_Capture.IsEnabled = true;
}
}
sourcePlayer.IsRunning标示的是摄像头是否工作。
2、多个摄像头切换:
///
/// 切换摄像头
///
///
///
private void button_Qh_Click(object sender, RoutedEventArgs e)
{
if (j < xcount-1)
{
j = j + 1;
}
else if (j == xcount-1)
{
j = 0;
}
sourcePlayer.Stop();
sourcePlayer.VideoSource = new VideoCaptureDevice(videoDevices[j].MonikerString);
sourcePlayer.Start();
}
3、拍摄照片:
///
/// 拍摄图像
///
///
///
private void button_Capture_Click(object sender, RoutedEventArgs e)
{
// 判断视频设备是否开启
try
{
if (sourcePlayer.IsRunning)
{ // 进行拍照
if (fingerPictureBox1.Visibility == Visibility.Hidden)
{
object box = this.FindName("fingerPictureBox1");
if (box is FingerPictureBox)
{
(box as FingerPictureBox).ActiveImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
sourcePlayer.GetCurrentVideoFrame().GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
fingerPictureBox1.Visibility = Visibility.Visible;
cam_sumit.Visibility = Visibility.Hidden;
lb_text.Content = "启动摄像头";
}
else {
fingerPictureBox1.Visibility = Visibility.Hidden;
cam_sumit.Visibility = Visibility.Visible;
lb_text.Content = "拍摄图像";
}
}
}
catch {
MessageBox.Show("请等待摄像头准备就绪再拍照!");
}
}
4、保存并返回:
///
/// 保存返回
///
///
///
private void button_Close_Click(object sender, RoutedEventArgs e)
{
var tempfilepath = "";
RenderTargetBitmap bmp = new RenderTargetBitmap(
(int)fingerPictureBox1.ActualWidth, (int)fingerPictureBox1.ActualHeight,
96, 96, PixelFormats.Default);
fingerPictureBox1.Measure(fingerPictureBox1.RenderSize);
fingerPictureBox1.Arrange(new Rect(fingerPictureBox1.RenderSize));
bmp.Render(fingerPictureBox1);
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
CaptureData = ms.ToArray();
var temppath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString("yyyy-mm-dd-hh-mm-sss") + ".jpg";
File.WriteAllBytes(temppath, CaptureData);
tempfilepath = FileOperation.AddFile(Common.Contract.WebModel.AyncModuleEnum.系统拍照, temppath, true);
}
if (CameraDoEvent != null)
{
CameraDoEvent(tempfilepath);
}
this.Close();
}
5、拍摄窗口关闭时一定要关闭摄像头,不然摄像头会一直开着:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (sourcePlayer.IsRunning)
{ // 停止视频
sourcePlayer.SignalToStop();
sourcePlayer.WaitForStop();
sourcePlayer.Stop();
}
}
-
上一个: C++成员函数调用方式用法详解
-
下一个: wpf连接摄像头并配置参数使用代码
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10