我是新手,得到了一份工作来构建软件来创建一个程序来录制来自 USB 相机的视频并将其保存到硬盘驱动器。如果我用-1替换fourcc方法并将文件扩展名使用.avi,我设法让对话框工作。正在创建文件,但不存在录制文件,文件大小仍为 0kb。不知道该怎么办。我的代码:
私人 Emgu.CV.Capture 捕获;
私人 bool captureInProgress;
public CameraCapture()
{
InitializeComponent();
}
//------------------------------------------------------------------------------//
//Process Frame() below is our user defined function in which we will create an EmguCv
//type image called ImageFrame. capture a frame from camera and allocate it to our
//ImageFrame. then show this image in ourEmguCV imageBox
//------------------------------------------------------------------------------//
VideoWriter writer;
private void ProcessFrame(object sender, EventArgs arg)
{
Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
CamImageBox.Image = ImageFrame;
writer = new VideoWriter(@"C:\Video.mpeg", CvInvoke.CV_FOURCC('L','M','P','2'), (int)Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS, (int)Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, (int)Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, true);
}
//btnStart_Click() function is the one that handles our "Start!" button' click
//event. it creates a new capture object if its not created already. e.g at first time
//starting. once the capture is created, it checks if the capture is still in progress,
//if so the
private void btnStart_Click(object sender, EventArgs e)
{
#region if capture is not created, create it now
if (capture == null)
{
try
{
capture = new Emgu.CV.Capture();
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
#endregion
if (capture != null)
{
if (captureInProgress)
{ //if camera is getting frames then stop the capture and set button Text
// "Start" for resuming capture
btnStart.Text = "Start!"; //
Application.Idle -= ProcessFrame;
}
else
{
//if camera is NOT getting frames then start the capture and set button
// Text to "Stop" for pausing capture
btnStart.Text = "Stop";
Application.Idle += ProcessFrame;
}
captureInProgress = !captureInProgress;
}
}
private void ReleaseData()
{
if (capture != null)
capture.Dispose();
}
我在使用消息框时发现了我的问题。原来 ProcessFrame 继续工作,所以我做了一些改变。这是我的新代码。私人 Emgu.CV.Capture 捕获;
私人 bool captureInProgress;
public CameraCapture()
{
InitializeComponent();
}
//------------------------------------------------------------------------------//
//Process Frame() below is our user defined function in which we will create an EmguCv
//type image called ImageFrame. capture a frame from camera and allocate it to our
//ImageFrame. then show this image in ourEmguCV imageBox
//------------------------------------------------------------------------------//
VideoWriter writer;
private void ProcessFrame(object sender, EventArgs arg)
{
Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
CamImageBox.Image = ImageFrame;
writer.WriteFrame(ImageFrame);
}
//btnStart_Click() function is the one that handles our "Start!" button' click
//event. it creates a new capture object if its not created already. e.g at first time
//starting. once the capture is created, it checks if the capture is still in progress,
//if so the
private void btnStart_Click(object sender, EventArgs e)
{
#region if capture is not created, create it now
if (capture == null)
{
try
{
capture = new Emgu.CV.Capture();
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
#endregion
if (capture != null)
{
if (captureInProgress)
{ //if camera is getting frames then stop the capture and set button Text
// "Start" for resuming capture
btnStart.Text = "Start!"; //
writer.Dispose();
writer = null;
Application.Idle -= ProcessFrame;
}
else
{
//if camera is NOT getting frames then start the capture and set button
// Text to "Stop" for pausing capture
btnStart.Text = "Stop";
writer = new VideoWriter(@"C:\Video.avi", -1, (int)Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS, 640,480, true);
Application.Idle += ProcessFrame;
}
captureInProgress = !captureInProgress;
}
}
private void ReleaseData()
{
if (capture != null)
{
writer.Dispose();
writer = null;
capture.Dispose();
}
}