// DN_sampleDlg.cpp : implementation file // #include "stdafx.h" #include "DN_sample.h" #include "DN_sampleDlg.h" #include #include #include"form2.h" #include #ifdef _DEBUG #define new DEBUG_NEW #endif // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementation protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() // CDN_sampleDlg dialog CDN_sampleDlg::CDN_sampleDlg(CWnd* pParent /*=NULL*/) : CDialog(CDN_sampleDlg::IDD, pParent) , mx(-1),my(0),mw(0) , m_ImgIndex(-1) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CDN_sampleDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_VIDEO, m_video); DDX_Control(pDX, IDC_LIST2, m_listBox); DDX_Control(pDX, IDC_TAB1, m_tab); } void CDN_sampleDlg::OnCancel() { for (auto item = m_ImgVec.begin(); item != m_ImgVec.end(); ++item) { delete (*item); } m_ImgVec.clear(); CDialog::OnCancel(); } BEGIN_MESSAGE_MAP(CDN_sampleDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_HSCROLL() ON_LBN_SELCHANGE(IDC_LIST2, &CDN_sampleDlg::SelectOneImg) ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, &CDN_sampleDlg::ChangeTab) ON_COMMAND(IDC_BTN_MINIMIZE, &CDN_sampleDlg::OnBtnMinimize) //ON_COMMAND(IDC_BTN_MAXIMIZE, &CDN_sampleDlg::OnBtnMaximize) ON_WM_CONTEXTMENU() ON_WM_RBUTTONDOWN() ON_COMMAND(ID_OPERAT_32772, &CDN_sampleDlg::OnDeleteOne) ON_COMMAND(ID_OPERAT_32773, &CDN_sampleDlg::OnSaveOne) END_MESSAGE_MAP() BEGIN_EVENTSINK_MAP(CDN_sampleDlg, CDialog) ON_EVENT(CDN_sampleDlg, IDC_VIDEO, 13, CDN_sampleDlg::MicroTouchPressedVideo, VTS_NONE) //ON_EVENT(CDN_sampleDlg, IDC_VIDEO, 14, CDN_sampleDlg::FootPedalPressed, VTS_NONE) //ON_EVENT(CDN_sampleDlg, IDC_VIDEO, DISPID_MOUSEMOVE, CDN_sampleDlg::MouseMoveVideo, VTS_I2 VTS_I2 VTS_I4 VTS_I4) END_EVENTSINK_MAP() void CDN_sampleDlg::SelectOneImg() { int index = m_listBox.GetCurSel(); if (index != LB_ERR) { if (index == m_ImgIndex ) return; m_ImgIndex = index; // 获取选中项的文本 CString selectedText; m_listBox.GetText(index, selectedText); if (index < m_ImgVec.size()) { m_tab.SetCurSel(0); m_f1.ShowWindow(SW_SHOW); m_f2.ShowWindow(SW_HIDE); m_video.ShowWindow(SW_HIDE); m_f2.SetMicroImage(m_ImgVec[index]); if (m_f1.hb != NULL) { DeleteObject(m_f1.hb); m_f1.hb = NULL; } m_f1.ShowOriginImg(m_ImgVec[index]); m_f1.Invalidate(); } } } BOOL CDN_sampleDlg::OnInitDialog() { CDialog::OnInitDialog(); //UpdateData(0); // 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 HDC i = ::GetDC(0); hdc = CreateCompatibleDC(i); ::ReleaseDC(0, i); m_tab.InsertItem(0, _T("抓拍图片")); m_tab.InsertItem(1, _T("距离测量")); m_tab.InsertItem(2, _T("实时预览")); //m_f1.SetParent(&m_tab); m_f1.Create(IDD_FORM2, &m_tab); m_f2.Create(IDD_CALIMAGE, &m_tab); m_video.SetParent(&m_tab); RECT rect; m_tab.GetClientRect(&rect); rect.top += 22; //对坐标做一些微调,这26是选项卡按钮的高度 rect.left += 2; //这是边框的宽度 m_f1.SetWindowPos(NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER); m_f2.SetWindowPos(NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER); m_f1.MoveWindow(&rect); m_f2.MoveWindow(&rect); m_f1.ShowWindow(SW_SHOW); m_f2.ShowWindow(SW_HIDE); m_video.ShowWindow(SW_HIDE); m_f2.SetImgPos(); CDC* pDC = GetDC(); // 获取设备上下文 TEXTMETRIC tm; // 获取字体的高度 pDC->GetTextMetrics(&tm); m_nFontHeight = tm.tmHeight; ReleaseDC(pDC); // 释放设备上下文 WCHAR buffer[MAX_PATH] = { 0 }; GetModuleFileNameW(NULL, buffer, MAX_PATH); CString path(buffer); int found = path.ReverseFind(L'\\'); m_ExePath = path.Left(found); // 可执行文件所在目录 if (theApp.g_IsDebug) { Test(); } if (m_video.GetVideoDeviceCount() == 0) return false; m_video.SetUseVideoFilter(1); m_video.SetConnected(TRUE); VARIANT vaResult; VariantInit(&vaResult); //if the Video have 1.3M resolution , then set the video as 1.3M. [Function Start] vaResult = m_video.GetVideoCaps(); int upper0 = vaResult.parray->rgsabound[0].cElements; int upper1 = vaResult.parray->rgsabound[1].cElements; if (m_video.GetDeviceID(0).Find(L"0863") > 0) { m_video.SetVideoFormat(640, 480); m_video.SetColorFormat(15); //m_video.SetPreviewScale(true); m_video.SetPreview(TRUE); } else if (upper0 != 0) { for (int i = 0; i <= upper1; i++)//for AM4013 ,AM4015 series (1280*1024) { int kk = ((unsigned long*)vaResult.parray->pvData)[i]; if (kk == 1280) { m_video.SetVideoFormat(1280, 1024); m_video.SetPreviewScale(true); } } for (int i = 0; i <= upper1; i++)//for AM7013 series (1280*960) { int kk = ((unsigned long*)vaResult.parray->pvData)[i]; if (kk == 2592) { m_video.SetVideoFormat(1280, 960); m_video.SetPreviewScale(true); m_video.SetColorFormat(15); m_video.SetCaptureRate(30); } } Sleep(200); m_video.SetPreview(TRUE); } //if the Video have 1.3M resolution , then set the video as 1.3M. [Function End] //if(m_video.GetDeviceID(0).Find(L"0863")>0) //{ // CSliderCtrl* pScrollBar5 = (CSliderCtrl*)GetDlgItem(IDC_SLIDER7); // pScrollBar5->ShowWindow(true); //} //else //{ // CSliderCtrl* pScrollBar5 = (CSliderCtrl*)GetDlgItem(IDC_SLIDER7); // pScrollBar5->ShowWindow(false); //} //m_capfilename="c:\\capture.avi"; //m_sldRotate.SetRange(0,259); m_device.Clear(); for (int f = 0; f < m_video.GetVideoDeviceCount(); f++) { m_device.AddString(m_video.GetVideoDeviceName(f)); m_device.SetCurSel(0); } m_videoCodec.AddString(L"(none)"); for (int f = 0; f < m_video.GetVideoCodecCount(); f++) { m_videoCodec.AddString(m_video.GetVideoCodecName(f)); } for (int f = 0; f < m_video.GetVideoCodecCount(); f++) { if (m_video.GetVideoCodecName(f) == L"MJPEG Compressor") m_videoCodec.SetCurSel(f + 1); } m_audioCodec.AddString(L"(none)"); for (int f = 0; f < m_video.GetAudioCodecCount(); f++) { m_audioCodec.AddString(m_video.GetAudioCodecName(f)); } m_audioCodec.SetCurSel(0); UpdateData(0); //SetTimer(1,250,0); //m_ae = m_video.GetExposure(m_device.GetCurSel()); //m_red = m_video.GetAWBR(m_device.GetCurSel()); //m_green = m_video.GetAWBG(m_device.GetCurSel()); //m_blue = m_video.GetAWBB(m_device.GetCurSel()); ////设置二值化拍照 //m_video.put_VideoProcAmp(-106, 0);//treshold //m_video.put_VideoProcAmp(-107, RGB(0, 0, 0));//color if (!m_video.EnableMicroTouch(true)) { //设置touch enable 启用返回false //AfxMessageBox(_T("EnableMicroTouch 启用"), MB_ICONHAND); } else { AfxMessageBox(_T("EnableMicroTouch 启用失败"), MB_ICONERROR); } return TRUE; // return TRUE unless you set the focus to a control } void CDN_sampleDlg::Test() { CString path[] = {L"\\DoniImage\\1-1.png" ,L"\\DoniImage\\1111.png",L"\\DoniImage\\1112.png",L"\\DoniImage\\2222.png" }; for (int i = 0; i < sizeof(path) / sizeof(CString); ++i) { std::string imgpath = CW2A(m_ExePath + path[i]); cv::Mat imgMat = cv::imread(imgpath); if (!imgMat.empty()) { MicroImage* img = new MicroImage(); img->m_height = imgMat.rows; img->m_width = imgMat.cols; img->m_ImgName = u8"抓图"+ MicroImage::GetStrTime(); int totalSize = imgMat.total() * imgMat.elemSize(); img->m_OriginData = new uchar[totalSize]; img->m_TotalSize = totalSize; memcpy_s(img->m_OriginData, totalSize, imgMat.data, totalSize); int len = MultiByteToWideChar(CP_UTF8, 0, img->m_ImgName.c_str(), -1, NULL, 0); std::wstring wstr(len, L'\0'); MultiByteToWideChar(CP_UTF8, 0, img->m_ImgName.c_str(), -1, &wstr[0], len); m_ImgVec.emplace_back(img); m_listBox.AddString(wstr.c_str()); } } } void CDN_sampleDlg::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 CDN_sampleDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast(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 function to obtain the cursor to display while the user drags // the minimized window. HCURSOR CDN_sampleDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } void CDN_sampleDlg::OnBtnMinimize() { ShowWindow(SW_MINIMIZE); // 最小化窗口 } //void CDN_sampleDlg::OnBtnMaximize() //{ // if (IsZoomed()) { // ((CButton*)GetDlgItem(IDC_BTN_MAXIMIZE))->SetWindowTextW(L"最大化"); // ShowWindow(SW_RESTORE); // 如果窗口已经最大化,则恢复正常大小 // // } // else { // ((CButton*)GetDlgItem(IDC_BTN_MAXIMIZE))->SetWindowTextW(L"还原"); // ShowWindow(SW_MAXIMIZE); // 否则最大化窗口 // // //m_listBox.SetWindowPos(); // } //} void CDN_sampleDlg::MicroTouchPressedVideo() { //AfxMessageBox(_T("抓拍一张"), MB_ICONHAND); CPictureHolder *pic=new CPictureHolder(); LPPICTUREDISP img = (LPPICTUREDISP)m_video.GrabFrame(); pic->SetPictureDispatch(img); OLE_HANDLE hBitmap; pic->m_pPict->get_Handle((OLE_HANDLE*)&hBitmap); BITMAP bmp; if (GetObject((HBITMAP)hBitmap, sizeof(BITMAP), &bmp)) { MicroImage* mi = new MicroImage(); mi->Init(bmp); m_ImgVec.emplace_back(mi); int len = MultiByteToWideChar(CP_UTF8, 0, mi->m_ImgName.c_str(), -1, NULL, 0); std::wstring wstr(len, L'\0'); MultiByteToWideChar(CP_UTF8, 0, mi->m_ImgName.c_str(), -1, &wstr[0], len); m_listBox.AddString(wstr.c_str()); } delete pic; } void CDN_sampleDlg::ChangeTab(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: 在此添加控件通知处理程序代码 switch (m_tab.GetCurSel()) { case 0: m_f1.ClearImg(); m_f1.ShowWindow(SW_SHOW); m_f2.ShowWindow(SW_HIDE); m_video.ShowWindow(SW_HIDE); break; case 1: m_f2.ClearImg(); m_f2.ShowWindow(SW_SHOW); m_f1.ShowWindow(SW_HIDE); m_video.ShowWindow(SW_HIDE); break; case 2: m_f2.ShowWindow(SW_HIDE); m_f1.ShowWindow(SW_HIDE); m_video.ShowWindow(SW_SHOW); break; default: m_f1.ClearImg(); m_f1.ShowWindow(SW_SHOW); m_f2.ShowWindow(SW_HIDE); m_video.ShowWindow(SW_HIDE); break; } *pResult = 0; } BOOL CDN_sampleDlg::PreTranslateMessage(MSG* pMsg) { if (WM_RBUTTONUP == pMsg->message) { if (m_listBox.GetCount() == 0) return FALSE; CPoint pt; CRect rtClient; m_listBox.GetClientRect(&rtClient); m_listBox.ClientToScreen(&rtClient); INT iScrollPos = m_listBox.GetScrollPos(SB_VERT); GetCursorPos(&pt); int nItemHeight = m_listBox.GetItemHeight(0); int iSelect = (pt.y - rtClient.top + iScrollPos * nItemHeight) / nItemHeight; if (iSelect < m_listBox.GetCount()) { m_listBox.SetCurSel(iSelect); m_listBox.RedrawWindow(); if (rtClient.PtInRect(pt)) { CMenu MyMenu; MyMenu.LoadMenu(IDR_MENU1); MyMenu.GetSubMenu(0)->TrackPopupMenu(TPM_RIGHTBUTTON, pt.x, pt.y, this, NULL); MyMenu.Detach(); } } } return __super::PreTranslateMessage(pMsg); } void CDN_sampleDlg::OnRButtonDown(UINT nFlags, CPoint point) { //CPoint windowPoint = point; //m_listBox.ScreenToClient(&windowPoint); // 将屏幕坐标转换为窗口坐标 //BOOL bOutSide = TRUE; //int index = m_listBox.ItemFromPoint(windowPoint, bOutSide); //if (index != LB_ERR) { // m_listBox.SetCurSel(index); // 设置选中该项 // CMenu menu; // BOOL res = menu.LoadMenuW(IDR_MENU1); // CMenu* pPopup = menu.GetSubMenu(0); // ASSERT(pPopup != nullptr); // CPoint screenPoint; // 假设这里有一个屏幕坐标,例如从 GetCursorPos 获取的坐标 // ::GetCursorPos(&screenPoint); // if (pPopup) pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, screenPoint.x, screenPoint.y, this); //} CDialog::OnRButtonDown(nFlags, point); } void CDN_sampleDlg::OnContextMenu(CWnd* /*pWnd*/, CPoint point) { OnRButtonDown(0, point); } void CDN_sampleDlg::OnDeleteOne() { int count = m_listBox.GetCount(); int index = m_listBox.GetCurSel(); if (index != LB_ERR){ m_listBox.DeleteString(index); if (index < m_ImgVec.size()) { delete m_ImgVec[index]; m_ImgVec.erase(m_ImgVec.begin()+index); if (index == count - 1) index -= 1; if (index <= 0) index = 0; m_listBox.SetCurSel(index); SelectOneImg(); m_f1.ClearImg(); m_f2.ClearImg(); m_f1.Invalidate(); m_f2.Invalidate(); } } } std::string CDN_sampleDlg::Utf8ToAscii(const std::string& str) { int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0); wchar_t* pUnicode = (wchar_t*)malloc(sizeof(wchar_t) * unicodeLen); MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, pUnicode, unicodeLen); int ansiiLen = WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, nullptr, 0, nullptr, nullptr); char* pAssii = (char*)malloc(sizeof(char) * ansiiLen); WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, pAssii, ansiiLen, nullptr, nullptr); std::string ret_str(pAssii); free(pUnicode); free(pAssii); return ret_str; } void CDN_sampleDlg::OnSaveOne() { int index = m_listBox.GetCurSel(); if (index != LB_ERR) { if (index < m_ImgVec.size()) { MicroImage* img = m_ImgVec[index]; cv::Mat imgMat = cv::Mat(img->m_height, img->m_width, CV_8UC3, img->m_OriginData); CString imgpath = m_ExePath + L"\\DoniImage\\"; std::string path = CW2A(imgpath); path += Utf8ToAscii(img->m_ImgName + u8".png"); cv::imwrite(path, imgMat); imgMat.release(); } } }