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

📄 ddcam.cpp

📁 PXA270 平台 Windows Mobile 5 摄像头驱动
💻 CPP
📖 第 1 页 / 共 2 页
字号:
   // Get COM interfaces

	if(FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
    {
        RETAILMSG(1, (TEXT("CoInitialize Failed!\r\n")));
        return FALSE;
    }

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WCETEST);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

    // Finished with COM
    CoUninitialize();

	return msg.wParam;
}


//
//  FUNCTION: InitInstance(HANDLE, int)
//
//  PURPOSE: Saves instance handle and creates main window
//
//  COMMENTS:
//
//    In this function, we save the instance handle in a global variable and
//    create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HRESULT hr;
	BOOL fActivated;
	TCHAR	szTitle[MAX_LOADSTRING];			// The title bar text
	TCHAR	szWindowClass[MAX_LOADSTRING];		// The window class name

	hInst = hInstance;		// Store instance handle in our global variable
	// Initialize global strings
	LoadString(hInstance, IDC_WCETEST, szWindowClass, MAX_LOADSTRING);
	//MyRegisterClass(hInstance, szWindowClass);
	WNDCLASS	wc;

    wc.style			= CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc		= (WNDPROC) WndProc;
    wc.cbClsExtra		= 0;
    wc.cbWndExtra		= 0;
    wc.hInstance		= hInstance;
    wc.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WCETEST));
    wc.hCursor			= 0;
    wc.hbrBackground	= (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName		= 0;
    wc.lpszClassName	= szWindowClass;

	RegisterClass(&wc);

	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);

    if(FAILED(ActivatePreviousInstance(szWindowClass, szTitle, &fActivated)) ||
            fActivated)
    {
        return(0);
    }

   // Create the main window.    
   //         WS_CLIPCHILDREN, // Setting this to 0 gives a default style we don't want.  Use a benign style bit instead.
  
	hWndApp = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

	if (!hWndApp)
	{	
		return FALSE;
	}

	ShowWindow(hWndApp, nCmdShow);
	UpdateWindow(hWndApp);

	hr = InitCapFilter();
	if (SUCCEEDED(hr))
	{	OpenCamera(NULL,false,  //Capture
		            true,  //Still
					true    //Preview
			);
		StartPreview();
		gcap.pVWS->put_WindowState(SW_HIDE);		
	}
	else
	{
		RETAILMSG(1,(TEXT("CamTest: Fail to create Capture filter. \r\n")));  
	}

	return TRUE;
}

BOOL ConvertGuidToString(GUID  * guid,unsigned short *str)
{
    // ConvertStringToGuid
    // this routine converts a string into a GUID and returns TRUE if the
    // conversion was successful.

    // Local variables.
    int  i;
    
	for (i = 0; i<MLIST_SIZE; i++)
    
    {   if (IsEqualGUID(*media_list[i].guid,*guid) )
		{			
			memcpy(str,media_list[i].text,6*sizeof(unsigned short)); //len = 6  :)
			return TRUE;
		}
	}    

    return FALSE;
}


/**************************************************************************************

   OnCreate

 **************************************************************************************/
LRESULT OnCreate(
    HWND hwnd,
    CREATESTRUCT* lParam
    )
{
    // create the menu bar
    SHMENUBARINFO mbi;
    ZeroMemory(&mbi, sizeof(SHMENUBARINFO));
    mbi.cbSize = sizeof(SHMENUBARINFO);
    mbi.hwndParent = hwnd;
    mbi.nToolBarId = IDM_MENU;
    mbi.hInstRes = hInst;
	mbi.dwFlags = SHCMBF_HMENU;

    if(!SHCreateMenuBar(&mbi))
    {
        // Couldn't create the menu bar.  Fail creation of the window.
        return(-1);
    }

	hWndMenu = mbi.hwndMB; 

	//hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDM_MENU)); //CommandBar_GetMenu(hWndMenu, 0);
	hMenu = GetSystemMenu(hWndMenu,FALSE);
			
	gcap.fPreviewing = FALSE;
	gcap.fStillCapturing  = FALSE;

    return(0); // continue creation of the window
}





//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
/**************************************************************************************

   WndProc

 **************************************************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    LRESULT lResult = TRUE;
	HRESULT hr;
	unsigned char buf;
	long value,flag=CameraControl_Flags_Manual;


//	cbInput = sizeof(CameraApp);
//	cbOutput = 0;

    switch(message)
    {
		case WM_CHAR:
			buf = (unsigned char) wParam;
			//RETAILMSG(1,(TEXT("Keypress 0x%x \r\n"), buf));

			switch(buf)
			{
				// Button "1"
			case 0x31:
			case 0x6b:
				
				// Select format 1280*960; 
				RETAILMSG(1,(TEXT("Capture type: SXGA\r\n")));
				
				break;

			// Button "2"
			case 0x32:
			case 0x6c:
				
				//Select format 640*480
				RETAILMSG(1,(TEXT("Capture type: VGA\r\n")));
				
				break;
			
			// Button "3"
			case 0x33:
			case 0x6d:				
				//Select format 320*240	
				RETAILMSG(1,(TEXT("Capture type: QVGA\r\n")));
				break;

			// Button "4" Increase Brightness (exposure)
			case 0x34:
				
				exposure++;
				if (exposure > 6)
					exposure = 6;
				value = exposure;
				hr = gcap.pCamControl->Set(CameraControl_Exposure, value,flag);
				
				break;
			// Button "5" Special Mode +
			case 0x35:
				break;
			// Button "6" Increase Contrast
			case 0x36:
				break;
			// Button "7" Decrease Brightness (exposure)
			case 0x37:
				
				exposure--;
				if (exposure < 0)
					exposure = 0;
				value = exposure;
				hr = gcap.pCamControl->Set(CameraControl_Exposure, value,flag);
				//SetExposure(exposure);				
				break;
			// Button "8" Special Mode -
			case 0x38:
				break;
			// Button "9" Decrease Contrast
			case 0x39:
				break;
			// Button "0" Take Snapshot
			case 0x30:
				//do_still_capture = TRUE;
				break;

			default:
				break;
			}

		break;

        case WM_CREATE:
            lResult = OnCreate(hWnd, (CREATESTRUCT*)lParam);
            break;

        case WM_COMMAND:
            switch (wParam)
            {
				case ID_STILLCAPTURE:					   
					CaptureStillImage();					
					//ReadFrameFromFile();
					break;
                case ID_CONTROL_EXIT:
					StopPreview();		
					CloseCamera();
	                DestroyWindow(hWnd);
	
				    break;

				case ID_CAMERA_START:
					StartPreview();
					break;
				case ID_CAMERA_STOP:
					StopPreview();
					break;

				default:
                    goto DoDefault;
            }
            break;

        case WM_PAINT:
            {
                HDC hdc;
                PAINTSTRUCT ps;
                RECT rect;

                hdc = BeginPaint(hWnd, &ps);
                GetClientRect(hWnd, &rect);

                EndPaint (hWnd, &ps);
            }
            break;

        case WM_DESTROY:
			StopPreview();		
			CloseCamera();
            DestroyWindow(hWnd);
            PostQuitMessage(0);
            break;

        DoDefault:
        default:
            lResult = DefWindowProc(hWnd, message, wParam, lParam);
            break;
    }

    return(lResult);
}


void WriteBMPToDisk(unsigned char *pStillImageBuffer)
{
	DWORD dwSize;
	HANDLE hFile;
	unsigned short *x;
	BOOL rc = TRUE;	

	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;

	UpdatePictureNumber();
	x = (unsigned short*)malloc(256);	
	bitmapSize = still_size;
	
    //pStillImageBuffer = new unsigned char[bitmapSize];
	if (pStillImageBuffer)
	{			  	 
	   wsprintf((unsigned short *)x, TEXT("%s\\image%d.bmp"), picture_path, picture_number++);
	   hFile = CreateFile(x, 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")));
	}

	free((void *)x);
	
}

// Look for cam.cfg
// If it doesn't exist, create it, and set picture number to 1.
// If it exists, read the value stored inside, increment the number, and write it back.
void UpdatePictureNumber()
{
	DWORD dwSize;
	HANDLE hFile;
	char *buffer;

	buffer = (char *)malloc(1024);

	hFile = CreateFile(TEXT("\\temp\\cam.cfg"), GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

	dwSize = 0;

	if (hFile == INVALID_HANDLE_VALUE)
	{
		// File did not exist, so we are going to create it, and initialize the counter.
		picture_number = 1;
		hFile = CreateFile(TEXT("\\temp\\cam.cfg"), GENERIC_WRITE | GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
		buffer[0] = picture_number & 0x00FF;
		buffer[1] = (picture_number & 0xFF00) >> 8;

		WriteFile(hFile, buffer, 2, &dwSize, NULL);
		CloseHandle(hFile);
	} else
	{
		dwSize = 0;
		ReadFile(hFile, buffer, 2, &dwSize, NULL);

		picture_number = buffer[1];
		picture_number <<= 8;
		picture_number |= buffer[0];
		picture_number++;

		CloseHandle(hFile);

		hFile = CreateFile(TEXT("\\temp\\cam.cfg"), GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		buffer[0] = picture_number & 0x00FF;
		buffer[1] = (picture_number & 0xFF00) >> 8;
		dwSize = 0;
		WriteFile(hFile, buffer, 2, &dwSize, NULL);
		CloseHandle(hFile);
	}

	free(buffer);
}


/****************************************************************************

   ActivatePreviousInstance

  ****************************************************************************/
HRESULT ActivatePreviousInstance(
    const TCHAR* pszClass,
    const TCHAR* pszTitle,
    BOOL* pfActivated
    )
{
    HRESULT hr = S_OK;
    int cTries;
    HANDLE hMutex = NULL;

    *pfActivated = FALSE;
    cTries = 5;
    while(cTries > 0)
    {
        hMutex = CreateMutex(NULL, FALSE, pszClass); // NOTE: We don't want to own the object.
        if(NULL == hMutex)
        {
            // Something bad happened, fail.
            hr = E_FAIL;
            goto Exit;
        }

        if(GetLastError() == ERROR_ALREADY_EXISTS)
        {
            HWND hwnd;

            CloseHandle(hMutex);
            hMutex = NULL;

            // There is already an instance of this app
            // running.  Try to bring it to the foreground.

            hwnd = FindWindow(pszClass, pszTitle);
            if(NULL == hwnd)
            {
                // It's possible that the other window is in the process of being created...
                Sleep(500);
                hwnd = FindWindow(pszClass, pszTitle);
            }

            if(NULL != hwnd) 
            {
                // Set the previous instance as the foreground window

                // The "| 0x01" in the code below activates
                // the correct owned window of the
                // previous instance's main window.
                SetForegroundWindow((HWND) (((ULONG) hwnd) | 0x01));

                // We are done.
                *pfActivated = TRUE;
                break;
            }

            // It's possible that the instance we found isn't coming up,
            // but rather is going down.  Try again.
            cTries--;
        }
        else
        {
            // We were the first one to create the mutex
            // so that makes us the main instance.  'leak'
            // the mutex in this function so it gets cleaned
            // up by the OS when this instance exits.
            break;
        }
    }

    if(cTries <= 0)
    {
        // Someone else owns the mutex but we cannot find
        // their main window to activate.
        hr = E_FAIL;
        goto Exit;
    }

Exit:
    return(hr);
}


⌨️ 快捷键说明

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