PDA

View Full Version : how can I use the Grabber objects in an array?



sony
March 6, 2004, 19:42:37
In my visual c++ project(Firststep),I’m trying to connect to two usb webcams. I add two public member to the class CFirstStepView:

DShowLib::Grabber* m_pGrabber1;
DShowLib::Grabber* m_pGrabber2;

And Create two new Grabber in the constructor of the CFirstStepView class:

m_pGrabber1 = new DShowLib::Grabber();
m_pGrabber2 = new DShowLib::Grabber();

in "OnInitialUpdate" method , where i trying to grab images from two webcams, I wrote:

.................................
.................................

// Try connecting webcam1

DShowLib::Grabber::tVidCapDevListPtrpVidCapDevList =m_pGrabber1->getAvailableVideoCaptureDevices();

m_pGrabber1->openDev( (*pVidCapDevList)[0] );// I'm sure there are two webcams , index=0 & index=1 are correct.
DShowLib::Grabber::tVidFmtListPtr pVideoFormats= m_pGrabber1->getAvailableVideoFormats();
m_pGrabber1->setVideoFormat( (*pVideoFormats)[1]);
m_pGrabber1->setHWND( GetSafeHwnd() );
m_pGrabber1->startLive();
......
m_pGrabber1->stopLive();
m_pGrabber1->closeDev();

// try connecting webcam2
m_pGrabber2->openDev( (*pVidCapDevList)[1] );
m_pGrabber2->setVideoFormat( (*pVideoFormats)[1]);
m_pGrabber2->setHWND( GetSafeHwnd() );
m_pGrabber2->startLive();

.................................
.................................

After compile and run it, program connecting to first webcam, start live and stop, but it couldn't connect to webcam2! (Without any message or crash). If i individuate available capture devices and video formats for m_pGrabber2 and use it, program crashed! For this problem, please guide me.

Also you told me we can use the Grabber objects in an array, but i don't know how?

Thanks


Sony

Stefan Geissler
March 8, 2004, 07:35:50
Hello Sony,

Just declare the Grabber objects in an array:


int i;
CString szImageName;
DshowLib::Grabber m_Grabber[2];

for( i = 0; i < 2; i++)
{
m_Grabber[i].openDev( pVidCapDevList)[i] );
if(m_Grabber[i].isDevValid() )
{
m_Grabber[i].startLive();
m_Grabber[i].snapImages(1,3000);
szImageName.Format(“Image%d.bmp”,i);
m_Grabber[i].getActiveMemBuffer()->save(szImageName);
m_Grabber[i].stopLive();
m_Grabber[i].closeDev();
}
}


The other problem could be a problem of the USB stack. You could find a solution here:
http://www.codeguru.com/forum/showthread.php?threadid=275026

sony
March 8, 2004, 10:17:36
Hello Stefan;
Thanks for all your help.

In your code:
..............
m_Grabber[i].openDev( pVidCapDevList)[i] );
..............

You used "pVidCapDevList[i]".Please, how can i get pVidCapDevList in an array and use it as pVidCapDevList[i]???

Thanks again,

Sony

Stefan Geissler
March 8, 2004, 10:23:40
Hello,

Sorry, this was an error. It should have been *pVidCapDevList)[0].

Also the pointer to the video format should have been querried previously:
DShowLib::Grabber::tVidFmtListPtr pVideoFormats= m_Grabber[i]->getAvailableVideoFormats();

sony
March 8, 2004, 13:50:44
Hello and thanks Stefan;
I partly confused! Please tell me, in your codes, where i must get video capture devices list (with "pVidCapDevList" command).

And if I use something like this:

DShowLib::Grabber::tVidCapDevListPtr pVidCapDevList=m_Grabber->getAvailableVideoCaptureDevices();

I must use which "m_Grabber" object? (Form two grabber m_Grabber[0]&[1] )

Code:
--------------------------------------------------------------------------------
1- int i;
2- CString szImageName;
3- DshowLib::Grabber m_Grabber[2];
4-
5- for( i = 0; i < 2; i++)
6- {
7- m_Grabber[i].openDev( pVidCapDevList)[i] );
8- if(m_Grabber[i].isDevValid() )
9- {
10- m_Grabber[i].startLive();
11- m_Grabber[i].snapImages(1,3000);
12- szImageName.Format(“Image%d.bmp”,i);
13- m_Grabber[i].getActiveMemBuffer()->save(szImageName);
14- m_Grabber[i].stopLive();
15- m_Grabber[i].closeDev();
16- }
17- }
--------------------------------------------------------------------------------

And :
I sent an e-mail request to you, would you send me a complete MDI sample code, that opens a child window for each connected Camera.( MS VC++ 6.0 )(My Case:7904). It’s maybe useful to realization my problem.

Best Regards

Sony

Stefan Geissler
March 8, 2004, 15:57:40
- int i;
2- CString szImageName;
3- DshowLib::Grabber m_Grabber[2];
pVidCapDevList = m_Grabber[0].GetAvailableVideoCaptureDevices;
4-
5- for( i = 0; i < 2; i++)
6- {
7- m_Grabber[i].openDev( pVidCapDevList)[i] );
8- if(m_Grabber[i].isDevValid() )
9- {
DShowLib::Grabber::tVidCapDevListPtr pVidCapDevList=m_Grabber[i].getAvailableVideoCaptureDevices();
10- m_Grabber[i].startLive();
11- m_Grabber[i].snapImages(1,3000);
12- szImageName.Format(“Image%d.bmp”,i);
13- m_Grabber[i].getActiveMemBuffer()->save(szImageName);
14- m_Grabber[i].stopLive();
15- m_Grabber[i].closeDev();
16- }
17- }

sony
March 8, 2004, 22:22:13
Dear Stefan;
First, Thank you for sending "MultipleCamera" project. But;
Your above suggestion code doesn’t work! With help of "MultiCamera" files, I can wrote a code that worked fine:

Code:
-----------------------------------------------------------
1-void CFirstStepView::OnInitialUpdate()
2-{
3- CView::OnInitialUpdate();
4-
5- DShowLib::Grabber grabber;
6- DShowLib::Grabber::tVidCapDevListPtr pVidCapDevList = grabber.getAvailableVideoCaptureDevices();
7- DShowLib::Grabber m_Grabber;
8- for (int i=0; i < pVidCapDevList->size(); ++i)
9- {
10- LPCTSTR lpszPathName=(*pVidCapDevList)[i].c_str();
11- CString m_CurrentVideoCaptureDevice = lpszPathName;
12- int i = m_CurrentVideoCaptureDevice.ReverseFind('\\')+1;
13- m_CurrentVideoCaptureDevice = m_CurrentVideoCaptureDevice.Mid(i);
14- m_Grabber.openDev( m_CurrentVideoCaptureDevice );
15- DShowLib::Grabber::tVidFmtListPtr pVidFmtList = m_Grabber.getAvailableVideoFormats();
16- if ( pVidFmtList!=0 )
17- {
18- int iSize = pVidFmtList->size();
19- if( iSize > 0)
20- {
21- m_Grabber.setVideoFormat( pVidFmtList->at(0) );
22- }
23- }
24- m_Grabber.startLive();
25- CString szImageName;
26- m_Grabber.snapImages(1,3000);
27- szImageName.Format("Image%d.bmp",i);
28- m_Grabber.getActiveMemBuffer()->save(szImageName);
29- m_Grabber.stopLive();
30- m_Grabber.closeDev();
31-
32- }
33-}
-----------------------------------------------------------

This code can switch between two webcams, but there is an error in line

28=>m_Grabber.getActiveMemBuffer()->save(szImageName);

Error message: ----------------------------------------------------------------
'save' : cannot convert parameter 1 from 'class CString' to 'const class std::basic_string<char,struct

std::char_traits<char>,cla
----------------------------------------------------------------

Why is it wrong?

And if you have time, would you learn me about previously code, and about why it doesn’t work?(i sent you an e-mail that

included my "FirstStep" project. in "void CFirstStepView::OnInitialUpdate()" part, there is your previously code that don't

work. if

You want, you can test it.

Best regards

Sony

Stefan Geissler
March 9, 2004, 07:06:13
Hello Sony,

Just write
m_Grabber.getActiveMemBuffer()->save(szImageName.getbuffer(szImageName.getlength)) );

sony
March 9, 2004, 09:07:33
Hello Stefan;
Thanks. My program now hasn’t any compile error message. But, when i run it, program couldn’t snap image and crashed. When program connect to webcam1 and try to "getActiveMemBuffer()", an error message accrued . During this error message, the window that show live image from webcam1, already work!

Error messagebox:
------------------------------------------------------------------------------------------------------
Activemovie window:FirstStep.exe-Application Error

The instruction at "0x10046e30" referenced memory at "0x00000004".The memory could not be "read".
-----------------------------------------------------------------------------------------------------

Would you help me about this?

Thanks

Sony

Stefan Geissler
March 9, 2004, 09:23:18
Sony,

You should compile the project as debug and find the position where the error occurs.

sony
March 9, 2004, 10:41:50
OK, Stefan;

With debugging it, an error occurs at "TIS_UDSHL05.DLL":

Error messagebox: --------------------------------------

unhandled exception in FirstStep.exe(TIS_UDSHL05.DLL):0x00000005:Access violation.

--------------------------------------------------------


That referenced to:

10046E30 mov ecx,dword ptr [eax+4]


What is it,how can i solve it Please?

Best regards;

Sony

sony
March 10, 2004, 11:58:53
Hello Stefan;
Thanks for all your help and your patience. With your help (and createing a membuffer as show in sample), it’s now working fine. I can switch between 2 webcams and grab images form them.

Best Regards
Sony