⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 camerawindow.cpp

📁 Microsoft WinCE 6.0 BSP FINAL release source code for use with the i.MX27ADS TO2 WCE600_FINAL_MX27_S
💻 CPP
📖 第 1 页 / 共 5 页
字号:

        DDraw_Changed = TRUE;

        while(pCamWin->m_bPreviewRunning)
        {

            LeaveCriticalSection(&pCamWin->m_csPreview);

            pCamWin->m_bPreviewStopped = FALSE;

            EnterCriticalSection(&pCamWin->m_csPreview);

            bResult = DeviceIoControl(pCamWin->m_hPreviewPinDll, // file handle to the driver
                        IOCTL_CS_BUFFERS,                        // I/O control code
                        &csBufferInfo,                           // in buffer
                        sizeof(csBufferInfo),                    // in buffer size
                        pCamWin->m_pPreviewStreamDesc,           // out buffer
                        sizeof(CS_STREAM_DESCRIPTOR),            // out buffer size
                        &dBytesTransferred,                      // number of bytes returned
                        NULL);                                   // ignored (=NULL)
                        
           // If enqueued the buffer error,break the loop.
           // 1. Something wrong with camera driver.
           // 2. The buffer has been enqueued.
           // 3. Camera Power management stopped the driver. 
            if ( FALSE == bResult )                                    
            {
                RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("CamApp_PreviewThread: Enqueue a buffer error !\r\n")));
                Sleep(10000);
                break;       
            }
            RETAILMSG(CAMAPP_DEBUG_MSG,(TEXT("CamApp_PreviewThread: WaitForSingleObject\r\n")));

            if (WaitForSingleObject(pCamWin->m_hPreviewMsgQ, 10000) != WAIT_OBJECT_0)
            {
                RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("CamApp_PreviewThread: WaitForSingleObject Timeout!\r\n")));
                return 1;
            }

            if(!ReadMsgQueue(pCamWin->m_hPreviewMsgQ, &CsMsgQBuff, sizeof(CS_MSGQUEUE_BUFFER), &size, 10, &flags))
            {
                RETAILMSG(CAMAPP_ERROR_MSG,(TEXT("CamApp_PreviewThread: unable to read preview message queue. Try again.\r\n")));
                return 1;
            }
            pbySrcBuffer = reinterpret_cast<PUINT8>(CsMsgQBuff.pStreamDescriptor->CsStreamHeader.Data);
  
            if (!pCamWin->m_bPreviewRunning)
            {
                break;
            }

            hRet = g_pDDSOverback->Lock( NULL, &ddsd, DDLOCK_WAITNOTBUSY, NULL); // DDLOCK_SURFACEMEMORYPTR|DDLOCK_WAIT, NULL);
            if (DD_OK != hRet)   
            {
                ReleaseAllObjects();
                RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("CamApp_PreviewThread:Init DDraw resource failure? \r\n")));
                return 0;
            }
        
            // If the output resolution is changed ,update the background.
            if (TRUE == DDraw_Changed)
            {          
                memset(ddsd.lpSurface,0,pCamWin->m_DisplayInfo.screenWidth*pCamWin->m_DisplayInfo.screenHeight*2);
                hRet = g_pDDSOverback->Unlock( NULL); 
                if (DD_OK != hRet)   
                {
                     ReleaseAllObjects();
                     RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("CamApp_PreviewThread:Init DDraw resource failure? \r\n")));
                     return 0;
                }

                hRet = g_pDDSOverlay->Flip(NULL,0);
                if (DD_OK != hRet)   
                {
                     ReleaseAllObjects();
                     RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("CamApp_PreviewThread:Init DDraw resource failure? \r\n")));
                     return 0;
                }
   
                hRet = g_pDDSOverback->Lock( NULL, &ddsd, DDLOCK_WAITNOTBUSY, NULL);
                if (DD_OK != hRet)   
                {
                     ReleaseAllObjects();
                     RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("CamApp_PreviewThread:Init DDraw resource failure? \r\n")));
                     return 0;
                }

                memset(ddsd.lpSurface,0,pCamWin->m_DisplayInfo.screenWidth*pCamWin->m_DisplayInfo.screenHeight*2);
                DDraw_Changed = FALSE;
            }

            pbyDstBuffer = reinterpret_cast<PUINT8>(ddsd.lpSurface) + 
                        ((pCamWin->m_DisplayInfo.xOffset + (pCamWin->m_DisplayInfo.yOffset - GetSystemMetrics(SM_CYMENU)) * pCamWin->m_iPanelWidth) *
                        (pCamWin->m_iPanelBpp/ 8));
                    
            for (height = 0; height < displayHeight; height++)
            {
                memcpy(pbyDstBuffer,pbySrcBuffer,displayWidth * (pCamWin->m_iPanelBpp / 8));

                pbySrcBuffer += pCamWin->m_DisplayInfo.imageWidth * (pCamWin->m_iPanelBpp / 8);
                pbyDstBuffer += pCamWin->m_DisplayInfo.screenWidth * (pCamWin->m_iPanelBpp / 8);
             }

            g_pDDSOverback->Unlock(NULL);  
            hRet = g_pDDSOverlay->Flip(NULL,0);
            if (DD_OK != hRet)   
            {
                ReleaseAllObjects();
                RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("CamApp_PreviewThread:Init DDraw resource failure? \r\n")));
                return 0;
            }
            
            RETAILMSG(CAMAPP_DEBUG_MSG,(TEXT("CamApp_PreviewThread: displayHeight = %d, displayWidth = %d.\r\n"), displayHeight, displayWidth));
        }

        LeaveCriticalSection(&pCamWin->m_csPreview);
        
        Sleep(10);
        pCamWin->m_bPreviewStopped = TRUE;
        RETAILMSG(CAMAPP_DEBUG_MSG,(TEXT("CamApp_PreviewThread: m_bPreviewStopped = TRUE\r\n")));
    }

    RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("CamApp_PreviewThread-\r\n")));
    
    return 0;
}

#else

/***********************************************************************
 *
 *  FUNCTION:        CamApp_PreviewThread
 *
 *  DESCRIPTION:     This function is Preview thread.
 *
 *  PARAMETERS:      None
 *
 *  RETURNS:         A double word value for ExitThread()
 *
 **********************************************************************/
UINT32 CamApp_PreviewThread(LPVOID lpParameter)
{        
    PCAMERAWINDOW  pCamWin = reinterpret_cast<PCAMERAWINDOW>(lpParameter);
    UINT8          *pbySrcBuffer = NULL;
    UINT16         displayHeight = 0;
    UINT16         displayWidth = 0;
    DWORD          dBytesTransferred;
    DWORD          size,flags;
    BOOL           bRotation = FALSE;
    HDC            m_hdcMain;
    RECT           rc;
    BOOL           bResult = FALSE;
    CSBUFFER_INFO  csBufferInfo;
    CS_VIDEOINFOHEADER  display_VideoInfo;
    CS_MSGQUEUE_BUFFER  CsMsgQBuff;
  
    RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("CamApp_PreviewThread+\r\n")));

    bRotation = pCamWin->m_bRotation;
    m_hdcMain = GetDC(pCamWin->m_hWnd);
    csBufferInfo.dwCommand = CS_ENQUEUE;
    csBufferInfo.pStreamDescriptor= pCamWin->m_pPreviewStreamDesc;
    
    while (!pCamWin->m_bCamAppThreadTerminate) 
    {
        memcpy( &display_VideoInfo,&(pCamWin->m_PreviewDataFormat.VideoInfoHeader),sizeof(display_VideoInfo));
        Sleep(1000);
        ::GetClientRect(pCamWin->m_hWnd, &rc);
        pCamWin->m_DisplayInfo.screenWidth =(UINT16)(rc.right - rc.left);
        pCamWin->m_DisplayInfo.screenHeight = (UINT16)( rc.bottom - rc.top - SKIN_HEIGHT - GetSystemMetrics(SM_CYMENU));

        if (!pCamWin->m_bRotation)
        {
            pCamWin->m_DisplayInfo.imageWidth = (UINT16)abs(pCamWin->m_PreviewDataFormat.VideoInfoHeader.bmiHeader.biWidth);
            pCamWin->m_DisplayInfo.imageHeight = (UINT16)abs(pCamWin->m_PreviewDataFormat.VideoInfoHeader.bmiHeader.biHeight);
        }
        else
        {
           if (FALSE == bRotation) //old status is not rotation. so convert height and width.
            {
                pCamWin->m_DisplayInfo.imageWidth = (UINT16)abs(pCamWin->m_PreviewDataFormat.VideoInfoHeader.bmiHeader.biHeight);
                pCamWin->m_DisplayInfo.imageHeight = (UINT16)abs(pCamWin->m_PreviewDataFormat.VideoInfoHeader.bmiHeader.biWidth);
            }
            else
            {
                pCamWin->m_DisplayInfo.imageWidth = (UINT16)abs(pCamWin->m_PreviewDataFormat.VideoInfoHeader.bmiHeader.biHeight);
                pCamWin->m_DisplayInfo.imageHeight = (UINT16)abs(pCamWin->m_PreviewDataFormat.VideoInfoHeader.bmiHeader.biWidth);
            }
        }

        display_VideoInfo.bmiHeader.biHeight = pCamWin->m_DisplayInfo.imageHeight;
        display_VideoInfo.bmiHeader.biWidth = pCamWin->m_DisplayInfo.imageWidth;

        bRotation = pCamWin->m_bRotation;

        if (pCamWin->m_DisplayInfo.imageWidth >= pCamWin->m_DisplayInfo.screenWidth)
        {
            pCamWin->m_DisplayInfo.xOffset = 0;
            displayWidth = pCamWin->m_DisplayInfo.screenWidth;
        }
        else
        {
            pCamWin->m_DisplayInfo.xOffset = (UINT16)(rc.left + (pCamWin->m_DisplayInfo.screenWidth - pCamWin->m_DisplayInfo.imageWidth)/2);
            displayWidth = pCamWin->m_DisplayInfo.imageWidth;
        }

        if (pCamWin->m_DisplayInfo.imageHeight >= pCamWin->m_DisplayInfo.screenHeight)
        {
            pCamWin->m_DisplayInfo.yOffset = (UINT16)(rc.top + GetSystemMetrics(SM_CYMENU));
            displayHeight = pCamWin->m_DisplayInfo.screenHeight;
        }
        else
        {
            pCamWin->m_DisplayInfo.yOffset = (UINT16)(rc.top + GetSystemMetrics(SM_CYMENU) + (pCamWin->m_DisplayInfo.screenHeight- pCamWin->m_DisplayInfo.imageHeight)/2);
            displayHeight = pCamWin->m_DisplayInfo.imageHeight;
        }

        rc.left = pCamWin->m_DisplayInfo.xOffset;
        rc.top = pCamWin->m_DisplayInfo.yOffset;        
        if ((pCamWin->m_DisplayInfo.xOffset + pCamWin->m_DisplayInfo.imageWidth) < pCamWin->m_DisplayInfo.screenWidth)
        {
            rc.right = pCamWin->m_DisplayInfo.xOffset + pCamWin->m_DisplayInfo.imageWidth; 
        }
        else
        {
            rc.right = pCamWin->m_DisplayInfo.screenWidth; 
        }

        if ((pCamWin->m_DisplayInfo.yOffset + pCamWin->m_DisplayInfo.imageHeight) < pCamWin->m_DisplayInfo.screenHeight)
        {
            rc.bottom = pCamWin->m_DisplayInfo.xOffset + pCamWin->m_DisplayInfo.imageHeight; 
        }
        else
        {
            rc.bottom = pCamWin->m_DisplayInfo.xOffset + ((pCamWin->m_DisplayInfo.screenWidth *  display_VideoInfo.bmiHeader.biHeight)/display_VideoInfo.bmiHeader.biWidth);
        }
                  
        if (pCamWin->m_bPreviewRunning)
            RETAILMSG(CAMAPP_DEBUG_MSG,(TEXT("CamApp_PreviewThread: m_bPreviewRunning = TRUE.\r\n")));
        else
            RETAILMSG(CAMAPP_DEBUG_MSG,(TEXT("CamApp_PreviewThread: m_bPreviewRunning = FALSE.\r\n")));
        
        EnterCriticalSection(&pCamWin->m_csPreview);
    
        while(pCamWin->m_bPreviewRunning)
        {
            LeaveCriticalSection(&pCamWin->m_csPreview);

            pCamWin->m_bPreviewStopped = FALSE;

            EnterCriticalSection(&pCamWin->m_csPreview);

            bResult = DeviceIoControl(pCamWin->m_hPreviewPinDll,// file handle to the driver
                        IOCTL_CS_BUFFERS,                       // I/O control code
                        &csBufferInfo,                          // in buffer
                        sizeof(csBufferInfo),                   // in buffer size
                        pCamWin->m_pPreviewStreamDesc,          // out buffer
                        sizeof(CS_STREAM_DESCRIPTOR),           // out buffer size
                        &dBytesTransferred,                     // number of bytes returned
                        NULL);                                  // ignored (=NULL)
                        
            // If enqueued the buffer error,break the loop.
            // 1. Something wrong with camera driver.
            // 2. The buffer has been enqueued.
            // 3. Camera Power management stopped the driver. 
            if ( FALSE == bResult )                                    
            {
                 RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("CamApp_PreviewThread: Enqueue a buffer error !\r\n")));
                 Sleep(10000);
                 break;       
            }
            RETAILMSG(CAMAPP_DEBUG_MSG,(TEXT("CamApp_PreviewThread: WaitForSingleObject\r\n")));

            if (WaitForSingleObject(pCamWin->m_hPreviewMsgQ,INFINITE) != WAIT_OBJECT_0)
            {
                RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("CamApp_PreviewThread: WaitForSingleObject Timeout!\r\n")));
                return 1;       
            }

            if(!ReadMsgQueue(pCamWin->m_hPreviewMsgQ, &CsMsgQBuff, sizeof(CS_MSGQUEUE_BUFFER), &size, 10, &flags))
            {
                RETAILMSG(CAMAPP_ERROR_MSG,(TEXT("CamApp_PreviewThread: unable to read preview message queue. Try again.\r\n")));
                return 1;
            }

			pbySrcBuffer = reinterpret_cast<PUINT8>(CsMsgQBuff.pStreamDescriptor->CsStreamHeader.Data);
			
            if (!pCamWin->m_bPreviewRunning)
            {
                break;
            }
            
            if(FALSE == StretchDIBits(m_hdcMain,
                                      rc.left,
                                      rc.top,
                                      displayWidth,
                                      displayHeight,
                                      0,
                                      0,
                                      display_VideoInfo.bmiHeader.biWidth,
                                      display_VideoInfo.bmiHeader.biHeight,
                                      pbySrcBuffer, 
                                      (BITMAPINFO *) &(display_VideoInfo.bmiHeader),
                                      DIB_RGB_COLORS, 
                                      SRCCOPY))
            {
                break;
            }

            RETAILMSG(0,(TEXT("CamApp_PreviewThread: displayHeight = %d, displayWidth = %d.\r\n"), displayHeight, displayWidth));
        }    
        LeaveCriticalSection(&pCamWin->m_csPreview);
        
        Sleep(10);
        pCamWin->m_bPreviewStopped = TRUE;
        RETAILMSG(CAMAPP_DEBUG_MSG,(TEXT("CamApp_PreviewThread: m_bPreviewStopped = TRUE\r\n")));
    }

    RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("CamApp_PreviewThread-\r\n")));
    
    return 0;
}
#endif

/*********************************************************************
 EXPORTED FUNCTIONS
*********************************************************************/
/********************************************************************
 *
 *  FUNCTION:       CCameraWindow
 *
 *  DESCRIPTION:    Constructor of CCameraWindow class
 *
 *  PARAMETERS:     HWND hWnd, HINSTANCE hInstance
 *
 *  RETURNS:        None
 *
 ********************************************************************/
CCameraWindow::CCameraWindow(HWND hWnd, HINSTANCE hInstance)
{
    m_hInstance = hInstance;
    m_hWnd = hWnd;
    m_hWndCB = NULL;
    m_hWndSC = NULL;
    m_hWndBrowser = NULL;
    m_dwZoomLevel = 1;

    m_bPreviewRunning = FALSE;
    m_bPreviewStopped = TRUE;
    m_bCamAppThreadTerminate = FALSE;
    m_hCameraDevice = NULL;
    m_hCameraDll = NULL;
    m_hPreviewPinDll = NULL;
    m_hCapturePinDll = NULL;
    m_hStillPinDll = NULL;
    
    m_hPreviewMsgQ = NULL;
    m_hStillMsgQ = NULL;
    m_hCaptureMsgQ = NULL;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -