Skip to content

Commit ad5aee5

Browse files
authored
Update example_09-11.cpp
Comments up front, extensive changes in the back
1 parent 28c90b2 commit ad5aee5

File tree

1 file changed

+125
-1
lines changed

1 file changed

+125
-1
lines changed

example_09-11.cpp

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Example 9-11. An example header file for our custom View class
2+
//
13
class COpenCVTestView : public CWindowImpl<COpenCVTestView> {
24
public:
35
DECLARE_WND_CLASS(NULL)
@@ -49,4 +51,126 @@ class COpenCVTestView : public CWindowImpl<COpenCVTestView> {
4951
cv::VideoCapture m_cap;
5052
cv::Mat m_cv_img;
5153
RGBTRIPLE* m_bitmapBits;
52-
};
54+
};
55+
56+
LRESULT CMainFrame::OnFileOpen(
57+
WORD /*wNotifyCode*/,
58+
WORD /*wID*/,
59+
HWND /*hWndCtl*/,
60+
BOOL& /*bHandled*/
61+
) {
62+
WTL::CFileDialog dlg(TRUE);
63+
if (IDOK == dlg.DoModal(m_hWnd)) {
64+
m_view.OpenFile(dlg.m_szFileName);
65+
}
66+
return 0;
67+
}
68+
69+
bool COpenCVTestView::OpenFile(std::string file) {
70+
if( !m_cap.open( file ) ) return false;
71+
// If we opened the file, set up everything now:
72+
//
73+
m_cap.read( m_cv_img );
74+
// could create a DIBSection here, but let's just allocate memory for raw bits
75+
//
76+
m_bitmapBits = new RGBTRIPLE[m_cv_img.cols * m_cv_img.rows];
77+
_copyImage();
78+
SetTimer(0, 1000.0f / m_cap.get( cv::CAP_PROP_FPS ) );
79+
return true;
80+
}
81+
82+
void COpenCVTestView::_copyImage() {
83+
// Copy the image data into the bitmap
84+
//
85+
cv::Mat cv_header_to_qt_image(
86+
cv::Size(
87+
m_cv_img.cols,
88+
m_cv_img.rows
89+
),
90+
CV_8UC3,
91+
m_bitmapBits
92+
);
93+
cv::cvtColor( m_cv_img, cv_header_to_qt_image, cv::BGR2RGB );
94+
}
95+
96+
LRESULT COpenCVTestView::OnPaint(
97+
UINT
98+
/* uMsg
99+
*/,
100+
WPARAM /* wParam
101+
*/,
102+
LPARAM /* lParam
103+
*/,
104+
BOOL& /* bHandled */
105+
) {
106+
CPaintDC dc(m_hWnd);
107+
WTL::CRect rect;
108+
GetClientRect(&rect);
109+
if( m_cap.isOpened() ) {
110+
BITMAPINFO bmi = {0};
111+
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
112+
bmi.bmiHeader.biCompression = BI_RGB;
113+
bmi.bmiHeader.biWidth
114+
= m_cv_img.cols;
115+
// note that bitmaps default to bottom-up, use negative height to
116+
// represent top-down
117+
//
118+
bmi.bmiHeader.biHeight = m_cv_img.rows * -1;
119+
bmi.bmiHeader.biPlanes = 1;
120+
bmi.bmiHeader.biBitCount = 24;
121+
dc.StretchDIBits(
122+
0,
123+
rect.Width(),
124+
0,
125+
bmi.bmiHeader.biWidth,
126+
m_bitmapBits,
127+
&bmi,
128+
DIB_RGB_COLORS,
129+
SRCCOPY
130+
// 32 if you use RGBQUADs for the bits
131+
0,
132+
rect.Height(),
133+
0,
134+
abs(bmi.bmiHeader.biHeight),
135+
Working with Windows
136+
|
137+
245);
138+
} else {
139+
dc.FillRect(rect, COLOR_WINDOW);
140+
}
141+
return 0;
142+
}
143+
144+
RESULT COpenCVTestView::OnTimer(
145+
UINT
146+
/* uMsg
147+
*/,
148+
WPARAM /* wParam
149+
*/,
150+
LPARAM /* lParam
151+
*/,
152+
BOOL& /* bHandled */
153+
) {
154+
// Nothing to do if capture object is not open
155+
//
156+
if( !m_cap.isOpened() ) return 0;
157+
m_cap.read( m_cv_img );
158+
_copyImage();
159+
Invalidate();
160+
return 0;
161+
}
162+
163+
LRESULT COpenCVTestView::OnEraseBkgnd(
164+
UINT
165+
/* uMsg
166+
*/,
167+
WPARAM /* wParam
168+
*/,
169+
LPARAM /* lParam
170+
*/,
171+
BOOL& /* bHandled */
172+
) {
173+
// since we completely paint our window in the OnPaint handler, use
174+
// an empty background handler
175+
return 0;
176+
}

0 commit comments

Comments
 (0)