www.pudn.com > QRAPPuie.rar > CappreviewDlg.cpp
// CappreviewDlg.cpp : implementation file // #include "stdafx.h" #include "QRAPP2.h" #include "CappreviewDlg.h" #include#include "Objbase.h" #include "..\transfilter\transfilter.h" #include "QRAPP2Dlg.h" #include "grabber.h" #include "Infoparse.h" ///1212 //#include "winceKBhook.h" /// #include "pwinuser.h" #define CHK( x ) do{ if( FAILED( hr = ( x ))) { goto Cleanup; }} while( FALSE ); #define ERR( x ) do{ hr = x; goto Cleanup; } while( FALSE ); #define RGB565_WHITE 0xffff; #define MyTimerID 1000 #define IDENTIFY_MAXCOUNT 10 //#define STILLWIDTH 1600 //#define STILLHEIGHT 1200 #define RGB565_MASK_RED 0xf800 #define RGB565_MASK_GREEN 0x7e0 #define RGB565_MASK_BLUE 0x001f ///#define WIDE_PREVIEW #ifdef WIDE_PREVIEW #define CLIPRECT_SIZE 420 #define CLIPRECT_TOP 390 #define CLIPRECT_BOTTOM 809 #define CLIPRECT_LEFT 590 #define CLIPRECT_RIGHT 1009 #define MAX_STILLIMG_WIDTH 1600 #define MAX_STILLIMG_HEIGHT 1200 #else #define CLIPRECT_SIZE 420 #define CLIPRECT_TOP 590 #define CLIPRECT_BOTTOM 1009 #define CLIPRECT_LEFT 390 #define CLIPRECT_RIGHT 809 #define MAX_STILLIMG_WIDTH 1200 #define MAX_STILLIMG_HEIGHT 1600 #endif HANDLE g_hImgDataArrived; BYTE *g_pOutRectImageData = NULL;//hmpinfohead and mapdata included int g_nOutRectImageLen = 0; int g_nImgNumber = 0; CString sFileName; // CCappreviewDlg dialog BOOL bCameraOpened = FALSE; CComPtr m_pFilterGraph; CComPtr m_pCaptureGraphBuilder; CComPtr m_pVideoCaptureFilter; CComPtr m_pTransformFilter;/// CComPtr m_pSampleGrabFilter; CComPtr m_pMediaControl;/// IBaseFilter *m_pRenderP ; IBaseFilter *m_pRenderS ; IVideoWindow *m_pVWP; /////////////////////////////1212 DWORD g_CAMVK_CODE =0; HHOOK g_hKBHook = NULL; static LRESULT CALLBACK CameraKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam); //////////////////////////// #define USR_CAMERAUNWORK WM_USER+200 IMPLEMENT_DYNAMIC(CCappreviewDlg, CDialog) CCappreviewDlg::CCappreviewDlg(CWnd* pParent /*=NULL*/) : CDialog(CCappreviewDlg::IDD, pParent) , m_nIdentifyCount(0) { } CCappreviewDlg::~CCappreviewDlg() { } void CCappreviewDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_EDIT_TITLE, m_ctrlTitle); // DDX_Control(pDX, IDC_EDIT_IDENTIFYING, m_ctrlIdentiing); } BEGIN_MESSAGE_MAP(CCappreviewDlg, CDialog) ON_COMMAND(ID_32810, &CCappreviewDlg::On32810Identify) ON_COMMAND(ID_32812, &CCappreviewDlg::On32812Return) /// ON_COMMAND(ID_32813, &CCappreviewDlg::On32813CancelIdenti) ON_WM_PAINT() ON_WM_ACTIVATE() ON_MESSAGE(USR_CAMERAUNWORK,OnMyReceiveCameraUnwork)// END_MESSAGE_MAP() /////////////////////////////////////////////////////////////////////////////////////////////// // 按键钩子1212 LRESULT CALLBACK CameraKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { KBDLLHOOKSTRUCT* testpkb = (KBDLLHOOKSTRUCT*)lParam; if (HC_ACTION == nCode) { KBDLLHOOKSTRUCT* pkb = (KBDLLHOOKSTRUCT*)lParam; switch (wParam) { case WM_KEYDOWN: if (pkb->vkCode == g_CAMVK_CODE)//pkb->vkCode == VK_APP3 && pkb->scanCode == 3 return TRUE; case WM_KEYUP: if (pkb->vkCode == g_CAMVK_CODE) return TRUE; /* if (g_hWndusekb == GetForegroundWindow()) { SendMessage(g_hWndusekb, wParam, pkb->vkCode, lParam); return TRUE; } break;*/ default: break; } } return CallNextHookEx(g_hKBHook, nCode, wParam, lParam); } void WriteBMPToDisk(unsigned char *pStillImageBuffer) { long StillWidth = MAX_STILLIMG_WIDTH; long StillHeight = MAX_STILLIMG_HEIGHT; DWORD dwSize; HANDLE hFile; BITMAPINFOHEADER bitmapinfoheader; BITMAPFILEHEADER hdr; DWORD color_table[3]; long bitmapSize = 0; int still_size = StillWidth*StillHeight*2; // Fill in the fields of the file header hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM" hdr.bfSize = still_size + sizeof( hdr ) + sizeof(BITMAPINFOHEADER)+ sizeof(DWORD)*3; hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfOffBits = (DWORD) (sizeof( hdr )) + sizeof(BITMAPINFOHEADER) + sizeof(DWORD)*3; bitmapinfoheader.biSize = sizeof(BITMAPINFOHEADER); bitmapinfoheader.biWidth = StillWidth; bitmapinfoheader.biHeight = StillHeight; bitmapinfoheader.biPlanes = 1; bitmapinfoheader.biBitCount = 16; bitmapinfoheader.biCompression = BI_BITFIELDS; //BI_RGB; bitmapinfoheader.biSizeImage = 0; bitmapinfoheader.biClrUsed = 0; color_table[0] = 0xF800; color_table[1] = 0x07E0; color_table[2] = 0x001F; bitmapSize = still_size; //pStillImageBuffer = new unsigned char[bitmapSize]; const TCHAR *picture_path = TEXT("\\My Documents\\abcd1025.bmp") ; if (pStillImageBuffer) { hFile = CreateFile(picture_path, GENERIC_WRITE | GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile != NULL) { WriteFile(hFile, &hdr, sizeof(BITMAPFILEHEADER), &dwSize, NULL); WriteFile(hFile, &bitmapinfoheader, sizeof(BITMAPINFOHEADER), &dwSize, NULL); WriteFile(hFile, color_table, sizeof(DWORD)*3, &dwSize, NULL); WriteFile(hFile, pStillImageBuffer, bitmapSize, &dwSize, NULL); CloseHandle(hFile); //free(pStillImageBuffer); } else { RETAILMSG(1,(TEXT("CamTest:Cannot create file \r\n"))); } } else { RETAILMSG(1,(TEXT("CamTest:Null buffer \r\n"))); } } void WriteBMP888ToDisk(PBYTE pStillImageBuffer,int nImagedataLen) { long StillWidth = CLIPRECT_SIZE; long StillHeight = CLIPRECT_SIZE; DWORD dwSize; HANDLE hFile; BITMAPINFOHEADER bitmapinfoheader; BITMAPFILEHEADER hdr; long bitmapSize = 0; int still_size = nImagedataLen; // Fill in the fields of the file header hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM" hdr.bfSize = still_size + sizeof( hdr ) + sizeof(BITMAPINFOHEADER); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfOffBits = (DWORD) (sizeof( hdr )) + sizeof(BITMAPINFOHEADER); bitmapinfoheader.biSize = sizeof(BITMAPINFOHEADER); bitmapinfoheader.biWidth = StillWidth; bitmapinfoheader.biHeight = StillHeight; bitmapinfoheader.biPlanes = 1; bitmapinfoheader.biBitCount = 24; bitmapinfoheader.biCompression = BI_RGB; //BI_RGB; bitmapinfoheader.biSizeImage = 0; bitmapinfoheader.biClrUsed = 0; bitmapSize = still_size;// sFileName.Format(TEXT("\\My Documents\\img%d.bmp"),g_nImgNumber++); if (pStillImageBuffer) { hFile = CreateFile(sFileName, GENERIC_WRITE | GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile != NULL) { WriteFile(hFile, &hdr, sizeof(BITMAPFILEHEADER), &dwSize, NULL); //// WriteFile(hFile, &bitmapinfoheader, sizeof(BITMAPINFOHEADER), &dwSize, NULL); WriteFile(hFile, pStillImageBuffer, bitmapSize, &dwSize, NULL); CloseHandle(hFile); } else { RETAILMSG(1,(TEXT("CamTest:Cannot create file \r\n"))); } } else { RETAILMSG(1,(TEXT("CamTest:Null buffer \r\n"))); } } HRESULT GrabSampleCallback( IMediaSample * pSample, REFERENCE_TIME * StartTime, REFERENCE_TIME * StopTime,BOOL TypeChanged ) { CRITICAL_SECTION csWRITESEC; InitializeCriticalSection(&csWRITESEC); EnterCriticalSection(&csWRITESEC); unsigned char *pImage; HRESULT hr = S_OK; // NOTE: We cannot do anything with this sample until we call GetConnectedMediaType // on the filter to find out what format these samples are. //RETAILMSG(1, (TEXT("Callback with sample %lx for time %ld"), pSample, long( *StartTime / 10000 ) ) ); hr = pSample->GetPointer(&pImage); /// WriteBMPToDisk(pImage); /********************************1110****/ BITMAPINFOHEADER bitmapinfoheader; bitmapinfoheader.biSize = sizeof(BITMAPINFOHEADER); bitmapinfoheader.biWidth = CLIPRECT_SIZE; bitmapinfoheader.biHeight = CLIPRECT_SIZE; bitmapinfoheader.biPlanes = 1; bitmapinfoheader.biBitCount = 24; bitmapinfoheader.biCompression = BI_RGB; //BI_RGB; bitmapinfoheader.biSizeImage = 0; bitmapinfoheader.biClrUsed = 0; int nInfosize = sizeof(BITMAPINFOHEADER); //clip rect map from bmp565 to bmp888 PBYTE clipPixelTopLine = NULL; PBYTE clipPixelLine = NULL; PBYTE clipPixel = NULL; RECT clipRt; ///420*420 clipRt.top = CLIPRECT_TOP; clipRt.bottom = CLIPRECT_BOTTOM; clipRt.left = CLIPRECT_LEFT; clipRt.right = CLIPRECT_RIGHT; int cLipWidth = clipRt.right - clipRt.left+1; int cLipHeight = clipRt.bottom - clipRt.top+1; int outImageStride = (cLipWidth * 3 +3) & ~3; /// multiple of 4byte int m_nPixelBytes = 2; int m_nImageStride = (MAX_STILLIMG_WIDTH * 2 + 3) & ~3; /// multiple of 4byte int m_nImageHeight = MAX_STILLIMG_HEIGHT; int imageStride = -m_nImageStride; if (g_pOutRectImageData != NULL) { delete []g_pOutRectImageData;// g_pOutRectImageData = NULL; g_nOutRectImageLen = 0; } g_nOutRectImageLen = nInfosize + (outImageStride * cLipHeight); g_pOutRectImageData = new BYTE[g_nOutRectImageLen]; ZeroMemory(g_pOutRectImageData,g_nOutRectImageLen); memcpy(g_pOutRectImageData,&bitmapinfoheader,nInfosize); BYTE *tmp = NULL; PBYTE pIMageTopline = pImage + m_nImageStride * (m_nImageHeight - 1); BYTE *outImageTopLine = g_pOutRectImageData+nInfosize + (cLipHeight-1)*outImageStride; clipPixelTopLine = pIMageTopline + clipRt.top * imageStride + clipRt.left * m_nPixelBytes; for(int i=0; i< cLipHeight; i++) { clipPixelLine = clipPixelTopLine + i * imageStride; tmp = outImageTopLine + i * (-outImageStride); ///?? clipPixel = clipPixelLine; for(int j=0; j >11) * 0xFF / 0x1F; //乘以8 unsigned short green = (((*pixel)&RGB565_MASK_GREEN)>>5) * 0xFF / 0x3F;//乘以4 unsigned short blue = ((*pixel)&RGB565_MASK_BLUE) * 0xFF / 0x1F;*/ unsigned short pixel = 0; memcpy(&pixel,clipPixel,2); unsigned short red =(((pixel)&RGB565_MASK_RED)>>11) * 0xFF / 0x1F; //乘以8 unsigned short green = (((pixel)&RGB565_MASK_GREEN)>>5) * 0xFF / 0x3F;//乘以4 unsigned short blue = ((pixel)&RGB565_MASK_BLUE) * 0xFF / 0x1F; *tmp++ = (BYTE)blue; *tmp++ = (BYTE)green; *tmp++ = (BYTE)red; clipPixel+=2; } } /// WriteBMP888ToDisk(g_pOutRectImageData,g_nOutRectImageLen); LeaveCriticalSection(&csWRITESEC); DeleteCriticalSection(&csWRITESEC); SetEvent(g_hImgDataArrived); return S_OK; } HRESULT CCappreviewDlg::InitCapFilter() { HRESULT hr; IPersistPropertyBag *pPropertyBag = NULL; hr = CoCreateInstance(CLSID_VideoCapture, NULL, CLSCTX_INPROC_SERVER,IID_IBaseFilter, (void **)&m_pVideoCaptureFilter); if (FAILED(hr)) return hr; GUID clsid = { 0xCB998A05, 0x122C, 0x4166, 0x84, 0x6A, 0x93, 0x3E, 0x4D, 0x7E, 0x3C, 0x86 }; DEVMGR_DEVICE_INFORMATION pdi; HANDLE hand = FindFirstDevice(DeviceSearchByGuid,&clsid,&pdi); RETAILMSG(1, (TEXT("CamTest: Find device: %x %x\r\n"),hand,pdi.szDeviceName)); hr = m_pVideoCaptureFilter->QueryInterface(IID_IPersistPropertyBag, (void **)&pPropertyBag); if (FAILED(hr)) { m_pVideoCaptureFilter.Release(); m_pVideoCaptureFilter = NULL; return hr; } VARIANT varCamName; IPropertyBag *propBag = NULL; varCamName.byref = L"CAM1:" ; hr = pPropertyBag->Load(propBag,NULL); ///0104 if (FAILED(hr)) { pPropertyBag->Release(); m_pVideoCaptureFilter.Release(); m_pVideoCaptureFilter = NULL; return hr; } else pPropertyBag->Release(); return S_OK; } HRESULT CCappreviewDlg::ConnectFilters(IGraphBuilder *pGraph,IBaseFilter *pF1, int iPin1,IBaseFilter *pF2,int iPin2,IPin **ppPinout) { IPin *pPin1, *pPin2; IEnumPins *pEnum; unsigned long fetched; HRESULT hr; hr = pF1->EnumPins(&pEnum); while (iPin1>0) { hr = pEnum->Next(1,&pPin1,&fetched); //Skip Capture pin iPin1--; pPin1->Release();/// } hr = pEnum->Next(1,&pPin1,&fetched); pEnum->Release(); hr = pF2->EnumPins(&pEnum); while (iPin2>0) { hr = pEnum->Next(1,&pPin2,&fetched); //Skip Capture pin iPin2--; pPin2->Release();/// } hr = pEnum->Next(1,&pPin2,&fetched); pEnum->Release(); hr = pGraph->Connect(pPin1,pPin2); if (ppPinout) { *ppPinout = pPin1; (*ppPinout)->AddRef(); /// } pPin1->Release(); pPin2->Release(); if (!SUCCEEDED(hr)) RETAILMSG(1, (TEXT("CamTest: Fail to Connect Pin! %x\r\n"),hr)); return hr; } HRESULT CCappreviewDlg::SetFormat() { //camera flash on or off IAMCameraControl *pCamConfig = NULL; IAMStreamConfig *pStreamConfig = NULL; HRESULT hr = S_OK; DWORD dTrace = 0; CHK( m_pVideoCaptureFilter->QueryInterface(IID_IAMCameraControl, (void**)&pCamConfig)); long lMax = 0; long lMin = 0; long lSteppingDelta = 0; long lDefault = 0; long lCapflag = 0; m_lCameraFlashFlag = 0; m_lCameraPanFlag = 0; m_lCameraTiltFlag = 0; m_lCameraRollFlag = 0; m_lCameraZoomFlag = 0; m_lCameraExposureFlag = 0; m_lCameraIrisFlag = 0; m_lCameraFocusFlag = 0; CHK (pCamConfig->GetRange(CameraControl_Flash,&lMin,&lMax,&lSteppingDelta,&lDefault,&lCapflag)); CHK( pCamConfig->Get(CameraControl_Flash,&m_lCameraFlash ,&lCapflag)); CHK( pCamConfig->Set(CameraControl_Flash,lMax,CameraControl_Flags_Manual)); ///////////////0124 CHK (pCamConfig->GetRange(CameraControl_Pan ,&lMin,&lMax,&lSteppingDelta,&lDefault,&lCapflag)); CHK( pCamConfig->Get(CameraControl_Pan ,&m_lCameraPan ,&lCapflag)); CHK( pCamConfig->Set(CameraControl_Pan ,lDefault,CameraControl_Flags_Manual));//CameraControl_Flags_Auto CHK (pCamConfig->GetRange(CameraControl_Tilt ,&lMin,&lMax,&lSteppingDelta,&lDefault,&lCapflag)); CHK( pCamConfig->Get(CameraControl_Tilt ,&m_lCameraTilt ,&lCapflag)); CHK( pCamConfig->Set(CameraControl_Tilt ,lDefault,CameraControl_Flags_Manual));//CameraControl_Flags_Auto CHK (pCamConfig->GetRange(CameraControl_Roll ,&lMin,&lMax,&lSteppingDelta,&lDefault,&lCapflag)); CHK( pCamConfig->Get(CameraControl_Roll ,&m_lCameraRoll ,&lCapflag)); CHK( pCamConfig->Set(CameraControl_Roll ,lDefault,CameraControl_Flags_Manual)); CHK (pCamConfig->GetRange(CameraControl_Zoom,&lMin,&lMax,&lSteppingDelta,&lDefault,&lCapflag)); CHK( pCamConfig->Get(CameraControl_Zoom,&m_lCameraZoom ,&lCapflag)); CHK( pCamConfig->Set(CameraControl_Zoom,lDefault,CameraControl_Flags_Manual)); CHK (pCamConfig->GetRange(CameraControl_Exposure ,&lMin,&lMax,&lSteppingDelta,&lDefault,&lCapflag)); CHK( pCamConfig->Get(CameraControl_Exposure,&m_lCameraExposure ,&lCapflag)); CHK( pCamConfig->Set(CameraControl_Exposure,lDefault,CameraControl_Flags_Manual)); CHK (pCamConfig->GetRange(CameraControl_Iris ,&lMin,&lMax,&lSteppingDelta,&lDefault,&lCapflag)); CHK( pCamConfig->Get(CameraControl_Iris ,&m_lCameraIris ,&lCapflag)); CHK( pCamConfig->Set(CameraControl_Iris ,lDefault,CameraControl_Flags_Manual)); CHK (pCamConfig->GetRange(CameraControl_Focus ,&lMin,&lMax,&lSteppingDelta,&lDefault,&lCapflag)); CHK( pCamConfig->Get(CameraControl_Focus ,&m_lCameraFocus ,&lCapflag)); CHK( pCamConfig->Set(CameraControl_Focus ,lDefault,CameraControl_Flags_Manual)); /////////////// if (pCamConfig!=NULL) { pCamConfig->Release(); pCamConfig = NULL; } dTrace=1; // format ////IAMStreamConfig *pStreamConfig = NULL; CHK (m_pCaptureGraphBuilder->FindInterface( &PIN_CATEGORY_STILL, // Preview pin. PIN_CATEGORY_CAPTURE PIN_CATEGORY_PREVIEW &MEDIATYPE_Video, // Any media type--0 , &MEDIATYPE_Video. m_pVideoCaptureFilter, // Pointer to the capture filter. IID_IAMStreamConfig, (void**)&pStreamConfig)); dTrace=2; int iCount = 0, iSize = 0; CHK (pStreamConfig->GetNumberOfCapabilities(&iCount, &iSize));//get the number of media types dTrace=3; // Check the size to make sure we pass in the correct structure. if (iSize == sizeof(VIDEO_STREAM_CONFIG_CAPS)) { // Use the video capabilities structure. for (int iFormat = 0; iFormat < iCount-1; iFormat++) //iCount { VIDEO_STREAM_CONFIG_CAPS scc; AM_MEDIA_TYPE *pmtConfig = NULL; CHK (pStreamConfig->GetStreamCaps(iFormat, &pmtConfig, (BYTE*)&scc));// GetStreamCaps method returns a media type and the corresponding capability structure: dTrace=4; if ((pmtConfig->formattype == FORMAT_VideoInfo) && (pmtConfig->subtype == MEDIASUBTYPE_RGB565)) { long lToSetWidth = MAX_STILLIMG_WIDTH; long lToSetHeight = MAX_STILLIMG_HEIGHT; /// unsigned short iColorBit = 16; RECT rcSrc; rcSrc.left = 0; rcSrc.right = 0; rcSrc.bottom = 0; rcSrc.top = 0; //use of VIDEO_STREAM_CONFIG_CAPS VIDEOINFOHEADER===> Source and Target Rectangles in Video Renderers VIDEOINFOHEADER* pVih; pVih = (VIDEOINFOHEADER*)pmtConfig->pbFormat; pVih->bmiHeader.biWidth = lToSetWidth; pVih->bmiHeader.biHeight = lToSetHeight; pVih->bmiHeader.biSizeImage = DIBSIZE(pVih->bmiHeader);//Set the sample size and image size.?? pVih->rcSource = rcSrc; pVih->rcTarget = rcSrc;//no cropping, use full video input buffer?? pmtConfig->lSampleSize = pVih->bmiHeader.biSizeImage;/// CHK ( pStreamConfig->SetFormat(pmtConfig)); dTrace=5; if (FAILED(hr)) AfxMessageBox(TEXT("SetFormat:SetFormat ERROR")); break; } // Delete the media type when you are done. DeleteMediaType(pmtConfig); } } if (pStreamConfig!=NULL) { pStreamConfig->Release(); pStreamConfig = NULL; } Cleanup: if (FAILED(hr)) { if (pCamConfig!=NULL) { pCamConfig->Release(); pCamConfig = NULL; } if (pStreamConfig!=NULL) { pStreamConfig->Release(); pStreamConfig = NULL; } // CString sMsg(TEXT("")); // sMsg.Format(TEXT("设置分辨率出错! %d"),dTrace); // AfxMessageBox(sMsg,0,0); return hr; } return S_OK; } void CCappreviewDlg::SetupVideoWindow(IVideoWindow *pVW) { HRESULT hr; RECT rt; if (pVW==NULL) return; HWND hStaticWnd = GetDlgItem(IDC_STATIC_CAPWND)->GetSafeHwnd(); GetDlgItem(IDC_STATIC_CAPWND)->GetClientRect(&rt); hr = pVW->put_Owner((OAHWND)hStaticWnd); hr = pVW->put_WindowStyle(WS_CHILD | WS_CLIPCHILDREN);//WS_CLIPCHILDREN | WS_CLIPSIBLINGS hr = pVW->SetWindowPosition(rt.left,rt.top,rt.right-rt.left,rt.bottom-rt.top); hr = pVW->put_Visible(OATRUE); return; } BOOL CCappreviewDlg::RunCapture(void) { if (bCameraOpened) return TRUE; HRESULT hr; CHK( CoInitialize(0)); CHK( CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&m_pFilterGraph)); CHK( CoCreateInstance(CLSID_CaptureGraphBuilder, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void **)&m_pCaptureGraphBuilder)); CHK( m_pCaptureGraphBuilder->SetFiltergraph(m_pFilterGraph)); CHK( m_pSampleGrabFilter.CoCreateInstance(CLSID_GrabberSample));//CLSID_CSampleGrabber IGrabberSample *pGrabber = NULL; CHK( m_pSampleGrabFilter->QueryInterface(IID_IGrabberSample, (void**)&pGrabber)); CHK( pGrabber->SetCallback(&GrabSampleCallback)); if (pGrabber!=NULL) { pGrabber->Release(); pGrabber = NULL; } CHK( InitCapFilter()); CHK( CoCreateInstance(CLSID_VideoRenderer, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&m_pRenderP)); CHK( CoCreateInstance(CLSID_VideoRenderer, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&m_pRenderS)); CHK( m_pFilterGraph->AddFilter(m_pVideoCaptureFilter,L"Video Capture Source")); CHK( m_pFilterGraph->AddFilter(m_pRenderP,L"Video Render1")); CHK( m_pFilterGraph->AddFilter(m_pRenderS,L"Video Render2")); CHK( m_pFilterGraph->AddFilter(m_pSampleGrabFilter,L"Grabber")); CHK( m_pTransformFilter.CoCreateInstance(CLSID_MyVideoMosaic));///CLSID_MyVideoMosaic CHK( m_pFilterGraph->AddFilter(m_pTransformFilter,L"Video Mosaic"));/// CHK (SetFormat()); /// hr = m_pCaptureGraphBuilder->RenderStream( &PIN_CATEGORY_STILL, &MEDIATYPE_Video, m_pVideoCaptureFilter, NULL, pImageSinkFilter ); /// hr = pImageSinkFilter.QueryInterface( &m_pImageSinkFilter ); CHK (ConnectFilters(m_pFilterGraph,m_pVideoCaptureFilter,1,m_pSampleGrabFilter,0,NULL)); // 1 is still pin NULL ? &pPin 0104 CHK (ConnectFilters(m_pFilterGraph,m_pSampleGrabFilter,1,m_pRenderS,0,NULL)); // 1 is grab's output pin 0104 /// hr = m_pFilterGraph->Render(pPin); ///IPin *pStillPin = NULL; ///hr = m_pCaptureGraphBuilder->FindPin(m_pVideoCaptureFilter, PINDIR_OUTPUT, &PIN_CATEGORY_PREVIEW, 0,FALSE, 0, &pPin); CHK (m_pCaptureGraphBuilder->RenderStream(&PIN_CATEGORY_CAPTURE,&MEDIATYPE_Video,m_pVideoCaptureFilter, m_pTransformFilter,m_pRenderP)); //m_pTransformFilter m_pRenderP CHK (m_pRenderP->QueryInterface(IID_IVideoWindow, (void**)&m_pVWP));// 0104 SetupVideoWindow(m_pVWP); CHK( m_pFilterGraph->QueryInterface(IID_IMediaControl, (void **)&m_pMediaControl)); if(SUCCEEDED(hr)) { hr = m_pMediaControl->Run(); if(FAILED(hr)) { // stop parts that ran DWORD dError = GetLastError(); m_pMediaControl->Stop(); return FALSE; } /// SetupVideoWindow(m_pVWP); } // return FALSE; Cleanup: if (FAILED(hr)) { ////////////////////////////////////////// if (m_pMediaControl != NULL) { m_pMediaControl->Stop(); m_pMediaControl.Release(); m_pMediaControl = NULL; } if (m_pVWP != NULL) { m_pVWP->put_Visible(OAFALSE); m_pVWP->put_Owner(NULL); m_pVWP->Release(); m_pVWP = NULL; } /* if (m_pFilterGraph != NULL) { IEnumFilters *pEnum = NULL; hr = m_pFilterGraph->EnumFilters(&pEnum); if (SUCCEEDED(hr)) { IBaseFilter *pFilter = NULL; while (S_OK == pEnum->Next(1, &pFilter, NULL) ) { m_pFilterGraph->RemoveFilter(pFilter); pEnum->Reset(); pFilter->Release(); } pEnum->Release(); } }*/ if (m_pFilterGraph != NULL) { //////// if (m_pVideoCaptureFilter!=NULL) m_pFilterGraph->RemoveFilter(m_pVideoCaptureFilter); if (m_pRenderP!=NULL) m_pFilterGraph->RemoveFilter(m_pRenderP); if (m_pRenderS!=NULL) m_pFilterGraph->RemoveFilter(m_pRenderS); if (m_pSampleGrabFilter!=NULL) m_pFilterGraph->RemoveFilter(m_pSampleGrabFilter); if (m_pTransformFilter!=NULL) m_pFilterGraph->RemoveFilter(m_pTransformFilter); /////////// m_pFilterGraph.Release(); m_pFilterGraph = NULL; } if (m_pCaptureGraphBuilder != NULL) { m_pCaptureGraphBuilder.Release(); m_pCaptureGraphBuilder = NULL; } if (m_pSampleGrabFilter != NULL) { m_pSampleGrabFilter.Release(); m_pSampleGrabFilter = NULL; } if (m_pVideoCaptureFilter != NULL) { m_pVideoCaptureFilter.Release(); m_pVideoCaptureFilter = NULL; } if (m_pTransformFilter != NULL) { m_pTransformFilter.Release(); m_pTransformFilter = NULL; } if (m_pRenderP != NULL) { m_pRenderP->Release(); m_pRenderP = NULL; } if (m_pRenderS != NULL) { m_pRenderS->Release(); m_pRenderS = NULL; } AfxMessageBox(TEXT("启动摄像头出错!"),0,0); bCameraOpened = FALSE; CoUninitialize(); return FALSE; /// SendMessage(); ////////////////////////////////////// } else///0104 { g_hImgDataArrived = CreateEvent(NULL,FALSE,FALSE,NULL); bCameraOpened = TRUE; } return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } BOOL CCappreviewDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here if (!m_dlgCommandBar.Create(this) || !m_dlgCommandBar.InsertMenuBar(IDR_MENU_CAPDLG_IDENTI)) { TRACE0("Failed to create CommandBar\n"); return FALSE; } m_ctrlTitle.SetWindowTextW(TEXT(" 中国移动条码识别V1.0.0\r\n \r\n请把待识别的QR码图像对准置于红框内")); GetDlgItem(IDC_STATIC_CAPWND)->SetFocus(); // ActivateKBHook(AfxGetApp()->m_hInstance,CameraKeyboardProc); ///////////////////////////////////// HKEY hKey; TCHAR pAppNameQuick[3][64]; CString sAppNameQuick; CString keyName; DWORD lAppName=64*sizeof(TCHAR); DWORD type= 0; LONG lResult=0; int i; for (i=0; i<3; i++) { keyName.Format(TEXT("Software\\Microsoft\\Shell\\Keys\\40C%d"),i+1); lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,keyName,0,0,&hKey); type = REG_SZ; lResult = RegQueryValueEx(hKey,L"",NULL,&type,(LPBYTE)pAppNameQuick[i],&lAppName);//TEXT("ResetCmd") // lResult = RegQueryValueEx(hKey,0,0,&type,(LPBYTE)pAppNameQuick[i],&lAppName);//ResetCmd if (lResult==ERROR_MORE_DATA) RegQueryValueEx(hKey,L"",NULL,&type,(LPBYTE)pAppNameQuick[i],&lAppName); sAppNameQuick = pAppNameQuick[i]; int j = sAppNameQuick.Find(TEXT("照相机"),0); if (j!=-1) break; } switch (i) { case 0: g_CAMVK_CODE = 0xC1; break; case 1: g_CAMVK_CODE = 0xC2; break; case 2: g_CAMVK_CODE = 0xC3; break; default: break; } if (g_CAMVK_CODE!=0) { if (g_hKBHook == NULL) g_hKBHook = SetWindowsHookEx(WH_KEYBOARD_LL,CameraKeyboardProc,AfxGetApp()->m_hInstance, NULL); } /// ::ChangeDisplaySettingsEx(NULL, &g_devModeForqr, NULL, 0, NULL); BOOL bCamRun = RunCapture(); if (!bCamRun) PostMessage(USR_CAMERAUNWORK,0,0); // SendMessage(AfxGetApp()->m_pActiveWnd,USR_MYMESSAGE1,0,0); /// SetupVideoWindow(m_pVWP); return FALSE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } LRESULT CCappreviewDlg::OnMyReceiveCameraUnwork(WPARAM wParam,LPARAM lParam) { On32812Return(); return TRUE; } void CCappreviewDlg::On32810Identify() { // TODO: Add your command handler code here /* if (!m_dlgCommandBar.InsertMenuBar(IDR_MENU_CAPDLG_CANCELIDENTI))//IDR_MENU_MODELESSDLG IDR_MENU_CANCEL { TRACE0("Failed to create CommandBar\n"); return ; } m_ctrlIdentiing.SetWindowTextW(TEXT("正在识别"));*/ m_nIdentifyCount = 0; /******************************/ CQRAPP2Dlg dlg; /// LPTSTR lpUnistr = NULL; TCHAR lpUnistr[MAX_QRSTRING_LENGTH+1]; HRESULT hr; IPin *pPin = NULL; IEnumPins *pEnum; unsigned long fetched; int nPin = 1; //Still Pin IAMVideoControl *pAMVidControl = NULL; if (m_pVideoCaptureFilter==NULL) return;// hr = m_pVideoCaptureFilter->QueryInterface(IID_IAMVideoControl,(void **)&pAMVidControl); hr = m_pVideoCaptureFilter->EnumPins(&pEnum); while (nPin>0) { hr = pEnum->Next(1,&pPin,&fetched); nPin--; pPin->Release();/// pPin = NULL; } hr = pEnum->Next(1,&pPin,&fetched); hr = pAMVidControl->SetMode(pPin, VideoControlFlag_Trigger); // SetTimer(MyTimerID,5000,NULL); // m_nIdentifyCount++; /// g_hImgDataArrived = CreateEvent(NULL,FALSE,FALSE,NULL); WaitForSingleObject(g_hImgDataArrived,INFINITE); ///if (TRUE == dlg.DecodeMapData(g_pOutRectImageData,&lpUnistr)) if (TRUE == dlg.DecodeMapData(g_pOutRectImageData,lpUnistr)) { // KillTimer(MyTimerID); m_nIdentifyCount = 0; if (g_pOutRectImageData != NULL) { delete []g_pOutRectImageData;// g_pOutRectImageData = NULL; g_nOutRectImageLen = 0; } pEnum->Release(); pEnum = NULL; pAMVidControl->Release();/// pAMVidControl = NULL; pPin->Release(); pPin = NULL; ///1212 /// On32812Return(); /// Infoparse info; if (info.CardDataParse_Uni(lpUnistr,lstrlen(lpUnistr)) ==FALSE) { if (AfxMessageBox(TEXT("识别失败,还要继续识别吗?"),MB_YESNO) == IDNO) { On32812Return(); } else { } } else { On32812Return(); dlg.ShowDataIndlg(lpUnistr,info); } } else { pEnum->Release(); pEnum = NULL; pAMVidControl->Release();/// pAMVidControl = NULL; pPin->Release(); pPin = NULL; if (AfxMessageBox(TEXT("识别失败,还要继续识别吗?"),MB_YESNO) == IDNO) { On32812Return(); } else { } } /// hr = m_pVWP->put_Visible(OATRUE); /*Cleanup: if( FAILED( hr )) { return; }*/ return ; } void CCappreviewDlg::CloseCapture() { if (!bCameraOpened) return; HRESULT hr = S_OK; CloseHandle(g_hImgDataArrived); //camera flash off IAMCameraControl *pCamConfig = NULL; CHK( m_pVideoCaptureFilter->QueryInterface(IID_IAMCameraControl, (void**)&pCamConfig)); long lMax = 0; long lMin = 0; long lSteppingDelta = 0; long lDefault = 0; long lCapflag = 0; /// CHK( pCamConfig->GetRange(CameraControl_Flash,&lMin,&lMax,&lSteppingDelta,&lDefault,&lCapflag)); CHK( pCamConfig->Set(CameraControl_Flash,m_lCameraFlash,CameraControl_Flags_Manual )); /////////////////////////////////0126 CHK( pCamConfig->Set(CameraControl_Pan ,m_lCameraPan,CameraControl_Flags_Manual )); CHK( pCamConfig->Set(CameraControl_Tilt ,m_lCameraTilt,CameraControl_Flags_Manual )); CHK( pCamConfig->Set(CameraControl_Roll ,m_lCameraRoll,CameraControl_Flags_Manual )); CHK( pCamConfig->Set(CameraControl_Zoom,m_lCameraZoom,CameraControl_Flags_Manual )); CHK( pCamConfig->Set(CameraControl_Exposure,m_lCameraExposure,CameraControl_Flags_Manual )); CHK( pCamConfig->Set(CameraControl_Iris ,m_lCameraIris,CameraControl_Flags_Manual )); CHK( pCamConfig->Set(CameraControl_Focus ,m_lCameraFocus,CameraControl_Flags_Manual )); ////////////////////////////////// pCamConfig->Release(); pCamConfig = NULL; if (m_pMediaControl != NULL) { CHK( m_pMediaControl->Stop()); m_pMediaControl.Release(); m_pMediaControl = NULL; } if (m_pVWP != NULL) { CHK( m_pVWP->put_Visible(OAFALSE)); CHK( m_pVWP->put_Owner(NULL)); m_pVWP->Release(); m_pVWP = NULL; } if (m_pFilterGraph != NULL) { IEnumFilters *pEnum = NULL; hr = m_pFilterGraph->EnumFilters(&pEnum); if (SUCCEEDED(hr)) { IBaseFilter *pFilter = NULL; while (S_OK == pEnum->Next(1, &pFilter, NULL) ) { m_pFilterGraph->RemoveFilter(pFilter); pEnum->Reset(); pFilter->Release(); } pEnum->Release(); } } if (m_pFilterGraph != NULL) { m_pFilterGraph.Release(); m_pFilterGraph = NULL; } if (m_pCaptureGraphBuilder != NULL) { m_pCaptureGraphBuilder.Release(); m_pCaptureGraphBuilder = NULL; } if (m_pSampleGrabFilter != NULL) { m_pSampleGrabFilter.Release(); m_pSampleGrabFilter = NULL; } if (m_pVideoCaptureFilter != NULL) { m_pVideoCaptureFilter.Release(); m_pVideoCaptureFilter = NULL; } if (m_pTransformFilter != NULL) { m_pTransformFilter.Release(); m_pTransformFilter = NULL; } if (m_pRenderP != NULL) { m_pRenderP->Release(); m_pRenderP = NULL; } if (m_pRenderS != NULL) { m_pRenderS->Release(); m_pRenderS = NULL; } bCameraOpened = FALSE; CoUninitialize(); Cleanup: return; } void CCappreviewDlg::On32812Return() { // TODO: Add your command handler code here HRESULT hr = S_OK; CloseCapture(); ///1212 if (g_hKBHook != NULL) { UnhookWindowsHookEx(g_hKBHook); g_hKBHook = NULL; g_CAMVK_CODE = 0; } ///1212 Cleanup: //CDialog::EndDialog(IDOK);*/ CDialog::OnOK(); /*UpdateData(); DestroyWindow();*/ /// } /* void CCappreviewDlg::On32813CancelIdenti() { // TODO: Add your command handler code here if (!m_dlgCommandBar.InsertMenuBar(IDR_MENU_CAPDLG_IDENTI))//IDR_MENU_MODELESSDLG IDR_MENU_CANCEL { TRACE0("Failed to create CommandBar\n"); return ; } m_ctrlIdentiing.SetWindowTextW(TEXT("")); KillTimer(MyTimerID); /// m_nIdentifyCount = 0; //m_nIdentifyCount = IDENTIFY_MAXCOUNT +1 ; }*/ /* void CCappreviewDlg::OnTimer(UINT_PTR nIDEvent) { // TODO: Add your message handler code here and/or call default switch(nIDEvent) { case MyTimerID: { if (m_nIdentifyCount == IDENTIFY_MAXCOUNT) { KillTimer(MyTimerID); m_nIdentifyCount = 0; /// On32812Return(); //AfxMessageBox(TEXT("识别失败"),0,0); if (AfxMessageBox(_T("识别失败是否继续识别?"), MB_YESNO)==IDNO) { On32812Return(); } else { On32810Identify(); } ///return; } else { /******************************/ /* CQRAPP2Dlg dlg; /// LPTSTR lpUnistr = NULL; TCHAR lpUnistr[MAX_QRSTRING_LENGTH+1]; HRESULT hr; IPin *pPin = NULL; IEnumPins *pEnum; unsigned long fetched; int nPin = 1; //Still Pin IAMVideoControl *pAMVidControl = NULL; hr = m_pVideoCaptureFilter->QueryInterface(IID_IAMVideoControl,(void **)&pAMVidControl); hr = m_pVideoCaptureFilter->EnumPins(&pEnum); while (nPin>0) { hr = pEnum->Next(1,&pPin,&fetched); nPin--; pPin->Release();/// pPin = NULL; } hr = pEnum->Next(1,&pPin,&fetched); hr = pAMVidControl->SetMode(pPin, VideoControlFlag_Trigger); // SetTimer(MyTimerID,3000,NULL); m_nIdentifyCount++; // g_hImgDataArrived = CreateEvent(NULL,FALSE,FALSE,NULL); WaitForSingleObject(g_hImgDataArrived,INFINITE); NKDbgPrintfW(TEXT("On32810Identify: WaitForSingleObject!\r\n")); /// if (TRUE == dlg.DecodeMapData(g_pOutRectImageData,&lpUnistr)) if (TRUE == dlg.DecodeMapData(g_pOutRectImageData,lpUnistr)) { NKDbgPrintfW(TEXT("On32810Identify: succeeds!\r\n")); KillTimer(MyTimerID); m_nIdentifyCount = 0; if (g_pOutRectImageData != NULL) { delete []g_pOutRectImageData;// g_pOutRectImageData = NULL; g_nOutRectImageLen = 0; } pEnum->Release(); pEnum = NULL; pAMVidControl->Release();/// pAMVidControl = NULL; pPin->Release(); pPin = NULL; On32812Return();//? dlg.ShowDataIndlg(lpUnistr); /* if (lpUnistr != NULL) { delete []lpUnistr; lpUnistr = NULL; } */ /* } else { NKDbgPrintfW(TEXT("On32810Identify: fails!\r\n")); pEnum->Release(); pEnum = NULL; pAMVidControl->Release();/// pAMVidControl = NULL; pPin->Release(); pPin = NULL; } } } break; default: break; } /// CDialog::OnTimer(nIDEvent); } */ void CCappreviewDlg::PostNcDestroy() { // TODO: Add your specialized code here and/or call the base class CDialog::PostNcDestroy(); //delete this; } void CCappreviewDlg::OnOK() { // TODO: Add your specialized code here and/or call the base class On32812Return(); ///CDialog::OnOK(); } void CCappreviewDlg::OnPaint() { /// CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here // Do not call CDialog::OnPaint() for painting messages /*******************************************/ /* CBitmap *pBmpOld; GetClientRect(&m_rectClient);//获得客户区大小位置信息并保存到m_rectClient VERIFY(pBmpOld=m_dcMem.SelectObject(&m_Bitmap)); //将位图选入m_dcMem m_Bitmap.GetBitmap(&m_bmpInfo);//获得位图信息 VERIFY(m_hBmpOld=(HBITMAP)pBmpOld->GetSafeHandle());//保存以前位图句柄 dc.StretchBlt(m_rectClient.left,m_rectClient.top,m_rectClient.Width(),m_rectClient.Height(),&m_dcMem,0,0,m_bmpInfo.bmWidth-1,m_bmpInfo.bmHeight-1,SRCCOPY); */ CRect rect; CPaintDC dc(this); GetClientRect(rect); dc.FillSolidRect(rect,RGB(206,214,255)); //设置为绿色背景 CDialog::OnPaint(); } void CCappreviewDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) { CDialog::OnActivate(nState, pWndOther, bMinimized); // TODO: Add your message handler code here /* if (pWndOther != NULL) return;*/ //if (nState==WA_INACTIVE && pWndOther==NULL) if (nState==WA_INACTIVE ) { g_bModeSettingChanged = TRUE; } if (pWndOther != NULL) return; else { if (nState == WA_ACTIVE) { // RunCapture(); //SetupVideoWindow(m_pVWP); DEVMODE mode= {0}; mode.dmSize = sizeof(DEVMODE); mode.dmFields = DM_DISPLAYORIENTATION; ::ChangeDisplaySettingsEx(NULL, &mode, 0, CDS_TEST, NULL); if (mode.dmDisplayOrientation != DMDO_0) ::ChangeDisplaySettingsEx(NULL, &g_devModeForqr, NULL, 0, NULL); } else if (nState == WA_INACTIVE) { /// CloseCapture(); g_bModeSettingChanged = TRUE; On32812Return(); } else if (nState == WA_CLICKACTIVE) { } } }