Bonjour à tous,
Je me suis deja servi de C++ sur différents projets. Cependant dans un projet je suis amené a dialoguer avec un periphérique (caméra infrarouge USB) via la librairie Com. Pour récupérer une image, je dois utiliser un evenement OnFrameAvailable, mais je ne parviens pas a récuperer cet evenement. J'ai tenter d'utiliser des Sink, mais rien n'y fait. Je suis assez novice avec la bibliothèque COM et les evenements.
J'arrive a dialoguer avec ma caméra, mais si je recupere une image sans l'evenement, il y a une chance sur 4 pour que se passe un Access Violation:
Je me suis inspiré des exemples fourni avec le SDK de la cam,cherché sur le net, mais rien n'y fait. Le problème vient il du fait que je suis avec une application console et pas une app MFC ?
Si qqun pouvait m'aider, voici mon code:
Merci
CameliaTests.cpp
et le .h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 #include "stdafx.h" #include <windows.h> #include "CameliaTests.h" #include <wrapper.h> #include <atlbase.h> #include <atlcom.h> #include <optitrack.h> #import "optitrack.tlb" #ifdef _DEBUG #define new DEBUG_NEW #endif #define CALL( x ) printf("\n%s: "#x"\n", ((x)==S_OK) ? "passed" : "FAILED"); // The one and only application object CWinApp theApp; using namespace std; /* Class to handle Events * */ class Tests: public CCameraEvents{ public: CComPtr<INPCameraCollection> collection; CComPtr<INPCamera> m_spCamera; HRESULT Init(); private: void __stdcall HandleFrameAvailable(INPCamera * pCamera); }; void __stdcall Tests::HandleFrameAvailable(INPCamera * pCamera) { printf("non"); } HRESULT Tests::Init(){ CoInitialize(NULL); // Create Collection collection.CoCreateInstance(CLSID_NPCameraCollection); // Initialize a list of camera collection->Enum(); // Get first camera collection->Item(0,&m_spCamera); // Register this as an event handler this->DispEventAdvise(m_spCamera); // Open the camera m_spCamera->Open(); m_spCamera->Start(); int i = 0; while(i < 200){ Sleep(10); i++; printf("_"); } this->DispEventUnadvise(m_spCamera); m_spCamera->Stop(); m_spCamera->Close(); m_spCamera.Release(); collection.Release(); CoUninitialize(); return S_OK; } int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed\n")); nRetCode = 1; } else { Tests * t = new Tests(); t->Init(); } return nRetCode; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 #pragma once #include <atlbase.h> #include <atlcom.h> #include "Optitrack.h" #include "resource.h" class CCameraEvents : public IDispEventImpl<0, CCameraEvents, &DIID__INPCameraEvents, &LIBID_OptiTrack, 1, 0> { public: CCameraEvents() { ; } ~CCameraEvents() { ; } public: // connection point notifications STDMETHOD_(void, OnFrameAvailableIdCamera)(INPCamera * pCamera, LONG Group, LONG Id) { HandleFrameAvailable(pCamera);HandleFrameAvailableIdCamera(pCamera, Group, Id); } STDMETHOD_(void, OnFrameAvailable)(INPCamera * pCamera) { HandleFrameAvailable(pCamera); } STDMETHOD_(void, FrameAvailable)(INPCamera * pCamera) { HandleFrameAvailable(pCamera); } STDMETHOD_(void, OnSwitchChange)(INPCamera * pCamera, LONG lNewSwitchState) { HandleFrameAvailable(pCamera);HandleSwitchChange(pCamera, lNewSwitchState); } virtual void __stdcall HandleFrameAvailable(INPCamera * pCamera) { printf("non"); } virtual void HandleSwitchChange(INPCamera * pCamera, LONG lNewSwitchState) { printf("non"); } virtual void HandleFrameAvailableIdCamera(INPCamera * pCamera, LONG Group, LONG Id) { printf("non");} public: BEGIN_SINK_MAP(CCameraEvents) SINK_ENTRY_EX(0, DIID__INPCameraEvents, 1, OnFrameAvailable) SINK_ENTRY_EX(0, DIID__INPCameraEvents, 2, OnSwitchChange) SINK_ENTRY_EX(0, DIID__INPCameraEvents, 3, OnFrameAvailableIdCamera) END_SINK_MAP() };
Partager