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

📄 demodlg.cpp

📁 采集卡demo
💻 CPP
📖 第 1 页 / 共 4 页
字号:
// HKVisionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Demo.h"
#include "DemoDlg.h"
#include "SelectDSP.h"
#include "settingsDlg.h"
#include "alarmfile.h"
#include "promptdlg.h"
#include "csetupdec.h"
#include "boardlist.h"
#include "videowin.h"

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
#include <mmsystem.h>


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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

int SplitRects(int width, int height, int cols, int rows, RECT *rectList);

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()

/////////////////////////////////////////////////////////////////////////////
// CHKVisionDlg dialog

CHKVisionDlg::CHKVisionDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHKVisionDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHKVisionDlg)
	m_brightness = 0;
	m_contrast = 0;
	m_hue = 0;
	m_saturation = 0;
	m_iframes = 0;
	m_pbframes = 0;
	m_lostframes = 0;
	m_audioframes = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHKVisionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHKVisionDlg)
	//DDX_Control(pDX, IDC_LOGWIN, m_Logwin);
	DDX_Slider(pDX, IDC_SLIDER_BRIGHT, m_brightness);
	DDX_Slider(pDX, IDC_SLIDER_CONTRAST, m_contrast);
	DDX_Slider(pDX, IDC_SLIDER_HUE, m_hue);
	DDX_Slider(pDX, IDC_SLIDER_SAT, m_saturation);
	DDX_Text(pDX, IDC_IFRAMES, m_iframes);
	DDX_Text(pDX, IDC_PBFRAMES, m_pbframes);
	DDX_Text(pDX, IDC_LOST_FRAMES, m_lostframes);
	DDX_Text(pDX, IDC_AUDIOFRAMES, m_audioframes);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CHKVisionDlg, CDialog)
	//{{AFX_MSG_MAP(CHKVisionDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_WM_CTLCOLOR()
	ON_WM_SHOWWINDOW()
	ON_WM_MOVE()
	ON_WM_HSCROLL()
	ON_WM_VSCROLL()
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDC_SETTINGS, OnSettings)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_CAPIMAGE, OnCapimage)
	ON_BN_CLICKED(IDC_AUDIO_PREVIEW, OnAudioPreview)
	ON_BN_CLICKED(IDC_VIDEO_PREVIEW, OnVideoPreview)
	ON_BN_CLICKED(IDC_CAPIFRAME, OnCapiframe)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_SUBSTOP, OnSubstop)
	ON_BN_CLICKED(IDC_BUTTON5, OnStartImageCap)
	ON_BN_CLICKED(IDC_BUTTON6, OnStopImageCap)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
	ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
	ON_REGISTERED_MESSAGE(MsgUpdateCounters, OnUpdateCounters)
	ON_REGISTERED_MESSAGE(MsgDataReady, OnDataReady)
	ON_BN_CLICKED(IDC_SERIAL, OnSerial)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()





/////////////////////////////////////////////////////////////////////////////
// CHKVisionDlg message handlers
int LogWrite(const char *Format, ...);

HANDLE ChannelHandle[MAX_CHANNELS];
int    logFile;
int    gFileHandle[MAX_CHANNELS];
BOOLEAN dlgInited = FALSE;
HWND gMainWnd;
RECT rectList[MAX_CHANNELS +1];
BOOL bDdrawMode =FALSE;
ULONG   gChannelFrames[MAX_CHANNELS];
LARGE_INTEGER   gChannelTotalLength[MAX_CHANNELS];
ULONG	gChannelFramesLost[MAX_CHANNELS];
ULONG	gChannelOverflow[MAX_CHANNELS];
ULONG	gCurrentFileLen[MAX_CHANNELS];
ULONG	gCaptureStartedNum = 0;
ULONG	gSignleCaptureNum = 0;
UINT    gTimer;
UINT    gCapImages;
ULONG	gFileSize = 100; 
int		gFileNum = 0;
int		gIFrameFileHandle;
AlarmFile alarmFile[64];
BOOL    bAlarmFileSave = FALSE;
BOOL bWriteToDisk = FALSE;			///hhh1120
BOOL bStartCapture = FALSE;
BOOL bTestData  = FALSE;
BOOL bAudioPre=FALSE;	//音频实时预览
BOOL bOverlayMode=FALSE;//如果函数SetPreviewOverlayMode的返回值为0,则bOverlayMode赋值为TRUE,表示启用Overlay方式预览

COLORREF gBackgroundColor = RGB(10, 10, 10);

int __cdecl StreamReadCallback(ULONG ChannelNum, void *Context);
typedef int (*STREAM_DIRECT_READ_CALLBACK)(ULONG channelNumber,void *DataBuf,DWORD Length,int FrameType,void *context);
int __cdecl StreamDirectReadCallback(ULONG channelNumber,void *DataBuf,DWORD Length,int FrameType,void *context);

void ImageStreamCallback(UINT channelNumber,void *Context);

extern int iLastSelect;
extern int iOldSelect;

ULONG StartTime;
ULONG LastTime;
extern CPromptDlg promptDlg;
static 	UINT testVideo[64], testAudio[64];
unsigned char StreamBuf[1000000];
//unsigned int motionData[MAX_CHANNELS][18];
unsigned char motionData[MAX_CHANNELS][1600];
///add ver3.4 by h
// qcif file handle while subChannel encoding cif and qcif 
int    gFileHandleQcif[MAX_CHANNELS];					
// qcif file header while subChannel encoding cif and qcif
unsigned char FileHeaderQcif[MAX_CHANNELS][200];		
// subChannel encoding cif and qcif
BOOL bEncodeCifAndQcif[MAX_CHANNELS];					
static HPEN penWhite = CreatePen(PS_SOLID,	1,RGB(255,255,255));
extern int DisplayMode ;

void CALLBACK DrawFun(long nport, HDC hDc,LONG nUser)
{
	CHKVisionDlg *pOwner = (CHKVisionDlg*)nUser;
	if (nport == iLastSelect  ) //&& DisplayMode != 1)
	{
		if (DisplayMode != 1)
		{
			int weight,height;
			weight = rectList[iLastSelect].right - rectList[iLastSelect].left;
			height = rectList[iLastSelect].bottom- rectList[iLastSelect].top;
			HGDIOBJ pOldPen = ::SelectObject(hDc,&penWhite);
			::MoveToEx(hDc,		1,  1,	NULL);
			::LineTo(hDc,		weight -1, 1);
			::LineTo(hDc,		weight -1,height-1);
			::LineTo(hDc,		1,height -1);
			::LineTo(hDc,		1,1);
			::SelectObject(hDc,pOldPen);
		}
		CString str;
		str.Format("channel%d",nport);
		SetTextColor(hDc, RGB(255,0,0));
		SetBkMode(hDc,TRANSPARENT);
		TextOut(hDc,10,10,str,str.GetLength());

		if(pOwner->m_bMoving[nport])
		{
			pOwner->m_VideoWin.DrawVect(nport,(char *)motionData[nport],hDc,pOwner->chstandard[nport]);
			pOwner->m_bMoving[nport] = FALSE;
		}
	}
	else
		pOwner->StopDrawFun(nport);
	return;

}

BOOL CHKVisionDlg::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);
	m_brush1.CreateSolidBrush(gBackgroundColor);   
	m_brush2.CreateSolidBrush(RGB(0,0,0));   
	m_bLog = FALSE;

	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

	m_brightness = 50;
	m_contrast = 50;
	m_hue = 0;
	m_saturation = 50;
	m_iframes = 0;
	m_pbframes = 0;
	m_audioframes = 0;
	m_lostframes = 0;
	
	m_bSwitchMetrix=FALSE;

	UpdateData(FALSE);

	m_VideoWin.SubclassDlgItem(IDC_VIDEOWIN, this);

	if(InitDSPs() <= 0){
		//afxDump<<"error:can not init DSPs\n";
		AfxMessageBox("can not init DSPs\n");
		return FALSE;
	}

	int iReturn=-1;
//根据函数SetPreviewOverlayMode的返回值来判断当前的显卡是否支持Overlay方式预览,
//0表示支持,其它值表示不支持	
	iReturn=SetPreviewOverlayMode(TRUE);
	TRACE("iReturn=%04x\n",iReturn);
	if(iReturn==0)
	{
		bOverlayMode=TRUE;
	}
	for(int i = 0; i < GetTotalDSPs(); i++){
		m_bDspPreset[i] = TRUE;
		bEncodeCifAndQcif[i] = FALSE;
	}
	

	for(i = 0; i < GetTotalDSPs(); i++){
		m_bDspPreset[i] = TRUE;
		bEncodeCifAndQcif[i] = FALSE;
	}
	
	dlgInited = TRUE;

	for(i = 0; i < GetTotalDSPs(); i++){
		ChannelHandle[i] = ChannelOpen(i);
		testVideo[i] = testAudio[i] =0;
		m_bMoving[i] = FALSE;
		RegisterDrawFun(i, DrawFun ,(DWORD)this);
	}
	int bright, contrast, sat, hue;
	for(i = 0; i < GetTotalDSPs(); i++)
	{
		GetVideoPara(ChannelHandle[i], &chstandard[i], &bright, &contrast, &sat, &hue);
	}

///add v34
//Channel0 now encoding cif and qcif, and having two subChannel  
//we can use SetupSubChannel() to config subchannel0(cif) and subchannel1(qcif)
//	SetEncoderPictureFormat(ChannelHandle[0], ENC_CIFQCIF_FORMAT);
//	bEncodeCifAndQcif[0] = TRUE;
//Channel1 now is encoding 2cif
//	if (GetTotalDSPs() >=2)
//		SetEncoderPictureFormat(ChannelHandle[1], ENC_2CIF_FORMAT);
	//SetEncoderPictureFormat(ChannelHandle[0], ENC_QCIF_FORMAT);
	//SetEncoderPictureFormat(ChannelHandle[1], ENC_2CIF_FORMAT);
	//SetEncoderPictureFormat(ChannelHandle[2], ENC_QCIF_FORMAT);
	//SetEncoderPictureFormat(ChannelHandle[3], ENC_QCIF_FORMAT);

//	SetupNotifyThreshold(ChannelHandle[0], 3);

	//SetVideoStandard(ChannelHandle[2], StandardNTSC);

	//int id = IDC_CHECK2;
	//for(i = 0; i < MAX_CHANNELS; i++){
	//	GetDlgItem(id + i)->EnableWindow(FALSE);
	//}
	
	logFile = _open("log.txt", _O_CREAT | _O_BINARY | _O_WRONLY| _O_TRUNC, _S_IREAD | _S_IWRITE);
	if(logFile == -1){
		AfxMessageBox("can not open log file\n");
		return FALSE;
	}
	RegisterStreamDirectReadCallback(::StreamDirectReadCallback,this);
	RegisterMessageNotifyHandle(m_hWnd, MsgDataReady);
//	RegisterStreamReadCallback(::StreamReadCallback, this);
	RegisterImageStreamCallback(ImageStreamCallback,this);

	gCapImages = 0;

/*	VERSION_INFO verInfo;
	GetSDKVersion(&verInfo);
	CString verStr;
	verStr.Format(" dsp ver%04x build%04x driver ver%04x build04%x sdk version%04x build%04x",
		verInfo.DspVersion, verInfo.DspBuildNum, verInfo.DriverVersion, verInfo.DriverBuildNum, 
		verInfo.SDKVersion, verInfo.SDKBuildNum);

	CString title;
	CString dsps;
	dsps.Format(" build(0701) %d DSPs Found", GetTotalDSPs());
	GetWindowText(title);
	title += dsps + verStr;
	SetWindowText(title);*/

	SetOverlayColorKey(gBackgroundColor);

	gTimer = SetTimer(1, 1000, 0);

//	SetTimer(2,5000,0);
	promptDlg.ShowWindow(SW_HIDE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CHKVisionDlg::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 CHKVisionDlg::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 CHKVisionDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



int LogWrite(const char *Format, ...)
{

	CHAR	Buffer0[1024];
	CHAR	Buffer1[512];
	DWORD	ItemsWritten;
	va_list	ArgumentList;

	if(logFile == -1)return -1;

	SYSTEMTIME sysTime;
	GetLocalTime(&sysTime);

	sprintf(Buffer0, "%02d%02d:%02d:%02d:%02d:", sysTime.wMonth, sysTime.wDay, sysTime.wHour, 
		sysTime.wMinute, sysTime.wSecond);

	va_start ( ArgumentList, Format ); 
	ItemsWritten = vsprintf ((char *)Buffer1, Format, ArgumentList );
	va_end ( ArgumentList );
	strcat(Buffer0, Buffer1);

	return _write ( logFile, Buffer0, strlen((const char *)Buffer0 ));
}


void CHKVisionDlg::OnStart() 
{
	// TODO: Add your control notification handler code here
	CSelectDSP selDialog;
	selDialog.DSPPreset(m_bDspPreset, GetTotalDSPs());

	if(selDialog.DoModal() == IDOK){
		TRACE("SelectDSP:OK button clicked\n");
		selDialog.GetDSPsChoiced(m_bDspPreset);

		CString str;
		str.Format("%d", 0);
		GetDlgItem(IDC_LOST_FRAMES)->SetWindowText((LPCTSTR)str);
		GetDlgItem(IDC_TOTALFRAMES)->SetWindowText((LPCTSTR)str);
		GetDlgItem(IDC_BPS)->SetWindowText((LPCTSTR)str);
		GetDlgItem(IDC_FPS)->SetWindowText((LPCTSTR)str);

		LogWrite("start capture\r\n"); 
//add v34
//		SetupSubChannel(ChannelHandle[0], 1);						//change to  sub channel
//		SetBitrateControlMode(ChannelHandle[0],  brCBR );			//setttings

⌨️ 快捷键说明

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