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

📄 usbdlg.cpp

📁 图像捕捉 可以用来捕捉图像的 适用于工业相机
💻 CPP
字号:
// usbDlg.cpp : implementation file
//

#include "stdafx.h"
#include "usb.h"
#include "usbDlg.h"
#include "MVCAPI.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
const DWORD MAXWIDTH  = 1280;
const DWORD MAXHEIGHT = 1024;
int  m_nPreviewW;
int  m_nPreviewH;

PBITMAPINFO bpBmp;
BOOL Cpature;
HWND    WinHwnd0;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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

/////////////////////////////////////////////////////////////////////////////
// CUsbDlg dialog

CUsbDlg::CUsbDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUsbDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUsbDlg)
		// 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 CUsbDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUsbDlg)
	DDX_Control(pDX, IDC_Pic1, m_Pic1);
	DDX_Control(pDX, IDC_SAVEFRAME, m_Pic);
	DDX_Control(pDX, IDC_VIEW, m_rectPreview);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUsbDlg, CDialog)
	//{{AFX_MSG_MAP(CUsbDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_CAPTURE, OnCapture)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUsbDlg message handlers
void CreateBMPInfo(BOOL Flip,DWORD m_nDisplayWidth,DWORD m_nDisplayHeight)
{       
	BYTE  BitCount;
	int   i,j=256;

	BitCount = 8;
	DWORD m_iTotalSize = m_nDisplayWidth*m_nDisplayHeight;
	bpBmp = (BITMAPINFO *) new BYTE[sizeof( BITMAPINFO ) + 
					(256 * sizeof( RGBQUAD )) ];	
	if ( bpBmp == NULL )return;

	bpBmp->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bpBmp->bmiHeader.biWidth = m_nDisplayWidth;
	bpBmp->bmiHeader.biHeight = m_nDisplayHeight;
	if(Flip != 0)
		bpBmp->bmiHeader.biHeight = - bpBmp->bmiHeader.biHeight;
	bpBmp->bmiHeader.biPlanes = 1;
	bpBmp->bmiHeader.biBitCount = BitCount;
	bpBmp->bmiHeader.biCompression = BI_RGB;
	bpBmp->bmiHeader.biSizeImage = m_iTotalSize;
	bpBmp->bmiHeader.biXPelsPerMeter = 0;
	bpBmp->bmiHeader.biYPelsPerMeter = 0;
	bpBmp->bmiHeader.biClrUsed = 0;
	bpBmp->bmiHeader.biClrImportant = 0;
	if(BitCount == 8)
	{
		for(i = 0; i < j; i++) 
		{
			bpBmp->bmiColors[i].rgbBlue = i ;
			bpBmp->bmiColors[i].rgbGreen = i  ;
			bpBmp->bmiColors[i].rgbRed = i ;
			bpBmp->bmiColors[i].rgbReserved = 0 ;
		}
	}
}

void CALLBACK FrameCallBackFunction(LPVOID lpParam, LPVOID lpUser)
{
	BYTE *pDataBuffer = (BYTE*)lpParam;
	CUsbDlg* dlg = (CUsbDlg*)lpUser;
	if (Cpature ) 
	{
		
		CreateBMPInfo(0,dlg->m_CapInfo.Width,dlg->m_CapInfo.Height);

		HDC hMemDC = ::GetWindowDC(WinHwnd0);
		CRect   rect;
		
		
		dlg->m_Pic.GetClientRect(&rect);

		::SetStretchBltMode(hMemDC,HALFTONE);
		::StretchDIBits(hMemDC, 0, 0, 
			rect.Width(), rect.Height(), 0, 0, dlg->m_CapInfo.Width,dlg->m_CapInfo.Height, pDataBuffer, bpBmp,
			DIB_RGB_COLORS, SRCCOPY);
		Cpature = FALSE;
	}	

}

BOOL CUsbDlg::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);
		}
	}
	m_bConnect = FALSE;
	m_bPreview = FALSE;
	m_pre = FALSE;
	m_hMVC1000        = NULL;
	DWORD RGBDataSize = MAXWIDTH*MAXHEIGHT*3;
	m_pRGBData        = (BYTE*)malloc(RGBDataSize*sizeof(BYTE));
	
	DWORD RawDataSize = MAXWIDTH*MAXHEIGHT+512;
	m_pRawData        = (BYTE*)malloc(RawDataSize*sizeof(BYTE));
	memset(m_pRawData,0,RawDataSize);
	m_CapInfo.Buffer  = m_pRawData;
	Cpature = FALSE;
	// 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
	WinHwnd0 = m_Pic.m_hWnd;
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CUsbDlg::OnStart() 
{
	// TODO: Add your control notification handler code here
	int nIndex;
	memset(&m_CapInfo, 0, sizeof(CapInfoStruct));
	m_CapInfo.Buffer = m_pRawData;
	m_CapInfo.Width  = MAXWIDTH;
	m_CapInfo.Height = MAXHEIGHT;
	m_nPreviewW       = MAXWIDTH;
	m_nPreviewH       = MAXHEIGHT;
	m_CapInfo.Exposure         = 100;
	m_CapInfo.Gain[0]          = 58;
	m_CapInfo.Gain[1]          = 58;
	m_CapInfo.Gain[2]          = 58;
	m_CapInfo.Control          = 0;
	memset(m_CapInfo.Reserved, 0, 8);

	int rt = MV_Usb2Init("MVC1000", &nIndex, &m_CapInfo, &m_hMVC1000);
	CRect   rect;   
	m_rectPreview.GetClientRect(&rect);

	MV_Usb2Start(	m_hMVC1000,	"MVC1000 Preview",
					WS_CHILD|WS_VISIBLE,
					0,	0, rect.Width(), rect.Height(),
					m_rectPreview.m_hWnd,
					NULL,
					THREAD_PRIORITY_NORMAL,
					THREAD_PRIORITY_NORMAL );
	MV_Usb2SetFrameCallBack(m_hMVC1000, FrameCallBackFunction, this);
}

void CUsbDlg::OnCapture() 
{
	Cpature = TRUE;
	MV_Usb2GetRawData(m_hMVC1000, &m_CapInfo); 
	CreateBMPInfo(0,m_CapInfo.Width,m_CapInfo.Height);
	HDC hMemDC1 = ::GetWindowDC(m_Pic1.m_hWnd);
	CRect   rect;		
	m_Pic1.GetClientRect(&rect);
	::SetStretchBltMode(hMemDC1,HALFTONE);
	::StretchDIBits(hMemDC1, 0, 0, 
		rect.Width(), rect.Height(), 0, 0, m_CapInfo.Width,m_CapInfo.Height, m_CapInfo.Buffer, bpBmp,
		DIB_RGB_COLORS, SRCCOPY);

}

⌨️ 快捷键说明

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