Drewski7280
Drewski7280

Reputation: 53

C++ Builder using 2 TVideoCaptureDevice objects at the same time

Working on an application in C++ Builder which brings in video from 2 different web cams. I use 2 TVideoCaptureDevice objects.

A button starts the capturing of the video. I can only get 1 web cam to work. Web Cam #2 activates (green light on) for a split second then stops capturing. Web Cam #1 continues to capture ok.

//The Code in the button's onClick event:

void __fastcall TForm2::StartButtonClick(TObject *Sender)
{
    
int DIndex1, DIndex2;
TCaptureDeviceManager *TheManager;
TCaptureDeviceList TheDeviceList;
    
TheManager = NULL;
TheManager = TCaptureDeviceManager::Current;
TheDeviceList = TCaptureDeviceManager::Current->GetDevicesByMediaType(TMediaType::Video);
    
// Create the Video Capture Devices
    
DIndex1 = CameraIndex[0];
CAM1VideoCamera = dynamic_cast<TVideoCaptureDevice*> (TheDeviceList->Items[DIndex1]);
if(NumbCameras > 1) {
    DIndex2 = CameraIndex[1];
    CAM2VideoCamera = dynamic_cast<TVideoCaptureDevice*> (TheDeviceList->Items[DIndex2]);
    }
    
if (CAM1VideoCamera != NULL) StartButton->Enabled=true;
    else StartButton->Enabled=false;
    
// Prepare the Camera Sample Threads
CAM1VideoCamera->OnSampleBufferReady = CAM1SampleBufferReady;
CAM1VideoCamera->Quality = TVideoCaptureQuality::PhotoQuality;
if(NumbCameras > 1) {
    CAM2VideoCamera->OnSampleBufferReady = CAM2SampleBufferReady;
    CAM2VideoCamera->Quality = TVideoCaptureQuality::PhotoQuality;
    }
    
// Now start capturing frames from the camera
CAM1VideoCamera->StartCapture();
if(NumbCameras > 1) CAM2VideoCamera->StartCapture();
return;
}    

The code for the Sample Buffer for CAM1

//---------------------------------------------------------------------------
void __fastcall TForm2::CAM1SampleBufferReady(TObject *Sender, const TMediaTime ATime)
{
TThread::Synchronize(TThread::CurrentThread, (TThreadMethod)&CAM1SampleBufferSync);
return;
}
    
void __fastcall TForm2::CAM1SampleBufferSync()
{
    TRectF ZoomRectangle;
    TPointF Center;
    int ZWidth, ZHeight;
    int CX, CY;
    int CTop, CBottom, CLeft, CRight;
    TRect SourceRect;
    TRectF src, trg;
    Graphics::TBitmap *TemporaryBitmap;
    int TestValue;
    
    try {
        CAM1VideoCamera->SampleBufferToBitmap(ZoomBitmap1, true);
    
        RawBitmap1->Assign(ZoomBitmap1);
        RawWidth = RawBitmap1->Width;
        RawHeight = RawBitmap1->Height;
        Camera1FullWidth = RawWidth;
        Camera1FullHeight = RawHeight;
    
        // This routine stretches the bitmap to the size of the screen.
        ZWidth = ZoomBitmap1->Width;
        ZHeight = ZoomBitmap1->Height;
    
        // Find the Center point of the bitmap
        CX = ZWidth / 2;
        CY = ZHeight / 2;
        ZWidth /= ZoomFactor1;
        ZHeight /= ZoomFactor1;
    
        // Now find the zoom rectangle co-ordinates
        // Must check if the panning is outside the Full size of the camera bitmap
    
        TestValue = CY - (ZHeight/2) + Camera1DY;   if(TestValue < 0 ) Camera1DY = -1*(CY-(ZHeight/2));
        CTop      = CY - (ZHeight/2) + Camera1DY;
        TestValue = CY + (ZHeight/2) + Camera1DY;   if(TestValue > Camera1FullHeight ) Camera1DY = Camera1FullHeight - (CY+(ZHeight/2));
        CBottom   = CY + (ZHeight/2) + Camera1DY;
        TestValue = CX - (ZWidth/2) + Camera1DX;    if(TestValue < 0 ) Camera1DX = -1*(CX-(ZWidth/2));
        CLeft     = CX - (ZWidth/2) + Camera1DX;
        TestValue = CX + (ZWidth/2) + Camera1DX;    if(TestValue > Camera1FullWidth )  Camera1DX = Camera1FullWidth - (CX+(ZWidth/2));
        CRight    = CX + (ZWidth/2) + Camera1DX;
    
        CalibrationForm->Camera1DY = Camera1DY;
        CalibrationForm->Camera1DX = Camera1DX;
    
        SourceRect.Left = CLeft;
        SourceRect.Right = CRight;
        SourceRect.Top = CTop;
        SourceRect.Bottom = CBottom;
    
        CutBitmap->Height = ZHeight;
        CutBitmap->Width = ZWidth;
        CutBitmap->Canvas->BeginScene();
        CutBitmap->CopyFromBitmap(ZoomBitmap1, SourceRect, 0,0);
        CutBitmap->Canvas->EndScene();
    
        src = TRectF(0,0,CutBitmap->Width, CutBitmap->Height);
        trg = TRectF(0,0,WImageWidth, WImageHeight);
    
        TemporaryBitmap = NULL;
        TemporaryBitmap = new Graphics::TBitmap;
        TemporaryBitmap->Width = WImageWidth;
        TemporaryBitmap->Height = WImageHeight;
    
        TemporaryBitmap->Canvas->BeginScene();
        TemporaryBitmap->Canvas->DrawBitmap(CutBitmap,src,trg,1);
        TemporaryBitmap->Canvas->EndScene();
    
        WImage->Bitmap->Assign(TemporaryBitmap);
        delete TemporaryBitmap;
        TemporaryBitmap = NULL;
        }
    
    catch (...) {
        }
    
    return;
    }

The sample buffer code for the 2nd web cam is almost identical in nature but has it's own parameters for zooming and offsets. So I won't post the code here for it.

Any ideas as to why the second web cam won't continue to stay on and capture video????

Upvotes: 0

Views: 145

Answers (0)

Related Questions