我有一个小项目,我从网络摄像头捕获图像并解码 qr。
以下代码捕获图像并将其存储到本地文件,但仅当它不在 while 循环中时。system.threading 似乎使捕获的图像变黑。如果我不使用它(循环),它每秒会捕获太多图像。
那么有没有办法改变 aforge.video 帧率,这样我就可以每 x 秒捕捉一次图像而无需 while 循环?
public partial class WebForm1 : System.Web.UI.Page
{
public int FrameRate { get; set; }
private FilterInfoCollection VideoCaptureDevices;
private VideoCaptureDevice FinalVideo;
protected void Page_Load(object sender, EventArgs e)
{
inputDevices.Items.Clear();
VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
{
inputDevices.Items.Add(VideoCaptureDevice.Name);
}
inputDevices.SelectedIndex = 0;
}
public void Start_OnClick(object sender, EventArgs e)
{
FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[inputDevices.SelectedIndex].MonikerString);
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}
void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
int i = 0;
while (i < 10)
{
Bitmap video = (Bitmap)eventArgs.Frame.Clone();
video.Save("C:\\Users\\Wayneio\\Desktop\\image\\test" + i + ".jpg");
i++;
System.Threading.Thread.Sleep(5000);
}
}
public void Stop_OnClick(object sender, EventArgs e)
{
this.FinalVideo.Stop();
}
}
此外,在尝试通过 asp 按钮停止捕获时出现此错误:
Object reference not set to an instance of an object on this.FinalVideo.Stop();
试过这个无济于事: ((VideoCaptureDevice)FinalVideo).DesiredFrameRate = 10;