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

📄 testov511dlg.cpp

📁 evc 界面 wince 5.0 ov511 usb camera 驱动原代码
💻 CPP
字号:
// TestOv511Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestOv511.h"
#include "TestOv511Dlg.h"

#include <winioctl.h>                 // For all that Windows stuff

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestOv511Dlg dialog

CTestOv511Dlg::CTestOv511Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestOv511Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestOv511Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestOv511Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestOv511Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestOv511Dlg, CDialog)
	//{{AFX_MSG_MAP(CTestOv511Dlg)
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_BN_CLICKED(IDC_CLOSE, OnClose)
	ON_BN_CLICKED(IDC_LED_ON, OnLedOn)
	ON_BN_CLICKED(IDC_LED_OFF, OnLedOff)
	ON_BN_CLICKED(IDC_LED_READ, OnLedRead)
	ON_BN_CLICKED(IDC_INIT_ISOC, OnInitIsoc)
	ON_BN_CLICKED(IDC_START_CAPTURE, OnStartCapture)
	ON_BN_CLICKED(IDC_STOP_CAPTURE, OnStopCapture)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestOv511Dlg message handlers

BOOL CTestOv511Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	hCam=INVALID_HANDLE_VALUE;

	pIsochData=(unsigned char*)malloc(240250);
	pRawFrameData=(unsigned char*)malloc(115223);
	pRealFrameData=(unsigned char*)malloc(115712);
	pBmpData=(unsigned char*)malloc(230400);

	//调整窗口
	CWnd *wnd,*bwnd;
	CRect rect,brect;
	wnd=this->GetDlgItem(IDC_REMOTEVIDEO);	// Video display window
   	bwnd=this->GetDlgItem(IDC_REMOTEBORDER); // Border window...
   	bwnd->GetWindowRect(brect);
	ScreenToClient(brect);

	remote_wnd_x=brect.TopLeft().x+(brect.Width()-IMAGE_WIDTH)/2;
	remote_wnd_y=brect.TopLeft().y+(brect.Height()-IMAGE_HEIGHT)/2;
	
	// Centre the remote video window
	wnd->SetWindowPos(&wndTop,remote_wnd_x-4,remote_wnd_y-4,IMAGE_WIDTH+9,IMAGE_HEIGHT+9,SWP_SHOWWINDOW | SWP_DRAWFRAME);

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CTestOv511Dlg::OnOpen() 
{
	hCam=CreateFile (TEXT("CAM1:"), GENERIC_WRITE | GENERIC_READ, 
		0, NULL, OPEN_EXISTING, 0, NULL);
	if (hCam == INVALID_HANDLE_VALUE)
	{
		AfxMessageBox(L"无法打开!");
	}
}

void CTestOv511Dlg::OnClose() 
{
	CloseHandle(hCam);
	hCam=INVALID_HANDLE_VALUE;
}

void CTestOv511Dlg::OnLedOn() 
{
	DWORD dwBytes;

	DeviceIoControl (hCam, IOCTL_CAMERA_DEVICE_LED_ON, 
							 0, 0, 0, 0, &dwBytes, NULL);
}

void CTestOv511Dlg::OnLedOff() 
{
	DWORD dwBytes;

	DeviceIoControl (hCam, IOCTL_CAMERA_DEVICE_LED_OFF, 
							 0, 0, 0, 0, &dwBytes, NULL);
}

void CTestOv511Dlg::OnLedRead() 
{
	DWORD dwBytes;

	BYTE Out;
	DWORD dwOut=1;

	DeviceIoControl (hCam, IOCTL_CAMERA_DEVICE_LED_READ, 
							 0, 0, &Out, dwOut, &dwBytes, NULL);

	CString inf;
	inf.Format(L"Led Status:%d",Out);
	AfxMessageBox(inf);
}

void CTestOv511Dlg::OnInitIsoc() 
{
	DWORD dwBytes;

	DeviceIoControl (hCam, IOCTL_CAMERA_DEVICE_START_CAPTURE, 
							 0, 0, 0, 0, &dwBytes, NULL);

	DeviceIoControl (hCam, IOCTL_CAMERA_DEVICE_CAPTURE_ONE_FRAME, 
							 0, 0,
							 pIsochData, 240250,  &dwBytes, NULL);

	/*
	CString inf;
	inf.Format(L"%d Bytes Aquired!",dwBytes);
	AfxMessageBox(inf);*/

	if(dwBytes>232562)
	{
		PBYTE ptemp = pIsochData;
		PBYTE ptr = pRawFrameData;
		for(DWORD j = 0; j < dwBytes; j++)
			{
				//查找帧开始标志
				int k = (ptemp[0] | ptemp[1] | ptemp[2] | ptemp[3] | 
					 ptemp[4] | ptemp[5] | ptemp[6] | ptemp[7]);
				int k1 = (ptemp[8] & 0xff);
				int k2 = (ptemp[8] & 0xff);
				if (k == 0 && (k1 == 24 || k2 ==25))
					 break;
				ptemp++;
			}
			ptemp += 9;
			//复制一帧原始数据,同时去除帧内的包序号
			memmove(ptr, ptemp, 951);    //复制第一包
			ptr += 951;
			ptemp += 952;
			for(int k = 0; k < 119; k++)     //复制其余包
			{
				memmove(ptr, ptemp, 960); 
				ptr += 960;
				ptemp += 961;
			}
			memmove(ptr, ptemp, 32);			
	}

	YUV2RGB(0, 76800, 96000, 320, 240, 300);

	//显示出来
	CBitmap bitmap;
	
	bitmap.CreateBitmap(320,240,1,24,pBmpData);

	CWnd* pWnd=GetDlgItem(IDC_REMOTEVIDEO);
	pWnd->UpdateWindow();

	CDC* pDC=pWnd->GetDC();
	CDC bitmapDC;
	
	bitmapDC.CreateCompatibleDC(pDC);
	CBitmap* pOldBitmap = bitmapDC.SelectObject(&bitmap);
	//pDC->StretchBlt(0,0,176,144,&bitmapDC,0,0,176,144,SRCCOPY);
	pDC->BitBlt(0,0,320,240,&bitmapDC,0,0,SRCCOPY);
	bitmapDC.SelectObject(pOldBitmap);
	bitmapDC.DeleteDC();
	bitmap.DeleteObject();


}

void CTestOv511Dlg::YUV2RGB(int yy, int uu, int vv, int width, int height, int SegNum)
{
	unsigned char y, u, v, seg[384];
	unsigned char *lp_y, *lp_u, *lp_v, *pt;
	int r,g,b;
	int i,j,iSegY = 0, iY, jY , iOutYP, iOutY, iUV, jUV, iOutUVP, iOutUV;
	
	//以块为单位,转换成标准的yuv420
	pt = pRawFrameData;
    for(i = 0; i < SegNum; i++)
	{
		memmove(seg, pt, 384); 
		pt += 384;
		iY = iSegY / 10;
		jY = iSegY - iY * 10;
		iOutYP = iY*8*320 + jY*32;
		iOutY = iOutYP * 3;
		iUV = iSegY / (10 * 2);
		jUV = iSegY - iUV * (10 * 2);
		iOutUVP = iUV*8*2*320 + jUV*16;
		iOutUV = iOutUVP * 3;
		ov511_parse_data_yuv420(seg, pRealFrameData, iOutYP, iUV*8*160 + jUV*8,
			320, 240);
		iSegY++;
	}
	
	
	
	//将yuv420转换成rgb24
	lp_y = pRealFrameData + yy;
	lp_u = pRealFrameData + uu;
	lp_v = pRealFrameData + vv;
	
	
	//YUV转换成RGB
	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		{
			y=*(lp_y+i*width+j);
			u=*(lp_u+(int)(i/2)*width/2+(int)(j/2));
			v=*(lp_v+(int)(i/2)*width/2+(int)(j/2));
			
			//YUV-》RGB 顺序:b,g,r		
			r=(int)(y+1.402*(v-128));
			g=(int)(y-0.34414*(u-128)-0.71414*(v-128));
			b=(int)(y+1.772*(u-128));
			
			*(pBmpData+(height-i-1)*width*3+j*3+2)=mmin(255,mmax(0,r));
			*(pBmpData+(height-i-1)*width*3+j*3+1)=mmin(255,mmax(0,g));
			*(pBmpData+(height-i-1)*width*3+j*3)=mmin(255,mmax(0,b));
			
		}
	}
}

void CTestOv511Dlg::ov511_parse_data_yuv420(unsigned char *pIn0,unsigned char *pOut0, int iOutY, int iOutUV, int iWidth, int iHeight)
{
	int k, l, m;
	unsigned char *pIn;
	unsigned char *pOut, *pOut1;
	unsigned a = iWidth * iHeight;
	unsigned w = iWidth / 2;

	pIn = pIn0;
	pOut = pOut0 + iOutUV + a;
	for (k = 0; k < 8; k++) {
		pOut1 = pOut;
		for (l = 0; l < 8; l++) *pOut1++ = *pIn++;
		pOut += w;
	}

	pIn = pIn0 + 64;
	pOut = pOut0 + iOutUV + a + a/4;
	for (k = 0; k < 8; k++) {
		pOut1 = pOut;
		for (l = 0; l < 8; l++) *pOut1++ = *pIn++;
		pOut += w;
	}

	pIn = pIn0 + 128;
	pOut = pOut0 + iOutY;
	for (k = 0; k < 4; k++) {
		pOut1 = pOut;
		for (l = 0; l < 8; l++) {
			for (m = 0; m < 8; m++)
				*pOut1++ =*pIn++;
			pOut1 += iWidth - 8;
		}
		pOut += 8;
	}
}

void CTestOv511Dlg::OnStartCapture() 
{
	DWORD dwBytes;

	DeviceIoControl (hCam, IOCTL_CAMERA_DEVICE_START_CAPTURE, 
							 0, 0, 0, 0, &dwBytes, NULL);
}

void CTestOv511Dlg::OnStopCapture() 
{
	DWORD dwBytes;

	DeviceIoControl (hCam, IOCTL_CAMERA_DEVICE_STOP_CAPTURE, 
							 0, 0, 0, 0, &dwBytes, NULL);
}

void CTestOv511Dlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	free(pIsochData);
	free(pRawFrameData);
	free(pRealFrameData);
	free(pBmpData);
}

⌨️ 快捷键说明

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