一聚教程网:一个值得你收藏的教程网站

热门教程

wpf调用摄像头/切换摄像头/保存图片及像素问题

时间:2022-06-25 08:42:38 编辑:袖梨 来源:一聚教程网


一篇说一下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();
    }
}

热门栏目