我有一个小项目,我从网络摄像头捕获图像并解码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;
答案 0 :(得分:-1)
设置像这样的framreate
FinalVideo.DesiredFrameRate = 10;
FinalVideo.Start();
跳过保存帧的另一个选项是使用并非总是如此的函数 如果你有一个全局计数器值myCounter
做一个模数计算,如下面的ix mycounter划分为10等于1
myCounter++
if (m(ycounter %% 10))==1) { //code to save your bitmap }