Webcam: Buffer some frames to avoid garbage accumulation.
This commit is contained in:
@@ -8,7 +8,7 @@ namespace VAR.Toolbox.Code.DirectShow
|
||||
/// individual media samples as they move through the filter graph.
|
||||
/// </summary>
|
||||
///
|
||||
[ComImport,
|
||||
[ComImport,
|
||||
Guid("6B652FFF-11FE-4FCE-92AD-0266B5D7C78F"),
|
||||
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal interface ISampleGrabber
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace VAR.Toolbox.Code.DirectShow
|
||||
/// The interface provides callback methods for the <see cref="ISampleGrabber.SetCallback"/> method.
|
||||
/// </summary>
|
||||
///
|
||||
[ComImport,
|
||||
[ComImport,
|
||||
Guid("0579154A-2B53-4994-B0D0-E773148EFF85"),
|
||||
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal interface ISampleGrabberCB
|
||||
|
||||
@@ -252,12 +252,6 @@ namespace VAR.Toolbox.Code
|
||||
|
||||
public event NewFrameEventHandler NewFrame;
|
||||
|
||||
private void OnNewFrame(Bitmap frame)
|
||||
{
|
||||
if (NewFrame != null)
|
||||
NewFrame(this, frame);
|
||||
}
|
||||
|
||||
#endregion NewFrameEvent
|
||||
|
||||
#region Grabber
|
||||
@@ -266,9 +260,26 @@ namespace VAR.Toolbox.Code
|
||||
{
|
||||
private Webcam _parent;
|
||||
|
||||
private Bitmap[] _frames = null;
|
||||
private int _numFrames = 10;
|
||||
private int _currentFrameIndex = -1;
|
||||
|
||||
public Grabber(Webcam parent)
|
||||
{
|
||||
_parent = parent;
|
||||
_frames = new Bitmap[_numFrames];
|
||||
}
|
||||
|
||||
private Bitmap GetNextFrame()
|
||||
{
|
||||
_currentFrameIndex = (_currentFrameIndex + 1) % _numFrames;
|
||||
Bitmap currentBitmap = _frames[_currentFrameIndex];
|
||||
if (currentBitmap == null || currentBitmap?.Width != _parent.width || currentBitmap?.Height != _parent.height)
|
||||
{
|
||||
currentBitmap = new Bitmap(_parent.width, _parent.height, PixelFormat.Format24bppRgb);
|
||||
_frames[_currentFrameIndex] = currentBitmap;
|
||||
}
|
||||
return currentBitmap;
|
||||
}
|
||||
|
||||
public int SampleCB(double sampleTime, IntPtr sample)
|
||||
@@ -281,7 +292,7 @@ namespace VAR.Toolbox.Code
|
||||
if (_parent.NewFrame != null)
|
||||
{
|
||||
// create new image
|
||||
Bitmap _image = new Bitmap(_parent.width, _parent.height, PixelFormat.Format24bppRgb);
|
||||
Bitmap _image = GetNextFrame();
|
||||
Rectangle _imageRect = new Rectangle(0, 0, _parent.width, _parent.height);
|
||||
|
||||
// lock bitmap data
|
||||
@@ -311,7 +322,7 @@ namespace VAR.Toolbox.Code
|
||||
_image.UnlockBits(imageData);
|
||||
|
||||
// notify parent
|
||||
_parent.OnNewFrame(_image);
|
||||
_parent.NewFrame?.Invoke(this, _image);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user