?? transclientdoc.cpp
字號(hào):
// TransClientDoc.cpp : implementation of the CTransClientDoc class
//
#include "stdafx.h"
#include "TransClient.h"
#include "TransClientDoc.h"
#include "TransClientView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "TransJpegView.h"
#include "ConnectDlg.h"
#include "..\ImgDecompress\ImgDecompress.h"
#include "..\Enroll\EnrollInterface.h"
/////////////////////////////////////////////////////////////////////////////
// CTransClientDoc
IMPLEMENT_DYNCREATE(CTransClientDoc, CDocument)
BEGIN_MESSAGE_MAP(CTransClientDoc, CDocument)
//{{AFX_MSG_MAP(CTransClientDoc)
ON_COMMAND(IDM_SYS_CONNECT, OnConnect)
ON_COMMAND(IDM_DB_SAVE, OnDBSave)
ON_COMMAND(IDM_DB_STOP, OnDBStop)
ON_COMMAND(IDM_DB_BROWSE, OnDBBrowse)
ON_COMMAND(ID_IMGPROC, OnImgproc)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTransClientDoc construction/destruction
CTransClientDoc::CTransClientDoc()
{
m_PatientID=-1;
m_ImgWidth=0;
m_ImgHeight=0;
m_StreamSocket=new CClientStreamSocket(this);
m_pDib=(BYTE*)::GlobalAlloc(GMEM_FIXED,MAX_BMP_SIZE);
if (!m_pDib){
AfxMessageBox("Error to allocate memory for pDib!",MB_ICONERROR);
}
m_JpegBuffer=(BYTE*)::GlobalAlloc(GMEM_FIXED,MAX_JPEG_SIZE);
if (!m_JpegBuffer){
AfxMessageBox("Error to allocate memory for JpegBuffer!",MB_ICONERROR);
}
m_Frequency[0]=m_Frequency[1]=m_Frequency[2]=m_FrameStartTime=0;
m_Bps[0]=m_Bps[1]=m_Bps[2]=0;
}
CTransClientDoc::~CTransClientDoc()
{
::GlobalFree(m_pDib);
::GlobalFree(m_JpegBuffer);
IDRelease(&m_jcprops);
}
BOOL CTransClientDoc::OnNewDocument()
{
#ifdef _DEBUG
SetTitle(GetTitle()+"<< Debug >>");
#endif
if (!CDocument::OnNewDocument())
return FALSE;
((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);
if (!IDInit(&m_jcprops)){
DisplayMsg(IDGetLastError());
}
if (InitNetwork())
DisplayMsg("Init network successful.");
else
DisplayMsg("Init network failed!");
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CTransClientDoc serialization
void CTransClientDoc::Serialize(CArchive& ar)
{
// CEditView contains an edit control which handles all serialization
((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}
/////////////////////////////////////////////////////////////////////////////
// CTransClientDoc diagnostics
#ifdef _DEBUG
void CTransClientDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CTransClientDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTransClientDoc commands
BOOL CTransClientDoc::InitNetwork()
{
::AfxSocketInit();
if (!m_StreamSocket->Create(0,SOCK_STREAM,FD_CONNECT | FD_READ | FD_WRITE)){
return FALSE;
}
return TRUE;
}
void CTransClientDoc::DisplayMsg(LPCTSTR Msg)
{
for (POSITION pos=GetFirstViewPosition();pos!=NULL;){
CView* pView=GetNextView(pos);
CTransClientView* pClientView=DYNAMIC_DOWNCAST(CTransClientView,pView);
if (pClientView!=NULL){
int len = pClientView->GetWindowTextLength();
pClientView->GetEditCtrl().SetSel(len,len);
pClientView->GetEditCtrl().ReplaceSel(Msg);
pClientView->GetEditCtrl().ReplaceSel("\n");
}
}
SetModifiedFlag(FALSE);
}
void CTransClientDoc::ReceiveData(STREAM_DATA *Data)
{
/*
CString sResult;
sResult.Format("Receive package #%d, size %d.",Data->nIndex,Data->nSize);
DisplayMsg(sResult);
*/
if (Data->bEnd){//is the last package
//calculate the bps of current frame
for (int i=0;i<2;i++){
m_Bps[i+1]=m_Bps[i];
}
DWORD TimeTmp=GetTickCount();
if (TimeTmp-m_FrameStartTime>0){
m_Bps[0]=(PackageSize*Data->nIndex+Data->nSize)*1000/(TimeTmp-m_FrameStartTime);
}
//copy memory
if ((PackageSize*Data->nIndex)>MAX_JPEG_SIZE || Data->nSize<0 || Data->nSize>PackageSize){
DisplayMsg("Fatal error when combine package!");
return;
}
CopyMemory(m_JpegBuffer+(PackageSize*Data->nIndex),Data->Buffer,Data->nSize);
if (IDDecompress(&m_jcprops,m_JpegBuffer,PackageSize*Data->nIndex+Data->nSize,&m_pDib,&m_ImgWidth,&m_ImgHeight)){
/*
DisplayMsg("Uncompress jpeg successful.");
*/
if(m_PatientID>=0){
if(ENAddImage(m_PatientID,m_JpegBuffer,PackageSize*Data->nIndex+Data->nSize)<0){
DisplayMsg("Save image failed!");
}
}
}
else{
DisplayMsg("Uncompress jpeg failed!");
}
//draw new image on the jpegview
for (POSITION pos=GetFirstViewPosition();pos!=NULL;){
CView* pView=GetNextView(pos);
CTransJpegView* pJpegView=DYNAMIC_DOWNCAST(CTransJpegView,pView);
if (pJpegView!=NULL){
pJpegView->DrawImg();
}
}
}
else{
//record start time for this frame
for (int j=0;j<2;j++){
m_Frequency[j+1]=m_Frequency[j];
}
if (Data->nIndex==0){
DWORD EndTime=GetTickCount();
m_Frequency[0]=EndTime-m_FrameStartTime;
m_FrameStartTime=EndTime;
}
CopyMemory(m_JpegBuffer+(PackageSize*Data->nIndex),Data->Buffer,PackageSize);
}
return;
}
//open the connect dialog, and connect to the ip which user select
//2002.04.19
void CTransClientDoc::OnConnect()
{
CConnectDlg ConDlg;
if (ConDlg.DoModal()==IDOK){
m_StreamSocket->Connect(ConDlg.m_sIpAddress,LISTEN_PORT);
}
}
void CTransClientDoc::OnDBSave()
{
m_PatientID=ENAddPatient();
if(m_PatientID<0){
DisplayMsg("Error to add new patient!");
}
else{
CString str;
str.Format("Begin to record image for patient %d.",m_PatientID);
DisplayMsg(str);
}
}
void CTransClientDoc::OnDBStop()
{
if (m_PatientID>=0){
DisplayMsg("Stop to record image!");
m_PatientID=-1;
}
}
void CTransClientDoc::OnDBBrowse()
{
ENBrowse();
}
void CTransClientDoc::OnImgproc()
{
if (m_pDib==NULL || m_ImgWidth<=0 || m_ImgHeight<=0){
AfxMessageBox("No image to process!");
return;
}
char AppFileName[MAX_PATH]="";
if (!GetModuleFileName(AfxGetApp()->m_hInstance,AppFileName,MAX_PATH)){
AfxMessageBox("Error when get application path!",MB_ICONERROR);
return;
}
CString TmpFileName=AppFileName;
TmpFileName+=".tmp";
CFile f;
f.Open(TmpFileName,CFile::modeCreate | CFile::modeWrite);
BITMAPFILEHEADER bmfh;
bmfh.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
bmfh.bfReserved1=0;
bmfh.bfReserved2=0;
bmfh.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+3*m_ImgWidth*m_ImgHeight;
bmfh.bfType=(WORD)('M'<< 8)|'B';
f.Write(&bmfh,sizeof(BITMAPFILEHEADER));
BITMAPINFOHEADER bmih;
bmih.biBitCount=24;
bmih.biClrImportant=0;
bmih.biClrUsed=0;
bmih.biCompression=0;
bmih.biHeight=m_ImgHeight;
bmih.biPlanes=1;
bmih.biSize=sizeof(BITMAPINFOHEADER);
bmih.biSizeImage=m_ImgWidth*m_ImgHeight;
bmih.biWidth=m_ImgWidth;
bmih.biXPelsPerMeter=1;
bmih.biYPelsPerMeter=1;
f.Write(&bmih,sizeof(BITMAPINFOHEADER));
f.WriteHuge(m_pDib,m_ImgWidth*m_ImgHeight*3);
f.Close();
CString ExeFileName=AppFileName;
ExeFileName=ExeFileName.Left(ExeFileName.ReverseFind('\\')+1);
ExeFileName+="ImgGrabProc.exe";
if ((int)::ShellExecute(AfxGetApp()->m_pMainWnd->GetSafeHwnd(),"open",ExeFileName,"\""+TmpFileName+"\"",NULL,SW_SHOW)<=32){
AfxMessageBox("Error when start image process application!",MB_ICONWARNING);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -