#需要引用:AForge类库、Microsoft.DirectX



using System;
using System.Windows.Forms;
namespace CameraTest
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void button5_Click(object sender, EventArgs e)
    {
      Form2 form = new Form2();
      form.Text = "拍照";
      form.Show();
    }
    private void button1_Click(object sender, EventArgs e)
    {
      Form3 form = new Form3();
      form.Text = "声音";
      form.Show();
    }
  }

}

using AForge.Video.DirectShow;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace CameraTest
{
  public partial class Form2 : Form
  {
      private FilterInfoCollection videoDevices;
      public Form2()
      {
        InitializeComponent();
      }
      private void videoSourcePlayer()
      {
        VideoCaptureDevice videoCapture = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
        #pragma warning disable CS0612 // 类型或成员已过时
        videoCapture.DesiredFrameSize = new Size(320, 240);
        #pragma warning restore CS0612 // 类型或成员已过时
        #pragma warning disable CS0612 // 类型或成员已过时
        videoCapture.DesiredFrameRate = 1;
        #pragma warning restore CS0612 // 类型或成员已过时
        videoPlayer1.VideoSource = videoCapture;
        videoPlayer1.Start();
      }
      private void button4_Click_1(object sender, EventArgs e)
      {
        try
         {
            //枚举视频输入设备
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (videoDevices.Count == 0)
            {
              throw new ApplicationException();
            }
            foreach (FilterInfo device in videoDevices)
            {
              comboBox1.Items.Add(device.Name);
            }
              comboBox1.SelectedIndex = 0;
          }
      catch
        {
          comboBox1.Items.Add("没有视频设备!");
          videoDevices = null;
        }
      }

      private void button1_Click_1(object sender, EventArgs e)
      {
        videoSourcePlayer();
      }

      private void button3_Click_1(object sender, EventArgs e)
      {
        //拍照

        if (videoPlayer1 == null)
        return;
        Bitmap bitmap = videoPlayer1.GetCurrentVideoFrame();
        saveFileDialog1.Filter = "Jpg 图片|*.jpg|Bmp 图片|*.bmp|Gif 图片|*.gif|Png 图片|*.png|Wmf 图片|*.wmf";
        saveFileDialog1.FilterIndex = 0;
        saveFileDialog1.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
        saveFileDialog1.CheckPathExists = true;//检查目录
        saveFileDialog1.FileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + "-"; ;//设置默认文件名
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
          bitmap.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);// image为要保存的图片
          MessageBox.Show(this, "图片保存成功!", "信息提示");
        }
        bitmap.Dispose();
      }
      private void button2_Click_1(object sender, EventArgs e)
      {
        videoPlayer1.SignalToStop();
        videoPlayer1.WaitForStop();
        this.Close();
      }
      private void Form2_Load(object sender, EventArgs e)
      {}
    }
}

using Microsoft.DirectX.DirectSound;
using System;
using System.Threading;
using System.Windows.Forms;
namespace CameraTest
{
  public partial class Form3 : Form
  {
      private const int SAMPLES = 8;
      private static int deviceIndex = -1;
      private CaptureBuffer buffer=null;
      private static Capture cap = null;
      private string deviceName = "没有检测到音频输入设备";

      ///
      // *****更改处
      ///
      private static AutoCompleteStringCollection deviceNames;
      private Thread liveVolumeThread;
      private int sampleDelay = 100;
      private int frameDelay = 10;
      private static int[] SAMPLE_FORMAT_ARRAY = { SAMPLES, 2, 1 };
      public Form3()
      {
        InitializeComponent();
        progressBar1.Maximum = Int16.MaxValue + 1;
        CheckForIllegalCrossThreadCalls = false;
      }
      /// <summary>
      /// 加载麦克风列表
      /// </summary>
      public void Stop()
      {
        if (liveVolumeThread != null)
        {
          liveVolumeThread.Abort();
          liveVolumeThread.Join();
          liveVolumeThread = null;
        }

        if (buffer != null)
        {
          if (buffer.Capturing)
          {
            buffer.Stop();
          }

          buffer.Dispose();
          buffer = null;
        }
      }

    public void Start()
    {
      Stop();
      if (deviceIndex != -1)
      {
        // capture 对象 捕获麦克风设备

        ///
        // *****更改处
        ///
        CaptureDevicesCollection audioDevices = new CaptureDevicesCollection();
        cap = new Capture(audioDevices[deviceIndex].DriverGuid);
        // 创建对 缓冲区信息 的描述
        CaptureBufferDescription desc = new CaptureBufferDescription();
        WaveFormat wf = new WaveFormat();
        wf.BitsPerSample = 16;
        wf.SamplesPerSecond = 44100;
        wf.Channels = 2;
        // 数据的最小的原子单元
        wf.BlockAlign = (short)(wf.Channels * wf.BitsPerSample / 8);
        // 单位采样点的字节数
        wf.AverageBytesPerSecond = wf.BlockAlign * wf.SamplesPerSecond;
        // 未经压缩的PCM
        wf.FormatTag = WaveFormatTag.Pcm;

        desc.Format = wf;
        desc.BufferBytes = SAMPLES * wf.BlockAlign;
        // 创建 capturebuffer对象
        buffer = new CaptureBuffer(desc, cap);
        // 捕捉数据至缓存
        buffer.Start(true);

        liveVolumeThread = new Thread(new ThreadStart(updateProgress));
        liveVolumeThread.Priority = ThreadPriority.Lowest;
        liveVolumeThread.Start();
      }
    }
    private void updateProgress()
    {
      while (true)
      {
        int tempFrameDelay = frameDelay;
        int tempSampleDelay = sampleDelay;
        ///
        // *****更改处
         ///
        label1.Text = "0";
        Array samples = buffer.Read(0, typeof(Int16), LockFlag.FromWriteCursor, SAMPLE_FORMAT_ARRAY);
        int goal = 0;
        for (int i = 0; i < SAMPLES; i++)
        {
          goal += (Int16)samples.GetValue(i, 0, 0);
        }
        goal = (int)Math.Abs(goal / SAMPLES);
        double range = goal - progressBar1.Value;
        double exactValue = progressBar1.Value;
        double stepSize = range / tempSampleDelay * tempFrameDelay;
        if (Math.Abs(stepSize) < .01)
        {
          stepSize = Math.Sign(range) * .01;
        }
        double absStepSize = Math.Abs(stepSize);

        if ((progressBar1.Value == goal))
        {
          Thread.Sleep(tempSampleDelay);
        }
        else
        {
          do
          {
            if (progressBar1.Value != goal)
            {
              if (absStepSize < Math.Abs(goal - progressBar1.Value))
              {
                exactValue += stepSize;
                progressBar1.Value = (int)Math.Round(exactValue);
                ///
                // *****更改处
                ///

                if (int.Parse(label1.Text) > (int)Math.Round(exactValue)+100|| int.Parse(label1.Text)< (int)Math.Round(exactValue) - 100)
                {
                  label1.Text = Math.Round(exactValue).ToString();
                  Application.DoEvents();
                }
              }
              else
              {
                progressBar1.Value = goal;
              }
            }
            Thread.Sleep(tempFrameDelay);
          } while ((progressBar1.Value != goal));
        }
      }
  }
  private void button2_Click(object sender, EventArgs e)
  {
    Stop();
  }
private void button3_Click(object sender, EventArgs e)
{
  CaptureDevicesCollection audioDevices = new CaptureDevicesCollection();
  deviceNames = new AutoCompleteStringCollection();
  for (int i = 0; i < audioDevices.Count; i++)
  {
    deviceNames.Add(audioDevices[i].Description);

    comboBox1.Items.Add(deviceNames[i].ToString());
    comboBox1.SelectedIndex = 0;
  }
  if (deviceNames.Count < 0)
  {
    comboBox1.Items.Clear();
    comboBox1.Items.Add(deviceName);
  }
}
private void button1_Click(object sender, EventArgs e)
{
  deviceIndex = comboBox1.SelectedIndex;
  Start();
}
private void button4_Click(object sender, EventArgs e)
{
  this.Close();
}
private void Form3_Load(object sender, EventArgs e)
{ }
  }
}

最新文章

  1. (转)linux下cp目录时排除一个或者多个目录的实现方法
  2. How to change drive in cygwin
  3. WebSocket 服务器3
  4. SQLServer错误:过程 sp_addextendedproperty,第 xxx 行对象无效。&#39;dbo.xxx.xxx&#39; 不允许有扩展属性,或对象不存在。
  5. hdu-acm steps Max sum
  6. [团队项目2.0]软件改进分析MathAPP
  7. java复制File文件操作
  8. freemarker使用map
  9. gridView行号的显示
  10. 2014 Multi-University Training Contest 1 - J Rating
  11. python 每日一练: 读取log文件中的数据,并画图表
  12. iptabes的用法
  13. 3.Properties文件的加载和使用
  14. C语言多维数组的指针传递
  15. 2019-04-10 集成JasperReport
  16. app:利用HBuilder打包webpack项目
  17. !!!常用CSS代码块
  18. 项目复审——Alpha阶段
  19. MSCRM2016 取消邮箱强制SSL
  20. [转]GCC系列: __attribute__((visibility(&quot;&quot;)))

热门文章

  1. Luogu P2754 星际转移问题
  2. python使用笔记006-函数+json操作
  3. 如何统计自动化测试用例的ROI 【投入产出比/投资回报率】
  4. C语言:\t\b\n应用
  5. Linux从入门到进阶全集——【第十四集:Shell编程-export命令】
  6. Docker的学习体验
  7. Mybatis order by 动态传参出现的一个小bug
  8. 如何用C++自己实现mysql数据库的连接池?
  9. Adaptive AUTOSAR 学习笔记 10 - 执行管理
  10. python中进程详解