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

📄 camerafunc.cpp

📁 Microsoft WinCE 6.0 BSP FINAL release source code for use with the i.MX27ADS TO2 WCE600_FINAL_MX27_S
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    IOCTLInput.Flags = CSPROPERTY_TYPE_SET;
    
    gPreviewDataFormat.VideoInfoHeader.bmiHeader.biBitCount = 16;
    gPreviewDataFormat.VideoInfoHeader.bmiHeader.biWidth = RECORD_DISPLAY_WIDTH;
    gPreviewDataFormat.VideoInfoHeader.bmiHeader.biHeight = RECORD_DISPLAY_HEIGHT;
    gPreviewDataFormat.VideoInfoHeader.AvgTimePerFrame = 333333;
    gPreviewDataFormat.VideoInfoHeader.dwBitRate = 
            (gPreviewDataFormat.VideoInfoHeader.bmiHeader.biWidth * gPreviewDataFormat.VideoInfoHeader.bmiHeader.biHeight * gPreviewDataFormat.VideoInfoHeader.bmiHeader.biBitCount * 
            (10000000 /(DWORD)gPreviewDataFormat.VideoInfoHeader.AvgTimePerFrame));
    gPreviewDataFormat.VideoInfoHeader.bmiHeader.biSizeImage = 
            (gPreviewDataFormat.VideoInfoHeader.bmiHeader.biWidth * gPreviewDataFormat.VideoInfoHeader.bmiHeader.biHeight * 
            (gPreviewDataFormat.VideoInfoHeader.bmiHeader.biBitCount ))/8;
    
    bResult = DeviceIoControl(ghPreviewPinDll,      // file handle to the driver
             IOCTL_CS_PROPERTY,                        // I/O control code
             &IOCTLInput,                            // in buffer
             sizeof (CSPROPERTY),                   // in buffer size
             &gPreviewDataFormat,                    // out buffer
             sizeof(CS_DATAFORMAT_VIDEOINFOHEADER), // out buffer size
             &dBytesTransferred,                    // number of bytes returned
             NULL);                                    // ignored (=NULL)    
    if (!bResult) {
        RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("LoadCamDriver: set BitCount for preview pin failed!\r\n")));
        return FALSE;
    }

    //set BitCount for capture pin
    gCaptureDataFormat.VideoInfoHeader.bmiHeader.biBitCount = 12;
    gCaptureDataFormat.VideoInfoHeader.bmiHeader.biWidth = CH2_CAPTURE_WIDTH;
    gCaptureDataFormat.VideoInfoHeader.bmiHeader.biHeight = - CH2_CAPTURE_HEIGHT;
    gCaptureDataFormat.VideoInfoHeader.AvgTimePerFrame = 333333;
    gCaptureDataFormat.VideoInfoHeader.dwBitRate = 
            (gCaptureDataFormat.VideoInfoHeader.bmiHeader.biWidth * 
            -1* gCaptureDataFormat.VideoInfoHeader.bmiHeader.biHeight * 
            gCaptureDataFormat.VideoInfoHeader.bmiHeader.biBitCount * 
            (10000000 /(DWORD)gCaptureDataFormat.VideoInfoHeader.AvgTimePerFrame)/100)*100;
    gCaptureDataFormat.VideoInfoHeader.bmiHeader.biSizeImage = 
            (gCaptureDataFormat.VideoInfoHeader.bmiHeader.biWidth * 
            -1 * gCaptureDataFormat.VideoInfoHeader.bmiHeader.biHeight * 
            (gCaptureDataFormat.VideoInfoHeader.bmiHeader.biBitCount))/8;


    bResult = DeviceIoControl(ghCapturePinDll,                  // file handle to the driver
             IOCTL_CS_PROPERTY,                                    // I/O control code
             &IOCTLInput,                                        // in buffer
             sizeof (CSPROPERTY),                                // in buffer size
             &gCaptureDataFormat,                                // out buffer
             sizeof(CS_DATAFORMAT_VIDEOINFOHEADER),             // out buffer size
             &dBytesTransferred,                                // number of bytes returned
             NULL);                                                // ignored (=NULL)    
    if (!bResult) {
        RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("LoadCamDriver: set BitCount for capture pin failed!\r\n")));
        return FALSE;
    }

    CSSTATE state = CSSTATE_PAUSE;
    IOCTLInput.Set = CSPROPSETID_Connection;
    IOCTLInput.Id = CSPROPERTY_CONNECTION_STATE;
    IOCTLInput.Flags = CSPROPERTY_TYPE_SET;
    
    
    //use CSSTATE_PAUSE to allocate the buffer list header for allocating buffer
    bResult = DeviceIoControl(ghPreviewPinDll,                  // file handle to the driver
             IOCTL_CS_PROPERTY,                                    // I/O control code
             &IOCTLInput,                                        // in buffer
             sizeof (CSPROPERTY),                               // in buffer size
             &state,                                            // out buffer
             sizeof(CSSTATE),                                     // out buffer size
             &dBytesTransferred,                                   // number of bytes returned
             NULL);                                               // ignored (=NULL)    */
    if (!bResult) {
        RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("LoadCamDriver: PAUSE preview pin failed!\r\n")));
        return FALSE;
    }
    
    // allocate buffer for preview pin
    //DWORD command = CS_ALLOCATE;
	csBufferInfo.dwCommand = CS_ALLOCATE;
    csBufferInfo.pStreamDescriptor = gpPreviewStreamDesc;
    bResult = DeviceIoControl(ghPreviewPinDll,                  // file handle to the driver
                        IOCTL_CS_BUFFERS,                        // I/O control code
                        &csBufferInfo,                                   // in buffer
                        sizeof(csBufferInfo),                            // in buffer size
                        gpPreviewStreamDesc,                    // out buffer
                        sizeof(CS_STREAM_DESCRIPTOR),            // out buffer size
                        &dBytesTransferred,                      // number of bytes returned
                        NULL);                                    // ignored (=NULL)
    if (!bResult) {
        RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("LoadCamDriver: allocate buffer for preview pin failed!\r\n")));
        return FALSE;
    }
    
    //use CSSTATE_PAUSE to allocate the buffer list header for allocating buffer
    state = CSSTATE_PAUSE;
    IOCTLInput.Set = CSPROPSETID_Connection;
    IOCTLInput.Id = CSPROPERTY_CONNECTION_STATE;
    IOCTLInput.Flags = CSPROPERTY_TYPE_SET;
    bResult = DeviceIoControl(ghCapturePinDll,                  // file handle to the driver
             IOCTL_CS_PROPERTY,                                    // I/O control code
             &IOCTLInput,                                        // in buffer
             sizeof (CSPROPERTY),                               // in buffer size
             &state,                                            // out buffer
             sizeof(CSSTATE),                                     // out buffer size
             &dBytesTransferred,                                   // number of bytes returned
             NULL);                                               // ignored (=NULL)    */
    if (!bResult) {
        RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("LoadCamDriver: PAUSE STILL pin failed!\r\n")));
        return FALSE;
    }

    // allocate buffer for capture pin
    //command = CS_ALLOCATE;
	csBufferInfo.dwCommand = CS_ALLOCATE;
    csBufferInfo.pStreamDescriptor = gpCaptureStreamDesc;
    bResult = DeviceIoControl(ghCapturePinDll,                  // file handle to the driver
                        IOCTL_CS_BUFFERS,                      // I/O control code
                        &csBufferInfo,                                 // in buffer
                        sizeof(csBufferInfo),                          // in buffer size
                        gpCaptureStreamDesc,                    // out buffer
                        sizeof(CS_STREAM_DESCRIPTOR),          // out buffer size
                        &dBytesTransferred,                    // number of bytes returned
                        NULL);                                  // ignored (=NULL)
    if (!bResult) {
        RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("LoadCamDriver: allocate buffer for preview pin failed!\r\n")));
        return FALSE;
    }
#endif   
    //change the framerate to 30fps
    CSPROPERTY_VIDEOCONTROL_FRAME_RATES_S CsPropVideoControlFrameRateInput;
    CSPROPERTY_VIDEOCONTROL_FRAME_RATES_S CsPropVideoControlFrameRateNew;

    // count real frame rate
    CsPropVideoControlFrameRateInput.Property.Set = PROPSETID_VIDCAP_VIDEOCONTROL;
    CsPropVideoControlFrameRateInput.Property.Id = CSPROPERTY_VIDEOCONTROL_FRAME_RATES;
    CsPropVideoControlFrameRateInput.Property.Flags = CSPROPERTY_TYPE_GET;
    CsPropVideoControlFrameRateInput.StreamIndex = PREVIEW; 
    CsPropVideoControlFrameRateInput.RangeIndex = 0;

    bResult = DeviceIoControl(ghCameraDll,                    // file handle to the driver
            IOCTL_CS_PROPERTY,                                 // I/O control code
            &CsPropVideoControlFrameRateInput,                 // in buffer
            sizeof (CSPROPERTY_VIDEOCONTROL_FRAME_RATES_S),    // in buffer size
            &CsPropVideoControlFrameRateNew,                   // out buffer
            sizeof (CSPROPERTY_VIDEOCONTROL_FRAME_RATES_S),    // out buffer size
            &dBytesTransferred,                               // number of bytes returned
            NULL);                                             // ignored (=NULL)    

    if (!bResult) {
        RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("ChangeFrameRate:Getting frame rate failed.\r\n")));
        return FALSE;
    }

    CsPropVideoControlFrameRateNew.RangeIndex = FRAMERATE_333000;       
    CsPropVideoControlFrameRateNew.Property.Flags = CSPROPERTY_TYPE_SET;

    bResult = DeviceIoControl(ghCameraDll,                    // file handle to the driver
            IOCTL_CS_PROPERTY,                                 // I/O control code
            &CsPropVideoControlFrameRateNew,                   // in buffer
            sizeof (CSPROPERTY_VIDEOCONTROL_FRAME_RATES_S),    // in buffer size
            &CsPropVideoControlFrameRateNew,                   // out buffer
            sizeof (CSPROPERTY_VIDEOCONTROL_FRAME_RATES_S),    // out buffer size
            &dBytesTransferred,                               // number of bytes returned
            NULL);                                             // ignored (=NULL)    

    if (!bResult) {
        RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("ChangeFrameRate:Changing frame rate failed.\r\n")));
        return FALSE;
    }

    RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("LoadCamDriver: LoadCamDriver Done!\r\n")));

    return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: StartCameraView
//
// Start the camera preview pin.
//
// Parameters:
//      None
//
// Returns:  
//      TRUE if successful; FALSE if failed.
//
//----------------------------------------------------------------------------
BOOL StartCameraView()
{
    CSPROPERTY IOCTLInput;
    CSSTATE state;
    DWORD dwBytesTransferred;
    BOOL bResult;

    EnterCriticalSection(&gcsPreview);
    RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("StartCameraView+\r\n")));

    IOCTLInput.Set = CSPROPSETID_Connection;
    IOCTLInput.Id = CSPROPERTY_CONNECTION_STATE;
    IOCTLInput.Flags = CSPROPERTY_TYPE_SET;
    state = CSSTATE_PAUSE;

    bResult = DeviceIoControl(ghPreviewPinDll,                  // file handle to the driver
                    IOCTL_CS_PROPERTY,                            // I/O control code
                    &IOCTLInput,                                // in buffer
                    sizeof(CSPROPERTY),                          // in buffer size
                    &state,                                        // out buffer
                    sizeof(CSSTATE),                            // out buffer size
                    &dwBytesTransferred,                        // number of bytes returned
                    NULL);                                        // ignored (=NULL)
    if (!bResult) {
        LeaveCriticalSection(&gcsPreview);
        RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("StartCameraView: PAUSE preview pin failed!\r\n")));
        return FALSE;
    }
    
    state = CSSTATE_RUN;
    bResult = DeviceIoControl(ghPreviewPinDll,        // file handle to the driver
                    IOCTL_CS_PROPERTY,                // I/O control code
                    &IOCTLInput,                    // in buffer
                    sizeof(CSPROPERTY),             // in buffer size
                    &state,                            // out buffer
                    sizeof(CSSTATE),                // out buffer size
                    &dwBytesTransferred,            // number of bytes returned
                    NULL);                            // ignored (=NULL)
    if (!bResult) {
        LeaveCriticalSection(&gcsPreview); 
        RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("StartCameraView: RUN preview pin failed!\r\n")));
        return FALSE;
    }

    LeaveCriticalSection(&gcsPreview);    
    RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("StartCameraView-\r\n")));
    return TRUE;

}
//-----------------------------------------------------------------------------
//
// Function: StopCameraView
//
// Stop the camera preview pin.
//
// Parameters:
//      None
//
// Returns:  
//      TRUE if successful; FALSE if failed.
//
//----------------------------------------------------------------------------
BOOL StopCameraView()
{
    CSPROPERTY IOCTLInput;
    CSSTATE state;
    DWORD dwBytesTransferred;

    EnterCriticalSection(&gcsPreview);
    RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("StopCameraView+\r\n")));


    IOCTLInput.Set = CSPROPSETID_Connection;
    IOCTLInput.Id = CSPROPERTY_CONNECTION_STATE;
    IOCTLInput.Flags = CSPROPERTY_TYPE_SET;
    state = CSSTATE_STOP;

    BOOL bResult = DeviceIoControl(ghPreviewPinDll,   // file handle to the driver
                        IOCTL_CS_PROPERTY,            // I/O control code
                        &IOCTLInput,                  // in buffer
                        sizeof(CSPROPERTY),           // in buffer size
                        &state,                       // out buffer
                        sizeof(CSSTATE),              // out buffer size
                        &dwBytesTransferred,          // number of bytes returned
                        NULL);                        // ignored (=NULL)
    if (!bResult) {
        LeaveCriticalSection(&gcsPreview);
        RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("StopCameraView: STOP preview pin failed!\r\n")));
        return FALSE;
    }

    LeaveCriticalSection(&gcsPreview);
    RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("StopCameraView-\r\n")));
    return TRUE;

}
//-----------------------------------------------------------------------------
//
// Function: PerFrameCameraView
//
// Output one frame through preview pin from camera
//
// Parameters:
//      pbyDstBuffer
//            [in]the address of output data buffer
//
// Returns:  
//      TRUE if successful; FALSE if failed.
//
//----------------------------------------------------------------------------
BOOL PerFrameCameraView(UINT8 * pbyDstBuffer)
{
    DWORD dBytesTransferred;
    //UINT16 displayHeight;
    //UINT16 displayWidth;
    CS_MSGQUEUE_BUFFER CsMsgQBuff;
    DWORD size,  flags;
    UINT8 *pbySrcBuffer;
    BOOL bResult = FALSE;

    EnterCriticalSection(&gcsPreview); 

    DWORD command = CS_ENQUEUE;
    while(1) {
        bResult = DeviceIoControl(ghPreviewPinDll,                 // file handle to the driver
                                   IOCTL_CS_BUFFERS,               // I/O control code
                                   &command,                       // in buffer
                                   sizeof(DWORD),                  // in buffer size
                                   gpPreviewStreamDesc,             // out buffer
                                   sizeof(CS_STREAM_DESCRIPTOR),   // out buffer size
                                   &dBytesTransferred,             // number of bytes returned
                                   NULL);                          // ignored (=NULL)
        if (!bResult){
            if(GetLastError()!= ERROR_SERVICE_NOT_ACTIVE) { // 
                LeaveCriticalSection(&gcsPreview);
                RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("PerFrameCameraView: Reading frame failed!\r\n")));
                return FALSE;
            }
            else
				        Sleep(500);
            continue;
		    }		
		    break;
    }
    if (WaitForSingleObject(ghPreviewMsgQ, INFINITE) != WAIT_OBJECT_0) {
        LeaveCriticalSection(&gcsPreview);
        RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("PerFrameCameraView: WaitForSingleObject Timeout!\r\n")));
        return FALSE;
    }
    if(!ReadMsgQueue(ghPreviewMsgQ, &CsMsgQBuff, sizeof(CS_MSGQUEUE_BUFFER), &size, 10, &flags)) {
        LeaveCriticalSection(&gcsPreview);
        RETAILMSG(CodecDemo_ERROR_MSG,(TEXT("PerFrameCameraView: unable to read preview message queue. Try again.\r\n")));
        return FALSE;
    }
    pbySrcBuffer = reinterpret_cast<PUINT8>(CsMsgQBuff.pStreamDescriptor->CsStreamHeader.Data);

    LeaveCriticalSection(&gcsPreview);
    memcpy(pbyDstBuffer,pbySrcBuffer,RECORD_DISPLAY_WIDTH * RECORD_DISPLAY_HEIGHT * CH1_CAPTURE_BYTE_PER_PIXEL);
    return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: StartCameraCapture
//
// Start the camera capture pin.
//
// Parameters:
//      [In] bUsePhyAddr is FLASE for DS, TRUE for customized method to get physical
//           address of YUV data captured by camera 
//
// Returns:  
//      TRUE if successful; FALSE if failed.
//
//----------------------------------------------------------------------------
BOOL StartCameraCapture(BOOL bUsePhyAddr)
{
    CSPROPERTY IOCTLInput;
    CSSTATE state;

⌨️ 快捷键说明

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