简体   繁体   English

使用库AForge无法通过网络摄像头拍照

[英]Failed to take photo with the webcam using the library AForge

I am making use of the AForge class library. 我正在使用AForge类库。

From this library I am using VideoSourcePlayer to take photos with the webcam. 从该库中,我正在使用VideoSourcePlayer与网络摄像头拍照。

My purpose is to create a function that allows the user to photograph images to establish them as company logo. 我的目的是创建一个允许用户拍摄图像以将其建立为公司徽标的功能。 Not only can you choose images from the computer, but you can also capture images from the outside through the camera, since you may only want to transfer a logo of a physical support (paper) to the program. 您不仅可以从计算机中选择图像,而且还可以通过相机从外部捕获图像,因为您可能只想将物理支持(纸)的徽标转移到程序中。

As commented earlier in SO, ( how to pause a video file played using videosourceplayer ), VideoSourcePlayer does not have a Pause method or any function that allows to freeze the image. 如SO前面所述( 如何暂停使用videosourceplayer播放的视频文件 ), VideoSourcePlayer没有Pause方法或任何可冻结图像的功能。

Yes, it is true that it has the GetCurrentFrame() method, but that only gets a Bitmap from the current frame that must be passed to a PictureBox . 是的,它确实具有GetCurrentFrame()方法,但是仅从必须传递到PictureBox的当前帧中获取一个Bitmap

But I want that when the user clicks the button Capture the image of the VideoSourcePlayer simulate being frozen, and when the user presses the Delete button because he did not like the photo, then the image stops being frozen and recovers its movement. 但我希望当用户单击按钮“ Capture ”时, VideoSourcePlayer模拟的图像被冻结,并且当用户由于不喜欢照片而按“ Delete按钮时,图像停止冻结并恢复其运动。

Logic is like pausing or playing a video. 逻辑就像暂停或播放视频一样。

Well, there's no method for it, so I decided to look for another way to get this, and ... 好吧,没有办法,所以我决定寻找另一种方法来实现这一目标,并且...

If a picture is taken, use a PictureBox that contains the last frame and that is displayed on the VideoSourcePlayer , but if it is deleted, then the PictureBox is removed and the VideoSourcePlayer is returned with video. 如果拍摄了照片,请使用包含最后一帧的PictureBox并显示在VideoSourcePlayer ,但是如果删除了该PictureBox ,则将删除PictureBox ,并返回Video的VideoSourcePlayer

 private readonly Bitmap EmptyBitmap;

 private void CaptureBtn_Click(object sender, EventArgs e)
 {
      Bitmap bitmap = this.VideoSource.GetCurrentVideoFrame();

      ShowTakedFrame(bitmap, false);
 }

 private void ShowTakedFrame(Bitmap Frame, bool remove)
 {
       var picture = new System.Windows.Forms.PictureBox();
           picture.Size = this.VideoSource.Size;
           picture.Location = this.VideoSource.Location;

           if (!remove)
           {
                this.VideoSource.Stop();
                picture.Image = Frame;
                this.Controls.Remove(VideoSource);
                this.Controls.Add(picture);
           }
           else
           {
                this.Controls.Remove(picture);
                this.Controls.Add(VideoSource);
                this.VideoSource.VideoSource = this.CaptureDevice;
                this.VideoSource.Start();
           }
 }

 private void DeleteBtn_Click(object sender, EventArgs e)
 {
      ShowTakedFrame(EmptyBitmap, true);
 }

My problem is that when capturing the photo, the image is a few seconds after the moment when you press the Capture button and when you delete the captured image, using the Delete button, the video of the VideoSourcePlayer is frozen. 我的问题是,在捕获照片时,图像是在您按下“ Capture按钮并在使用“ Delete按钮删除捕获的图像之后几秒钟后冻结了VideoSourcePlayer的视频。

Can someone help me with this? 有人可以帮我弄这个吗?

The problem is that when you remove the PictureBox and add the VideoSourcePlayer , it creates a new object, that is, one that does not have the configuration properties of the previous one. 问题在于,当您删除PictureBox并添加VideoSourcePlayer ,它将创建一个新对象,即该对象不具有上一个对象的配置属性。 My recommendation is that you create the capture in a different form. 我的建议是您以其他形式创建捕获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM