?? camtooldlg.cpp
字號:
// Resize the video preview window to match owner window size
if (g_pVW)
g_pVW->SetWindowPosition(0, 0, rc.right, rc.bottom);
}
HRESULT CCamToolDlg::ChangePreviewState(int nShow)
{
HRESULT hr;
// If the media control interface isn't ready, don't call it
if (!g_pMC)
return S_OK;
if (nShow)
{
if (g_psCurrent != Running)
{
// Start previewing video data
hr = g_pMC->Run();
g_psCurrent = Running;
}
}
else
{
// Stop previewing video data
hr = g_pMC->StopWhenReady();
g_psCurrent = Stopped;
}
return hr;
}
HRESULT CCamToolDlg::SetupVideoWindow()
{
HRESULT hr;
// Set the video window to be a child of the main window
hr = g_pVW->put_Owner((long)PreviewHwnd);
if (FAILED(hr))
return hr;
// Set video window style
hr = g_pVW->put_WindowStyle(WS_CHILD | WS_CLIPCHILDREN);
if (FAILED(hr))
return hr;
// Use helper function to position video window in client rect
// of main application window
ResizeVideoWindow();
// Make the video window visible, now that it is properly positioned
hr = g_pVW->put_Visible(OATRUE);
if (FAILED(hr))
return hr;
return hr;
}
HRESULT CCamToolDlg::FindCaptureDevice(IBaseFilter ** ppSrcFilter)
{
HRESULT hr;
IBaseFilter * pSrc = NULL;
CComPtr <IMoniker> pMoniker =NULL;
ULONG cFetched;
// Create the system device enumerator
CComPtr <ICreateDevEnum> pDevEnum =NULL;
hr = CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
IID_ICreateDevEnum, (void ** ) &pDevEnum);
if (FAILED(hr))
{
Msg(TEXT("Couldn't create system enumerator! hr=0x%x"), hr);
return hr;
}
// Create an enumerator for the video capture devices
CComPtr <IEnumMoniker> pClassEnum = NULL;
hr = pDevEnum->CreateClassEnumerator (CLSID_VideoInputDeviceCategory, &pClassEnum, 0);
if (FAILED(hr))
{
Msg(TEXT("Couldn't create class enumerator! hr=0x%x"), hr);
return hr;
}
// If there are no enumerators for the requested type, then
// CreateClassEnumerator will succeed, but pClassEnum will be NULL.
if (pClassEnum == NULL)
{
::MessageBox(PreviewHwnd,TEXT("No video capture device was detected.\r\n\r\n")
TEXT("This sample requires a video capture device, such as a USB WebCam,\r\n")
TEXT("to be installed and working properly. The sample will now close."),
TEXT("No Video Capture Hardware"), MB_OK | MB_ICONINFORMATION);
return E_FAIL;
}
// Use the first video capture device on the device list.
// Note that if the Next() call succeeds but there are no monikers,
// it will return S_FALSE (which is not a failure). Therefore, we
// check that the return code is S_OK instead of using SUCCEEDED() macro.
if (S_OK == (pClassEnum->Next (1, &pMoniker, &cFetched)))
{
// Bind Moniker to a filter object
hr = pMoniker->BindToObject(0,0,IID_IBaseFilter, (void**)&pSrc);
if (FAILED(hr))
{
Msg(TEXT("Couldn't bind moniker to filter object! hr=0x%x"), hr);
return hr;
}
}
else
{
Msg(TEXT("Unable to access video capture device!"));
return E_FAIL;
}
// Copy the found filter pointer to the output parameter.
// Do NOT Release() the reference, since it will still be used
// by the calling function.
*ppSrcFilter = pSrc;
return hr;
}
HRESULT CCamToolDlg::GetInterfaces()
{
HRESULT hr;
// Create the filter graph
hr = CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC,
IID_IGraphBuilder, (void **) &g_pGraph);
if (FAILED(hr))
return hr;
// Create the capture graph builder
hr = CoCreateInstance (CLSID_CaptureGraphBuilder2 , NULL, CLSCTX_INPROC,
IID_ICaptureGraphBuilder2, (void **) &g_pCapture);
if (FAILED(hr))
return hr;
// Obtain interfaces for media control and Video Window
hr = g_pGraph->QueryInterface(IID_IMediaControl,(LPVOID *) &g_pMC);
if (FAILED(hr))
return hr;
hr = g_pGraph->QueryInterface(IID_IVideoWindow, (LPVOID *) &g_pVW);
if (FAILED(hr))
return hr;
hr = g_pGraph->QueryInterface(IID_IMediaEvent, (LPVOID *) &g_pME);
if (FAILED(hr))
return hr;
// Set the window handle used to process graph events
hr = g_pME->SetNotifyWindow((OAHWND)/*PreviewHwnd*/AppHwnd, WM_GRAPHNOTIFY, 0);
return hr;
}
HRESULT CCamToolDlg::HandleGraphEvent()
{
LONG evCode, evParam1, evParam2;
HRESULT hr=S_OK;
while(SUCCEEDED(g_pME->GetEvent(&evCode, &evParam1, &evParam2, 0)))
{
//
// Free event parameters to prevent memory leaks associated with
// event parameter data. While this application is not interested
// in the received events, applications should always process them.
//
// EC_ACTIVATE A video window is being activated or deactivated.
// EC_BUFFERING_DATA The graph is buffering data, or has stopped buffering data.
// EC_CLOCK_CHANGED The reference clock has changed.
// EC_COMPLETE All data from a particular stream has been rendered.
// EC_DEVICE_LOST A Plug and Play device was removed or has become available again.
// EC_DISPLAY_CHANGED The display mode has changed.
// EC_END_OF_SEGMENT The end of a segment has been reached.
// EC_ERROR_STILLPLAYING An asynchronous command to run the graph has failed.
// EC_ERRORABORT An operation was aborted because of an error.
// EC_FULLSCREEN_LOST The video renderer is switching out of full-screen mode.
// EC_GRAPH_CHANGED The filter graph has changed.
// EC_NEED_RESTART A filter is requesting that the graph be restarted.
// EC_NOTIFY_WINDOW Notifies a filter of the video renderer's window.
// EC_OLE_EVENT A filter is passing a text string to the application.
// EC_OPENING_FILE The graph is opening a file, or has finished opening a file.
// EC_PALETTE_CHANGED The video palette has changed.
// EC_PAUSED A pause request has completed.
// EC_QUALITY_CHANGE The graph is dropping samples, for quality control.
// EC_REPAINT A video renderer requires a repaint.
// EC_SEGMENT_STARTED A new segment has started.
// EC_SHUTTING_DOWN The filter graph is shutting down, prior to being destroyed.
// EC_SNDDEV_IN_ERROR An audio device error has occurred on an input pin.
// EC_SNDDEV_OUT_ERROR An audio device error has occurred on an output pin.
// EC_STARVATION A filter is not receiving enough data.
// EC_STEP_COMPLETE A filter performing frame stepping has stepped the specified number of frames.
// EC_STREAM_CONTROL_STARTED A stream-control start command has taken effect.
// EC_STREAM_CONTROL_STOPPED A stream-control start command has taken effect.
// EC_STREAM_ERROR_STILLPLAYING An error has occurred in a stream. The stream is still playing.
// EC_STREAM_ERROR_STOPPED A stream has stopped because of an error.
// EC_USERABORT The user has terminated playback.
// EC_VIDEO_SIZE_CHANGED The native video size has changed.
// EC_WINDOW_DESTROYED The video renderer was destroyed or removed from the graph.
hr = g_pME->FreeEventParams(evCode, evParam1, evParam2);
// Insert event processing code here, if desired
if (evCode == EC_DEVICE_LOST)
{
// Check if we have lost a capture filter being used.
// lParam2 of EC_DEVICE_LOST event == 1 indicates device added
// == 0 indicates device removed
if (evParam2 == 0)
{
m_connect->EnableWindow(FALSE);
EnableAllUI(FALSE);
m_exit->EnableWindow(TRUE);
Error( TEXT("Device Removed or No Connect!!!\r\n\r\n")
TEXT("Please connect the camera to PC, and execute\r\n")
TEXT("the CamTool again.") );
}
}
}
return hr;
}
HRESULT CCamToolDlg::CaptureVideo()
{
HRESULT hr;
IBaseFilter *pSrcFilter=NULL;
// Get DirectShow interfaces
hr = GetInterfaces();
if (FAILED(hr))
{
Msg(TEXT("Failed to get video interfaces! hr=0x%x"), hr);
return hr;
}
// Attach the filter graph to the capture graph
hr = g_pCapture->SetFiltergraph(g_pGraph);
if (FAILED(hr))
{
Msg(TEXT("Failed to set capture filter graph! hr=0x%x"), hr);
return hr;
}
// Use the system device enumerator and class enumerator to find
// a video capture/preview device, such as a desktop USB video camera.
hr = FindCaptureDevice(&pSrcFilter);
if (FAILED(hr))
{
// Don't display a message because FindCaptureDevice will handle it
return hr;
}
// Add Capture filter to our graph.
hr = g_pGraph->AddFilter(pSrcFilter, L"Video Capture");
if (FAILED(hr))
{
Msg(TEXT("Couldn't add capture filter to graph! hr=0x%x"), hr);
pSrcFilter->Release();
return hr;
}
// Render the preview pin on the video capture filter
// Use this instead of g_pGraph->RenderFile
hr = g_pCapture->RenderStream (&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,
pSrcFilter, NULL, NULL);
if (FAILED(hr))
{
Msg(TEXT("Couldn't render capture stream. "
"The device may already be in use.\r\n\r\nhr=0x%x"), hr);
pSrcFilter->Release();
return hr;
}
// Now that the filter has been added to the graph and we have
// rendered its stream, we can release this reference to the filter.
pSrcFilter->Release();
// Set video window style and position
hr = SetupVideoWindow();
if (FAILED(hr))
{
Msg(TEXT("Couldn't initialize video window! hr=0x%x"), hr);
return hr;
}
// Start previewing video data
hr = g_pMC->Run();
if (FAILED(hr))
{
Msg(TEXT("Couldn't run the graph! hr=0x%x"), hr);
return hr;
}
// Remember current state
g_psCurrent = Running;
return S_OK;
}
void CCamToolDlg::CloseGraphInterface()
{
// Stop previewing data
if (g_pMC)
g_pMC->StopWhenReady();
g_psCurrent = Stopped;
// Stop receiving events
if (g_pME)
g_pME->SetNotifyWindow(NULL, WM_GRAPHNOTIFY, 0);
// Relinquish ownership (IMPORTANT!) of the video window.
// Failing to call put_Owner can lead to assert failures within
// the video renderer, as it still assumes that it has a valid
// parent window.
if(g_pVW)
{
g_pVW->put_Visible(OAFALSE);
g_pVW->put_Owner(NULL);
}
// Release DirectShow interfaces
SAFE_RELEASE(g_pMC);
SAFE_RELEASE(g_pME);
SAFE_RELEASE(g_pVW);
SAFE_RELEASE(g_pGraph);
SAFE_RELEASE(g_pCapture);
}
//Load JPEG file by file name, and transfer YUV into RGB
void CCamToolDlg::LoadJPG(CString fileName)
{
if (m_buf!=NULL) {
delete [] m_buf;
m_buf=NULL;
}
// read to buffer tmp
m_buf = JpegFile::JpegFileToRGB(fileName, &m_width, &m_height);
//////////////////////
// set up for display
// do this before DWORD-alignment!!!
// this works on packed (not DWORD-aligned) buffers
// swap red and blue for display
JpegFile::BGRFromRGB(m_buf, m_width, m_height);
// vertical flip for display
JpegFile::VertFlipBuf(m_buf, m_width * 3, m_height);
}
void CCamToolDlg::DrawPics(unsigned char redraw)//, unsigned char action)
{
if (m_buf==NULL) return ;
CWnd *pWnd = GetDlgItem(IDC_DISPLAY);
CDC *theDC = pWnd->GetDC();
UINT col, row;
UINT x,y;
unsigned short DivX, DivY;
if (theDC!=NULL) {
CRect clientRect;
pWnd->GetClientRect(&clientRect);
if(redraw) {
pWnd->Invalidate();
pWnd->UpdateWindow();
}
// Center It
int left = clientRect.left;
int top = clientRect.top;
DivX = m_width / 320;
DivY = m_height / 240;
x = 320;//m_width/REF_DIV;
y = 240;//m_height/REF_DIV;
for (row = 0; row < y; row++) {
for (col = 0; col < x; col++) {
*(bmp+ (row * x + col)*3) = *(m_buf + (row* m_width * DivY + col*DivX)*3);
*(bmp+ (row * x + col)*3 + 1) = *(m_buf + (row* m_width * DivY + col*DivX)*3 +1);
*(bmp+ (row * x + col)*3 + 2) = *(m_buf + (row* m_width * DivY + col*DivX)*3 + 2);
}
}
tmp= JpegFile::MakeDwordAlignedBuf(bmp,
x,
y,
&m_widthDW);
// set up a DIB
BITMAPINFOHEADER bmiHeader;
bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmiHeader.biWidth = x;
bmiHeader.biHeight = y;
bmiHeader.biPlanes = 1;
bmiHeader.biBitCount = 24;
bmiHeader.biCompression = BI_RGB;
bmiHeader.biSizeImage = x*y*3;
bmiHeader.biXPelsPerMeter = 0;
bmiHeader.biYPelsPerMeter = 0;
bmiHeader.biClrUsed = 0;
bmiHeader.biClrImportant = 0;
int lines = StretchDIBits(theDC->m_hDC,
left, top,
x,
y,
0,0,
x,
y,
tmp,
(LPBITMAPINFO)&bmiHeader,
DIB_RGB_COLORS,
SRCCOPY);
ReleaseDC(theDC);
}
}
BOOL CCamToolDlg::ReadDefaultFromIniFile()
{
int cFlength;
char drive[_MAX_DRIVE], szOurPath[_MAX_PATH];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
char szProfileName[_MAX_PATH];
CFile cf;
HINSTANCE hInstance;
hInstance=AfxGetInstanceHandle();
if( GetModuleFileName( hInstance, szOurPath, _MAX_PATH ) == 0 )
{
// SetWindowText( "GetModuelfile error!" );
return 1;
}
_splitpath(szOurPath, drive, dir, fname, ext);
_makepath(szProfileName, drive, dir, fname, "ini" ); // make profile filename
if( cf.Open( "KD3310Z.ini", CFile::modeRead ) ) {
cFlength = cf.GetLength();
cf.Close();
_makepath(szProfileName, drive, dir, "KD3310Z", "ini" ); // make profile filename
}else {
// SetWindowText( "file 'KD3310Z.ini' not find" );
return 1;
}
RRatio = GetPrivateProfileInt("Init Value","RRatio" , 913, szProfileName);
GRatio = GetPrivateProfileInt("Init Value","GRatio" , 985, szProfileName);
BRatio = GetPrivateProfileInt("Init Value","BRatio" , 807, szProfileName);
SharpnessValue = GetPrivateProfileInt("Init Value","Sharpness" , 2, szProfileName);
OBValue = GetPrivateProfileInt("Init Value","OB" , 0, szProfileName);
SaturaionValue = GetPrivateProfileInt("Init Value","Saturation" , 1, szProfileName);
// char str[80];
// sprintf(str,"%d,%d,%d,%d,%d,%d",RRatio,GRatio,BRatio,SharpnessValue,OBValue,SaturaionValue);
// SetWindowText(str);
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -