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

📄 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 页
字号:

    m_pPreviewStreamDesc= NULL;
    m_pStillStreamDesc= NULL;
    m_pCaptureStreamDesc= NULL;
    
    m_pStillPinBuff = NULL;

    m_dFrameRate = 30;
    m_dActualRate = 0.0;
    m_uiTimer = 0;

    m_bRotation = FALSE;
    m_bVertFlip = FALSE;
    m_bHorizFlip = FALSE;
    m_dwResolution = QVGA;

    m_iPanelWidth = 0;
    m_iPanelHeight = 0;
    m_iPanelBpp = 0;

    m_pPreviewDataRanges = NULL;
    m_pStillDataRanges = NULL;
    m_pCaptureDataRanges = NULL;

    m_iNumPreviewDataRanges = 0;
    m_iNumStillDataRanges = 0;
    m_iNumCaptureDataRanges = 0;

    InitializeCriticalSection(&m_csCapture);
    InitializeCriticalSection(&m_csStill);
    InitializeCriticalSection(&m_csPreview);
    InitializeCriticalSection(&m_csCamera);
}


/********************************************************************
 *
 *  FUNCTION:       ~CCameraWindow
 *
 *  DESCRIPTION:    Destructor of CCameraWindow class
 *
 *  PARAMETERS:     None
 *
 *  RETURNS:        None
 *
 ********************************************************************/
CCameraWindow::~CCameraWindow()
{
    Fini();
}

/********************************************************************
 *
 *  FUNCTION:       init
 *
 *  DESCRIPTION:    Initialize the CCameraWindow class
 *
 *  PARAMETERS:     None
 *
 *  RETURNS:        TRUE if initialize successfully, otherwise return FALSE
 *
 ********************************************************************/

 BOOL CCameraWindow::Init()
{
	RECT         rc;

    // Create the command bar
    m_hWndCB = ::CommandBar_Create(m_hInstance, m_hWnd, 1);
    if (NULL != m_hWndCB)
    {
        // Add a menu bar
        ::CommandBar_InsertMenubar(m_hWndCB, m_hInstance, IDR_MENUBAR, 0);
        ::CommandBar_AddAdornments(m_hWndCB, 0, 0);
        ::CommandBar_Show(m_hWndCB, TRUE);
    }
    RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("Init: Create Menu Done!\r\n")));

    ::GetClientRect(m_hWnd, &rc);
    
    //Initialize Button 
    UINT iYButton = rc.bottom -SKIN_HEIGHT + (SKIN_HEIGHT - BUTTON_HEIGHT)/2;
    
    m_binfo[BUTTON_CAPTURE].eState = DISABLED;
    m_binfo[BUTTON_CAPTURE].pCaptNormal = L"Capture";
    m_binfo[BUTTON_CAPTURE].pCaptCapture = L"Save";
    m_binfo[BUTTON_CAPTURE].hID = (HMENU)CamApp_BTN_ID_CAPTURE;
    m_binfo[BUTTON_CAPTURE].hWnd = NULL;
    m_binfo[BUTTON_CAPTURE].x = 10;
    m_binfo[BUTTON_CAPTURE].y = iYButton;
    m_binfo[BUTTON_CAPTURE].nWidth = BUTTON_WIDTH;
    m_binfo[BUTTON_CAPTURE].nHeight = BUTTON_HEIGHT;

    m_binfo[BUTTON_VIEW].eState = DISABLED;
    m_binfo[BUTTON_VIEW].pCaptNormal = L"View";
    m_binfo[BUTTON_VIEW].pCaptCapture = L"Discard";
    m_binfo[BUTTON_VIEW].hID = (HMENU)CamApp_BTN_ID_VIEW;
    m_binfo[BUTTON_VIEW].hWnd = NULL;
    m_binfo[BUTTON_VIEW].x = 70;
    m_binfo[BUTTON_VIEW].y = iYButton;
    m_binfo[BUTTON_VIEW].nWidth = BUTTON_WIDTH;
    m_binfo[BUTTON_VIEW].nHeight = BUTTON_HEIGHT;

    m_binfo[BUTTON_ROTATE].eState = DISABLED;
    m_binfo[BUTTON_ROTATE].pCaptNormal = L"Rotate";
    m_binfo[BUTTON_ROTATE].pCaptCapture = L"Rotate";
    m_binfo[BUTTON_ROTATE].hID = (HMENU)CamApp_BTN_ID_ROTATE;
    m_binfo[BUTTON_ROTATE].hWnd = NULL;
    m_binfo[BUTTON_ROTATE].x = 130;
    m_binfo[BUTTON_ROTATE].y = iYButton;
    m_binfo[BUTTON_ROTATE].nWidth = BUTTON_WIDTH;
    m_binfo[BUTTON_ROTATE].nHeight = BUTTON_HEIGHT;
    
    // Create Button
    for (int ii=0; ii<SKIN_SIZE; ii++) 
    {
        m_binfo[ii].hWnd = CreateWindowEx(0, 
                                L"BUTTON", 
                                m_binfo[ii].pCaptNormal,
                                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
                                m_binfo[ii].x, 
                                m_binfo[ii].y, 
                                m_binfo[ii].nWidth, 
                                m_binfo[ii].nHeight,
                                m_hWnd,
                                m_binfo[ii].hID,
                                m_hInstance, 
                                NULL );
    }
    
    RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("Init: Create Button Done!\r\n")));

    //Create text window to diaplay framerate.
    m_hFrameRateText = CreateWindowEx(0L, 
                            L"STATIC", 
                            L"    ",
                            WS_CHILD | WS_VISIBLE | SS_LEFT,
                            190, 
                            iYButton+2, 
                            BUTTON_WIDTH, 
                            BUTTON_HEIGHT, 
                            m_hWnd,
                            (HMENU)CamApp_TEXT_ID_FrameRate,
                            m_hInstance,
                            NULL);

    m_uiTimer = SetTimer(m_hWnd, CAMAPP_TIMER, CAMAPP_TIMEOUT, NULL);
    
    //Create a static control for drawing preview picture by GDI functions
    //Currently not supported. 
    /*
    int  iXPos, iYPos;
    int   iWidth, iHeight;
    iXPos = rc.left + SKIN_MARGIN;
    iYPos = rc.top +SKIN_MARGIN;
    iWidth = rc.right -rc.left - SKIN_MARGIN*2;
    iHeight = rc.bottom -rc.top - SKIN_HEIGHT -SKIN_MARGIN;
    m_hWndSC = CreateWindowEx(0, 
                            L"STATIC", 
                            TEXT(""),
                            WS_CHILD ,
                            iXPos,
                            iYPos, 
                            iWidth, 
                            iHeight,
                            m_hWnd,
                            (HMENU)CamApp_STATIC_ID_PREVIEW,
                            m_hInstance, 
                            NULL );
    
    if (NULL == m_hWndSC)
    {
        RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("Init: Create a static control for preview video Failed. Last error code is %d \r\n"), GetLastError()));
        return FALSE;
    }
    RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("Init: Create Static Control Done!\r\n")));
    */

    m_pPreviewStreamDesc = (PCS_STREAM_DESCRIPTOR) malloc(sizeof(CS_STREAM_DESCRIPTOR));
    if (m_pPreviewStreamDesc == NULL) 
    {
        RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("Init: m_pPreviewStreamDesc initialize failed. \r\n")));
        return FALSE;
    }
    memset(m_pPreviewStreamDesc, 0, sizeof(CS_STREAM_DESCRIPTOR));
    
    m_pStillStreamDesc = (PCS_STREAM_DESCRIPTOR)malloc(sizeof(CS_STREAM_DESCRIPTOR));
    if (m_pStillStreamDesc == NULL) 
    {
        RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("Init: m_pStillStreamDesc initialize failed. \r\n")));
        return FALSE;
    }
    memset(m_pStillStreamDesc, 0, sizeof(CS_STREAM_DESCRIPTOR));

    m_pCaptureStreamDesc = (PCS_STREAM_DESCRIPTOR)malloc(sizeof(CS_STREAM_DESCRIPTOR));
    if (m_pCaptureStreamDesc == NULL) 
    {
        RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("Init: m_pCaptureStreamDesc initialize failed. \r\n")));
        return FALSE;
    }
    memset(m_pCaptureStreamDesc, 0, sizeof(CS_STREAM_DESCRIPTOR));

    if (!LoadCamDriver())
        return FALSE;

#ifdef DIRECT_DRAW_MODE
    // Update for compatible with Win Mobile.
    if (OnFlipping( TRUE, TRUE ) == FALSE)
    {
        RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("Init: OnFlipping for preview pin failed.\r\n")));
        return FALSE;
    }
    Sleep(100);
#endif

    // Create Thread
    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
    m_hPreviewThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CamApp_PreviewThread, this, 0, NULL);
    if (m_hPreviewThread == NULL) 
    {
        RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("Init: CreateThread m_hPreviewThread failed. \r\n")));
        return FALSE;
    }

    if (PreviewPinStart() == FALSE)
    {
        RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("Init: Restart preview pin failed.\r\n")));
        return FALSE;
    }

    for (ii=0; ii<SKIN_SIZE; ii++)
    {
        m_binfo[ii].eState = ENABLED;
    }
    UpdateButtons();
    
    return TRUE;
}


/********************************************************************
 *
 *  FUNCTION:       Fini
 *
 *  DESCRIPTION:    Release all Resources acquired by the CCameraWindow class
 *
 *  PARAMETERS:     None
 *
 *  RETURNS:        TRUE if successfully, otherwise return FALSE
 *
 ********************************************************************/
BOOL CCameraWindow::Fini()
{
    DWORD dwExitCode;
    BOOL bResult = TRUE;

    m_bPreviewRunning = FALSE;
    m_bCamAppThreadTerminate = TRUE;

    // Restore the flipping setting to default.
    OnFlipping(TRUE,FALSE);
    OnFlipping(FALSE,FALSE);

    // Restore zoom to default.
    ApplyZoomLevel(1);

    // Restore resolution to default.
    ChangeResolution(QVGA);

    // Restore rotation to default.
    OnRotation(FALSE);
      
    if (NULL != m_uiTimer)
    {
       KillTimer(m_hWnd, m_uiTimer);
       m_uiTimer = NULL;
    }
    
    do 
    {
        Sleep(10);
        if (!GetExitCodeThread(CamApp_PreviewThread, &dwExitCode)) 
        {
             break;
        }
    } while (dwExitCode == STILL_ACTIVE);
    m_hPreviewThread = NULL;
 
    //Stop the pins.
    PreviewPinStop();
    CapturePinStop();
    StillPinStop();
 
    CloseHandle(m_hPreviewPinDll);
    CloseHandle(m_hStillPinDll);
    CloseHandle(m_hCapturePinDll);

    CloseHandle(m_hCameraDll);

    CloseMsgQueue(m_hPreviewMsgQ);
    CloseMsgQueue(m_hStillMsgQ);
    CloseMsgQueue(m_hCaptureMsgQ);

    if (NULL != m_pStillPinBuff)
         free(m_pStillPinBuff);

    if (NULL != m_pCaptureStreamDesc)
         free(m_pCaptureStreamDesc);

    if (NULL != m_pPreviewStreamDesc) 
         free(m_pPreviewStreamDesc);

    if (NULL != m_pStillStreamDesc)
         free(m_pStillStreamDesc);

    if (NULL != m_pCaptureDataRanges)
         free(m_pCaptureDataRanges);

    if (NULL != m_pPreviewDataRanges)
         free(m_pPreviewDataRanges);

    if (NULL != m_pStillDataRanges)
         free(m_pStillDataRanges);

    DeleteCriticalSection(&m_csCapture);
    DeleteCriticalSection(&m_csStill);
    DeleteCriticalSection(&m_csPreview);
    DeleteCriticalSection(&m_csCamera);
    
    // If there is a picture browser dialog, close it
    if (NULL != m_hWndBrowser)
    {
        DestroyWindow(m_hWndBrowser);
        m_hWndBrowser = NULL;
    }

    // If there is a command bar, close it
    if (NULL != m_hWndCB)
    {
        DestroyWindow(m_hWndCB);
        m_hWndCB = NULL;
    }

    // Hide our window (if we have one)
    if (NULL != m_hWnd)
    {
        ::ShowWindow(m_hWnd, SW_HIDE);
        ::UpdateWindow(m_hWnd);
    }

    return bResult;
}


/********************************************************************
 *
 *  FUNCTION:       UpdateButtons
 *
 *  DESCRIPTION:    Updates the Buttons' states

⌨️ 快捷键说明

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