?? mainframe.cpp
字號:
// MainFrame.cpp : implementation file
//
#include "stdafx.h"
#include "test1394show.h"
#include "MainFrame.h"
#include "DisplayDlg.h"
#include "DeviceDlg.h"
#include "Configure1394Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define SCREEN_BPP 8
#define BLOCKSIZE 1024//2048 //當總線速度為400Kb/S,每個異步包的有效數據塊大小
#define IMAGESIZE 101376//307200 //圖像字節數
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
CMainFrame::CMainFrame()
{
LoadFrame(IDR_MAINFRAME);
m_bWindowed = TRUE;
m_nDisplayOrSave = 0;
m_bStop = FALSE;
m_bOpen = FALSE;
char cTemp[200];
::GetCurrentDirectory(200,cTemp);
m_strImagePath = cTemp;
m_strImagePath += "\\";
m_strImageName = "image";
m_strImageExt = "raw";
}
CMainFrame::~CMainFrame()
{
FreeDirectDraw();
}
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_COMMAND(ID_START, OnStart)
ON_UPDATE_COMMAND_UI(ID_START, OnUpdateStart)
ON_UPDATE_COMMAND_UI(ID_STOP, OnUpdateStop)
ON_COMMAND(ID_CONFIGURE_1394, OnConfigure1394)
ON_COMMAND(ID_CONFIGURE_DISPLAY, OnConfigureDisplay)
ON_COMMAND(ID_GET_1394DEVICE, OnGet1394device)
ON_WM_CREATE()
ON_COMMAND(ID_FULL_SCREEN, OnFullScreen)
ON_WM_CHAR()
ON_WM_MOVE()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
//圖像大小已經改為640*480,2004.2.11,王沛改
void CMainFrame::OnStart()
{
//顯示已知的圖像序列
if( m_nDisplayOrSave == 1 )
{
if( m_ImageNameExtList.IsEmpty() )
{
MessageBox("請設定需要顯示的圖像序列!");
return;
}
CString strAllName,str;
POSITION pos = m_ImageNameExtList.GetHeadPosition();
for (int i=0;i < m_ImageNameExtList.GetCount();i++)
{
if( TRUE == m_bStop )
break;
str = m_ImageNameExtList.GetNext(pos);
strAllName = m_strImagePath + str;
if(FAILED( m_pDisplay->CreateSurfaceFromFile(&m_pLogoSurface,strAllName.GetBuffer(strAllName.GetLength()),0,0,m_strImageExt)))
{
MessageBox("顯示圖像失敗");
return;
}
DisplayFrame();
SAFE_DELETE( m_pLogoSurface );
}
}
//采集圖像并顯示、存儲
//開始從1394設備讀取數據并顯示
//讀取數據
//顯示
if( m_nDisplayOrSave == 0 )
{
/*
time_t ltimeLa;
_tzset();
time( <imeLa );
*/
//先判斷1394設備是否打開
if( !m_bOpen )
return;
//得到設定的將要保存的圖像的路徑、名稱、擴展名(文件類型)
//設置循環,每次得到一幀圖像,存儲并顯示
DWORD Status;
//定義ASYNC_READ
ASYNC_READ asyncRead;
//自動得到Generation次數
asyncRead.bGetGeneration = TRUE;
asyncRead.ulGeneration = 0;
//地址
asyncRead.DestinationAddress.IA_Destination_Offset.Off_High = 0;
asyncRead.DestinationAddress.IA_Destination_Offset.Off_Low = 0;
//讀取的字節數
asyncRead.nNumberOfBytesToRead = BLOCKSIZE;
asyncRead.nBlockSize = 0;
asyncRead.fulFlags = 0;
/*
asyncRead.fulFlags |= ASYNC_FLAGS_NONINCREMENTING;
asyncRead.fulFlags |= ASYNC_FLAGS_PING;
*/
//開辟傳給驅動的存儲空間
ULONG ulBufferSize;
PASYNC_READ pAsyncRead = NULL;
ulBufferSize = sizeof(ASYNC_READ) + asyncRead.nNumberOfBytesToRead;//字節為單位
pAsyncRead = (PASYNC_READ)LocalAlloc(LPTR, ulBufferSize);
FillMemory(pAsyncRead, ulBufferSize, 0);//開辟的每一個字節都為零
*pAsyncRead = asyncRead;
int nFrame;nFrame = 0;
int nTime,nRow,nLine;
char byteFrame[IMAGESIZE];//存儲采集的一幀圖像
ZeroMemory(byteFrame,IMAGESIZE);
// ZeroMemory(byteFrame,35);
while( nFrame < m_nFrameNumber )//采集的幀數
{
if( TRUE == m_bStop )
break;
nRow = nLine = 0;
//采集一幀的數據,然后存儲到文件,并將裸圖轉換為位圖
for( nTime = 0;nTime < (IMAGESIZE/BLOCKSIZE-1);nTime++ )
//for( nTime = 0;nTime < IMAGESIZE/BLOCKSIZE;nTime++ )//640*480 = 2048*150 = 307200
{
Status = g_CurrentDev.AsyncRead(pAsyncRead,ulBufferSize);//調驅動,讀BLOCKSIZE個字節
if ( Status == STATUS_SUCCESS )
{
//輸出讀入的數據到byteFrame
CopyMemory(&byteFrame[nTime*(BLOCKSIZE-4)],pAsyncRead->Data,(BLOCKSIZE-4));//hyb 改,為了去除黑點,只取前1020字節,04-11-08
//CopyMemory(&byteFrame[nTime*BLOCKSIZE],pAsyncRead->Data,BLOCKSIZE);//每次拷貝BLOCKSIZE字節,直到考完BLOCKSIZE*150字節
}
else
{
Status = GetLastError();
// PrintOut(NL"AsyncRead failed"NL);
// PrintError(Status);
MessageBox("AsyncRead failed");
break;
}
}
if( NULL != pAsyncRead )
LocalFree(pAsyncRead);
//保存byteFrame中的一幀數據到文件中
SaveImageToFile(byteFrame,nFrame);
//在內存中創建圖像
if(FAILED( m_pDisplay->CreateSurfaceFromImage(&m_pLogoSurface,byteFrame,0,0) ))
{
MessageBox("顯示圖像失敗");
return;
}
//顯示內存中的一幀圖像
DisplayFrame();
SAFE_DELETE( m_pLogoSurface );
nFrame++;
}
// close device
g_CurrentDev.Close();
// MessageBox("采集圖像結束!");
}
}
void CMainFrame::OnStop()
{
//停止讀取數據和顯示
m_bStop = TRUE;
}
void CMainFrame::OnUpdateStart(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
}
void CMainFrame::OnUpdateStop(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
}
//對于1394設備的屬性和顯示屬性,在程序啟動時默認設置,用戶可在Setting菜單下修改
void CMainFrame::OnConfigure1394()
{
//獲得和設置1394設備的屬性
CConfigure1394Dlg dlg;
if( IDOK != dlg.DoModal() )
return;
else
{
}
}
void CMainFrame::OnConfigureDisplay()
{
//獲得和設置顯示屬性
CDisplayDlg DisplayDlg;
if( IDOK != DisplayDlg.DoModal() )
return;
else
{
m_nDisplayOrSave = DisplayDlg.m_nDisplayOrSave;//顯示或存儲
m_strImagePath = DisplayDlg.m_strImagePath;//路徑
m_strImageName = DisplayDlg.m_strImageName;//圖像名稱
m_strImageExt = DisplayDlg.m_strImageExt;//擴展名
if( m_nDisplayOrSave == 1 )
{
//得到預顯示的圖像的名稱
m_ImageNameExtList.RemoveAll();
CString str;
POSITION pos = DisplayDlg.m_FileNameExtList.GetHeadPosition();
for (int i=0;i < DisplayDlg.m_FileNameExtList.GetCount();i++)
{
str = DisplayDlg.m_FileNameExtList.GetNext(pos);
m_ImageNameExtList.AddTail(str);
}
}
}
}
HRESULT CMainFrame::InitDirectDraw()
{
LPDIRECTDRAWPALETTE pDDPal = NULL;
HRESULT hr;
// int iSprite;
//創建DirectDraw對象,窗口模式
m_pDisplay = new C1394Display();
HWND hWnd = m_hWnd;
if( m_bWindowed )
{
if( FAILED( hr = m_pDisplay->CreateWindowedDisplay( hWnd, WINDOW_WIDTH, WINDOW_HEIGHT ) ) )
{
MessageBox( "建立DirectDraw對象失敗" );
return hr;
}
}
else if( FAILED( hr = m_pDisplay->CreateFullScreenDisplay( hWnd, WINDOW_WIDTH, WINDOW_HEIGHT, SCREEN_BPP ) ) )
{
MessageBox( "建立DirectDraw對象失敗" );
return hr;
}
/* //創建調色板
// Create and set the palette when in palettized color
if( FAILED( hr = m_pDisplay->CreatePaletteFromBitmap( &pDDPal, MAKEINTRESOURCE( IDB_DIRECTX ) ) ) )
return hr;
m_pDisplay->SetPalette( pDDPal );
SAFE_RELEASE( pDDPal );
*/ //創建表面
// Create a surface, and draw a bitmap resource on it.
/* if( FAILED( hr = m_pDisplay->CreateSurfaceFromBitmap( &m_pLogoSurface, MAKEINTRESOURCE( IDB_DIRECTX ),
SPRITE_DIAMETER, SPRITE_DIAMETER ) ) )
return hr;
*/
// if (FAILED(hr = m_pDisplay->CreateSurfaceFromBitmap
// (&m_pLogoSurface,"F:\\wx\\Motion1.bmp",0,0)))
/*
// Create a surface, and draw text to it.
if( FAILED( hr = m_pDisplay->CreateSurfaceFromText( &m_pTextSurface, NULL, HELPTEXT,
RGB(0,0,0), RGB(255, 255, 0) ) ) )
return hr;
*/
// Set the color key for the logo sprite to black
// if( FAILED( hr = m_pLogoSurface->SetColorKey( 0 ) ) )
// return hr;
return S_OK;
}
void CMainFrame::FreeDirectDraw()
{
// SAFE_DELETE( m_pLogoSurface );
/* for( int i=0;i<10;i++ )
SAFE_DELETE( m_pLogoSurface );
*/
// SAFE_DELETE( m_pTextSurface );
SAFE_DELETE( m_pDisplay );
}
void CMainFrame::OnGet1394device()
{
// 查詢所有的1394設備,并打開其中一個
CDeviceDlg dlg;
if( IDOK != dlg.DoModal() )
return;
else
{
m_nFrameNumber = dlg.m_nFrameNumber;
m_bOpen = dlg.m_bOpen;
}
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
InitDirectDraw();/*
//
CString strName = "F:\\wx\\計算機視覺圖片資料1\\\\Motion";
CString strBack = ".bmp";
CString strNum,strAllName;
for( int i=0;i<10;i++ )
{
strNum.Empty();
strNum.Format("%d",i+1);
strAllName = strName + strNum + strBack;
if(FAILED( m_pDisplay->CreateSurfaceFromFile(&m_pLogoSurface[i],strAllName.GetBuffer(strAllName.GetLength()),0,0)))
{
MessageBox("顯示圖像失敗");
return -1;
}
// DisplayFrame();
// SAFE_DELETE( m_pLogoSurface );
}
//*/
return 0;
}
HRESULT CMainFrame::DisplayFrame()
{
HRESULT hr;
// Fill the back buffer with black, ignoring errors until the flip
m_pDisplay->Clear( 0 );//清空 DisplaySurface
// Blt the help text on the backbuffer, ignoring errors until the flip
// m_pDisplay->Blt( 10, 10, m_pTextSurface, NULL );//用 Blt() 描繪圖象
// Blt all the sprites onto the back buffer using color keying,
// ignoring errors until the last blt. Note that all of these sprites
// use the same DirectDraw surface.
m_pDisplay->Blt( 0, 0, m_pLogoSurface, NULL );
// We are in windowed mode so perform a blt from the backbuffer
// to the primary, returning any errors like DDERR_SURFACELOST
if( FAILED( hr = m_pDisplay->Present() ) )//用 Present() 換幀
return hr;
return S_OK;
}
HRESULT CMainFrame::DisplayFrame(int i)
{
HRESULT hr;
// Fill the back buffer with black, ignoring errors until the flip
// m_pDisplay->Clear( 0 );//清空 DisplaySurface
// Blt the help text on the backbuffer, ignoring errors until the flip
// m_pDisplay->Blt( 10, 10, m_pTextSurface, NULL );//用 Blt() 描繪圖象
// Blt all the sprites onto the back buffer using color keying,
// ignoring errors until the last blt. Note that all of these sprites
// use the same DirectDraw surface.
m_pDisplay->Blt( 0, 0, m_pLogoSurfaceMul[i], NULL );
// We are in windowed mode so perform a blt from the backbuffer
// to the primary, returning any errors like DDERR_SURFACELOST
if( FAILED( hr = m_pDisplay->Present() ) )//用 Present() 換幀
return hr;
return S_OK;
}
void CMainFrame::OnFullScreen()
{
}
void CMainFrame::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if( nChar== VK_ESCAPE ) //判斷是否按下 Esc 鍵
PostMessage(WM_CLOSE ); //傳送WM_CLOSE信息
if( nChar== VK_SPACE ) //判斷是否按下 空格 鍵
OnStart();
CFrameWnd::OnChar(nChar, nRepCnt, nFlags);
}
void CMainFrame::OnMove(int x, int y)
{
CFrameWnd::OnMove(x, y);
m_pDisplay->UpdateBounds();
}
void CMainFrame::SaveImageToFile(char *byteFrame, int n)
{
//保存圖像數據到裸圖文件
//構造文件名為 *00.raw、*01.raw......
CString strFile,strTemp;
strTemp.Format("%d",n);
int nLen;
nLen = strTemp.GetLength();
if( 1== nLen )
strTemp = "00" + strTemp;
if( 2== nLen )
strTemp = "0" + strTemp;
strFile = m_strImagePath + m_strImageName + strTemp + ".raw";
HANDLE hf; // file handle
hf = CreateFile(strFile,
GENERIC_READ | GENERIC_WRITE,
(DWORD) 0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (hf == INVALID_HANDLE_VALUE)
{
DWORD dw = GetLastError();
}
// Copy the BITMAPFILEHEADER into the .BMP file.
DWORD dwTmp;
//if (!WriteFile(hf, byteFrame, IMAGESIZE, (LPDWORD) &dwTmp, NULL))
if (!WriteFile(hf, byteFrame, 99960, (LPDWORD) &dwTmp, NULL))//1020*98=99960
{
DWORD dw = GetLastError();
}
// Close the .BMP file.
if (!CloseHandle(hf))
{
DWORD dw = GetLastError();
}
}
void CMainFrame::OnDestroy()
{
CFrameWnd::OnDestroy();
// close device
g_CurrentDev.Close();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -