📄 cameravideodlg.cpp
字号:
// CameraVideoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CameraVideo.h"
#include "CameraVideoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
// Global varialbes...
int count=0;
int retvalue=0;
unsigned char cdata[10000];
unsigned char vdata[30000];
int cbuffer_size=10000;
unsigned char rgbdata[80000];
int buffersize=80000;
// Callback function gets invoked during compression...
// It returns the compressed data byte by byte...
void OwnWriteFunction(int byte)
{
if(count<cbuffer_size)
cdata[count]=(unsigned char)byte;
count++;
}
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCameraVideoDlg dialog
CCameraVideoDlg::CCameraVideoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCameraVideoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCameraVideoDlg)
// 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 CCameraVideoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCameraVideoDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCameraVideoDlg, CDialog)
//{{AFX_MSG_MAP(CCameraVideoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_START, OnStart)
ON_WM_DESTROY()
ON_MESSAGE(WM_NET_DATA_ARRIVE,OnDataArrivedMsg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCameraVideoDlg message handlers
BOOL CCameraVideoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
// TODO: Add extra initialization here
// Prepare for capture video from webcam...
vidcap=new VideoCapture();
vidcap->SetDialog(this);
if(vidcap->Initialize()) // success
{
this->m_bmpinfo=&vidcap->m_bmpinfo;
isVideo=TRUE;
// Initialize the compressor
// Initialize table for RGB to YUV conversion
InitLookupTable();
// Initialize the compressor
cparams.format = CPARAM_QCIF;
InitH263Encoder(&cparams);
// Set up the callback function
WriteByteFunction = OwnWriteFunction;
}
else
{
isVideo=FALSE;
// Setup bmpinfo structure yourself
m_bmpinfo=new BITMAPINFO;
m_bmpinfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
m_bmpinfo->bmiHeader.biWidth=IMAGE_WIDTH;
m_bmpinfo->bmiHeader.biHeight=IMAGE_HEIGHT;
m_bmpinfo->bmiHeader.biPlanes=1;
m_bmpinfo->bmiHeader.biBitCount=24;
m_bmpinfo->bmiHeader.biCompression=0;
m_bmpinfo->bmiHeader.biSizeImage=0;
m_bmpinfo->bmiHeader.biXPelsPerMeter=0;
m_bmpinfo->bmiHeader.biYPelsPerMeter=0;
m_bmpinfo->bmiHeader.biClrUsed=0;
m_bmpinfo->bmiHeader.biClrImportant=0;
}
// Capturing is not yet started...
isCaptureStarted=FALSE;
isViewLocalVideo=TRUE;
// Initialize decompressor
InitH263Decoder();
// Adjust display windows
CWnd *wnd,*bwnd;
CRect rect,brect;
// For local video display window
wnd=this->GetDlgItem(IDC_LOCALVIDEO);
bwnd=this->GetDlgItem(IDC_LOCALBORDER);
bwnd->GetWindowRect(brect);
ScreenToClient(brect);
local_wnd_x=brect.TopLeft().x+(brect.Width()-IMAGE_WIDTH)/2;
local_wnd_y=brect.TopLeft().y+(brect.Height()-IMAGE_HEIGHT)/2;
// Centre the local video window
wnd->SetWindowPos(&wndTop,local_wnd_x-4,local_wnd_y-4,IMAGE_WIDTH+9,IMAGE_HEIGHT+9,SWP_SHOWWINDOW | SWP_DRAWFRAME);
// For remote video display window
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);
// Get Dialog DC
m_hdc=this->GetDC()->m_hDC;
// Initialize DIB for drawing...
hdib=::DrawDibOpen();
if(hdib!=NULL)
{
::DrawDibBegin(hdib,
m_hdc,
-1, // don't stretch
-1, // don't stretch
&m_bmpinfo->bmiHeader,
IMAGE_WIDTH, // width of image
IMAGE_HEIGHT, // height of image
0
);
}
m_pServer=new CIocpModeSvr;
m_bRequest=FALSE;
return TRUE; // return TRUE unless you set the focus to a control
}
void CCameraVideoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CCameraVideoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCameraVideoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCameraVideoDlg::SendVideo(BYTE *data,int size)
{
CRect wndrect;
Bits bits;
if(vidcap->isOver==TRUE)
return;
if(data==NULL)
return;
// Display the captured frame...
if(isViewLocalVideo)
{
::DrawDibDraw(hdib,
m_hdc,
local_wnd_x, // dest : left pos
local_wnd_y, // dest : top pos
-1, // don't zoom x
-1, // don't zoom y
&m_bmpinfo->bmiHeader, // bmp header info
data, // bmp data
0, // src :left
0, // src :top
IMAGE_WIDTH, // src : width
IMAGE_HEIGHT, // src : height
DDF_SAME_DRAW // use prev params....
);
}
//进行H263压缩
//log.WriteString("\n Converting to YUV format..");
//Convert the data from rgb format to YUV format
ConvertRGB2YUV(IMAGE_WIDTH,IMAGE_HEIGHT,data,yuv);
// Reset the counter
count=0;
//Compress the data...to h263
cparams.format=CPARAM_QCIF;
cparams.inter = CPARAM_INTRA;
cparams.Q_intra = 3;
cparams.data=yuv; // Data in YUV format...
CompressFrame(&cparams, &bits);
retvalue=count;
for(int i=0;i<retvalue;i++)
{
vdata[i]=cdata[i];
}
DisplayRemoteFrame(vdata,retvalue);
if(m_bRequest)
{
TRACE("SendData!\n");
m_pServer->SendMsgToAll((char*)vdata,retvalue);
m_bRequest=FALSE;
}
}
void CCameraVideoDlg::DisplayRemoteFrame(unsigned char *data,int size)
{
int retvalue;
retvalue=DecompressFrame(data,size,rgbdata,buffersize);
if(!retvalue)
{
return;
}
::DrawDibDraw(hdib,
m_hdc,
remote_wnd_x, // dest : left pos
remote_wnd_y, // dest : top pos
-1, // don't zoom x
-1, // don't zoom y
&m_bmpinfo->bmiHeader, // bmp header info
rgbdata, // bmp data
0, // src :left
0, // src :top
IMAGE_WIDTH, // src : width
IMAGE_HEIGHT, // src : height
DDF_SAME_DRAW // use prev params....
);
}
void CCameraVideoDlg::OnStart()
{
if(isCaptureStarted)
{
vidcap->Destroy();
isCaptureStarted=FALSE;
SetDlgItemText(IDC_START,"开始");
m_pServer->UnInit();
m_bRequest=FALSE;
}
else
{
if(vidcap->StartCapture()==FALSE)
{
MessageBox("Unable to start the capture");
isCaptureStarted=FALSE;
}
else
{
isCaptureStarted=TRUE;
}
isCaptureStarted=TRUE;
m_bRequest=FALSE;
m_pServer->Init(OnDataArrive,8765,(DWORD)this);
SetDlgItemText(IDC_START,"停止");
}
}
void CCameraVideoDlg::OnOK()
{
CDialog::OnOK();
}
void CCameraVideoDlg::OnDestroy()
{
CDialog::OnDestroy();
m_pServer->UnInit();
// Close the video capture device
vidcap->Destroy();
// Close graphics....
if(hdib!=NULL)
{
::DrawDibEnd(hdib);
::DrawDibClose(hdib);
}
if(isVideo)
{
ExitH263Encoder(&cparams);
}
// close decoder ......
ExitH263Decoder();
delete m_pServer;
}
void CCameraVideoDlg::OnDataArrive(unsigned long sIP, SOCKET sClient, char * pData, unsigned long DataLength,DWORD userdata)
{
CCameraVideoDlg* pDlg=(CCameraVideoDlg*)userdata;
DWORD inf[2];
inf[0]=DataLength;
inf[1]=sClient;
pDlg->SendMessage(WM_NET_DATA_ARRIVE,(WPARAM)pData,(LPARAM)&inf);
}
LONG CCameraVideoDlg::OnDataArrivedMsg(WPARAM wParam,LPARAM lParam)
{
/*
DWORD inf[2];
memcpy(&inf,(DWORD*)lParam,2*sizeof(DWORD));
m_pServer->SendMsg((SOCKET)inf[1],(char*)wParam,inf[0]);
*/
m_bRequest=TRUE;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -