Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // VideoThreadDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include<iostream>
- #include<stdlib.h>
- #include "SerialPort.h"
- using namespace std;
- #include "VideoThread.h"
- #include "VideoThreadDlg.h"
- #include "afxdialogex.h"
- //
- //#include <opencv\cv.h>
- //#include <opencv\highgui.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- //using namespace cv;
- //using namespace std;
- //
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialogEx
- {
- public:
- CAboutDlg();
- // Dialog Data
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- // Implementation
- protected:
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
- END_MESSAGE_MAP()
- // CVideoThreadDlg dialog
- CVideoThreadDlg::CVideoThreadDlg(CWnd* pParent /*=NULL*/)
- : CDialogEx(CVideoThreadDlg::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CVideoThreadDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_VIDEO, m_Video);
- DDX_Control(pDX, IDC_EDIT_NAME, m_ImgName);
- DDX_Control(pDX, IDOK, m_OK);
- DDX_Control(pDX, IDC_BTN_CAPTURE, m_Capture);
- DDX_Control(pDX, IDC_BTN_SAVE, m_SaveImg);
- // DDX_Control(pDX, IDC_BTN_INCREASE, m_Increase);
- }
- BEGIN_MESSAGE_MAP(CVideoThreadDlg, CDialogEx)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDOK, &CVideoThreadDlg::OnBnClickedOk)
- // ON_BN_CLICKED(IDC_BTN_INCREASE, &CVideoThreadDlg::OnBnClickedBtnIncrease)
- ON_BN_CLICKED(IDC_BTN_SAVE, &CVideoThreadDlg::OnBnClickedBtnSave)
- ON_BN_CLICKED(IDCANCEL, &CVideoThreadDlg::OnBnClickedCancel)
- ON_BN_CLICKED(IDC_BTN_CAPTURE, &CVideoThreadDlg::OnBnClickedBtnCapture)
- ON_MESSAGE (WM_USER_THREAD_FINISHED, OnThreadFinished)
- ON_WM_TIMER()
- END_MESSAGE_MAP()
- // CVideoThreadDlg message handlers
- BOOL CVideoThreadDlg::OnInitDialog()
- {
- CDialogEx::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)
- {
- BOOL bNameValid;
- CString strAboutMenu;
- bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
- ASSERT(bNameValid);
- 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
- SetTimer(1,1000,NULL);
- // TODO: Add extra initialization here
- control = 0;
- num = 0;
- bSave = false;
- bImageSaved = false;
- bCapture = false;
- bExitThread = false;
- m_SaveImg.EnableWindow(false);
- m_Capture.EnableWindow(false);
- portName = "\\\\.\\COM7";
- serial = new SerialPort(portName); //SerialPort::SerialPort(char*portName) //(1)
- if(serial->isConnected()) //bool SerialPort::isConnected() //(2)
- {
- cout << "Mission accomplish"<< endl;
- }
- else
- {
- cout << "Check the connection"<<endl;
- }
- //while (serial->isConnected())
- //{
- // int read_result = serial ->readSerialPort(incomingData, MAX_DATA_LENGTH); //check if data has been read or not
- // puts(incomingData); //print out data
- // Sleep(10); //Wait for a bit
- //
- //}
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CVideoThreadDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialogEx::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 CVideoThreadDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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;
- switch(control)
- {
- case 0:
- dc.Ellipse(260,150,380,30); //Head
- dc.Rectangle(250,150,380,400);//square body
- dc.Rectangle(70,170,250,220);//left arm
- dc.Rectangle(70,280,130,220); //additional left arm
- dc.Rectangle(380,170,570,220);//right arm
- dc.Rectangle(510,280,570,220);//additional right arm
- dc.Rectangle(250,400,305,600); //left leg
- dc.Rectangle(180,600,250,560);//additional left leg
- dc.Rectangle(320,400,380,600); //right leg
- dc.Rectangle(380,560,440,600);//additional right leg
- break;
- case 1:
- dc.Ellipse(260,150,380,30); //Head
- dc.Rectangle(250,150,380,400);//square body
- //--------------------------------------------------------------------------------------------------------
- dc.MoveTo(250,190);
- dc.LineTo(120,50);
- dc.MoveTo(88,80);
- dc.LineTo(120,50);
- dc.MoveTo(88,77);
- dc.LineTo(250,250);
- //left arms
- dc.MoveTo(63,27);
- dc.LineTo(90,80);
- dc.MoveTo(110,7);
- dc.LineTo(65,30);
- dc.MoveTo(110,10);
- dc.LineTo(130,60);
- //left arm joint
- //--------------------------------------------------------------------------------------------------------
- dc.MoveTo(500,48);
- dc.LineTo(380,195);
- dc.MoveTo(500,50);
- dc.LineTo(550,70);
- dc.MoveTo(535,60);
- dc.LineTo(380,255);
- //right arms
- dc.MoveTo(550,15);
- dc.LineTo(520,90);
- dc.MoveTo(500,10);
- dc.LineTo(480,70);
- dc.MoveTo(500,10);
- dc.LineTo(550,20);
- //right arm joint
- //--------------------------------------------------------------------------------------------------------
- dc.MoveTo(105,590);
- dc.LineTo(252,395);
- dc.MoveTo(148,605);
- dc.LineTo(110,580);
- dc.MoveTo(145,610);
- dc.LineTo(304,400);
- //left leg
- dc.MoveTo(150,610);
- dc.LineTo(80,640);
- dc.MoveTo(50,610);
- dc.LineTo(80,640);
- dc.MoveTo(120,580);
- dc.LineTo(50,615);
- //left leg joint
- //--------------------------------------------------------------------------------------------------------
- dc.MoveTo(505,540);
- dc.LineTo(380,400);
- dc.MoveTo(515,535);
- dc.LineTo(480,580);
- dc.MoveTo(323,400);
- dc.LineTo(477,580);
- //right leg
- dc.MoveTo(580,570);
- dc.LineTo(505,540);
- dc.MoveTo(580,560);
- dc.LineTo(545,615);
- dc.MoveTo(550,605);
- dc.LineTo(465,570);
- //right leg joint
- break;
- //--------------------------------------------------------------------------------------------------------
- case 2:
- dc.Ellipse(260,150,380,30); //Head
- dc.Rectangle(250,150,380,400);//square body
- //-------------------------------------------------------------------------------------------------------
- dc.MoveTo(250,170);
- dc.LineTo(90,330);
- dc.MoveTo(90,330);
- dc.LineTo(130,365);
- dc.MoveTo(250,245);
- dc.LineTo(125,368);
- //left arms
- dc.MoveTo(130,420);
- dc.LineTo(130,360);
- dc.MoveTo(90,420);
- dc.LineTo(130, 420);
- dc.MoveTo(90,420);
- dc.LineTo(90,325);
- //left arms joints
- //-------------------------------------------------------------------------------------------------------
- dc.MoveTo(550,310);
- dc.LineTo(380,170);
- dc.MoveTo(510,350);
- dc.LineTo(555,305);
- dc.MoveTo(510,350);
- dc.LineTo(380,240);
- //right arms
- dc.MoveTo(510,410);
- dc.LineTo(510,350);
- dc.MoveTo(550,405);
- dc.LineTo(510,410);
- dc.MoveTo(550,405);
- dc.LineTo(550,310);
- //right arms joints
- //-------------------------------------------------------------------------------------------------------
- dc.Rectangle(250,400,305,600); //left leg
- dc.Rectangle(180,600,250,560);//additional left leg
- //--------------------------------------------------------------------------------------------------------
- dc.Rectangle(320,400,380,600); //right leg
- dc.Rectangle(380,560,440,600);//additional right leg
- break;
- //--------------------------------------------------------------------------------------------------------
- case 3:
- dc.Ellipse(260,150,380,30); //Head
- dc.Rectangle(250,150,380,400);//square body
- //-------------------------------------------------------------------------------------------------------
- dc.Rectangle(70,170,250,220);//left arm
- dc.Rectangle(70,280,130,220); //additional left arm
- dc.Rectangle(380,170,570,220);//right arm
- dc.Rectangle(510,110,570,170);//additional right arm
- //-------------------------------------------------------------------------------------------------------
- dc.Rectangle(100,400,290,450); //left leg
- dc.Rectangle(100,450,150,510);//additional left leg
- //-------------------------------------------------------------------------------------------------------
- dc.Rectangle(320,400,380,600); //right leg
- dc.Rectangle(380,560,440,600);//additional right leg
- //-------------------------------------------------------------------------------------------------------
- break;
- case 4:
- dc.Ellipse(260,150,380,30); //Head
- dc.Rectangle(250,150,380,400);//square body
- //-------------------------------------------------------------------------------------------------------
- dc.Rectangle(70,170,250,220);//left arm
- dc.Rectangle(70,170,130,100); //additional left arm
- dc.Rectangle(380,170,570,220);//right arm
- dc.Rectangle(510,280,570,220);//additional right arm
- dc.Rectangle(250,400,305,600); //left leg
- dc.Rectangle(180,600,250,560);//additional left leg
- dc.Rectangle(330,400,540,450); //right leg
- dc.Rectangle(490,450,540,500);//additional right leg
- break;
- case 5:
- dc.Ellipse(260,150,380,30); //Head
- dc.Rectangle(250,150,380,400);//square body
- //-------------------------------------------------------------------------------------------------------
- dc.Rectangle(70,170,250,220);//left arm
- dc.Rectangle(70,170,130,110); //additional left arm
- dc.Rectangle(380,170,570,220);//right arm
- dc.Rectangle(510,110,570,170);//additional right arm
- //-------------------------------------------------------------------------------------------------------
- dc.Rectangle(100,400,290,450); //left leg
- dc.Rectangle(100,450,150,510);//additional left leg
- //-------------------------------------------------------------------------------------------------------
- dc.Rectangle(330,400,540,450); //right leg
- dc.Rectangle(490,450,540,500);//additional right leg
- //-------------------------------------------------------------------------------------------------------
- break;
- case 6:
- dc.Ellipse(260,150,380,30); //Head
- dc.Rectangle(250,150,380,400);//square body
- //-------------------------------------------------------------------------------------------------------
- dc.MoveTo(250,170);
- dc.LineTo(90,330);
- dc.MoveTo(90,330);
- dc.LineTo(130,365);
- dc.MoveTo(250,245);
- dc.LineTo(125,368);
- //left arms
- dc.MoveTo(130,420);
- dc.LineTo(130,360);
- dc.MoveTo(90,420);
- dc.LineTo(130, 420);
- dc.MoveTo(90,420);
- dc.LineTo(90,325);
- //left arms joints
- dc.MoveTo(550,310);
- dc.LineTo(380,170);
- dc.MoveTo(510,350);
- dc.LineTo(555,305);
- dc.MoveTo(510,350);
- dc.LineTo(380,240);
- //right arms
- dc.MoveTo(510,410);
- dc.LineTo(510,350);
- dc.MoveTo(550,405);
- dc.LineTo(510,410);
- dc.MoveTo(550,405);
- dc.LineTo(550,310);
- //right arms joints
- //-------------------------------------------------------------------------------------------------------
- dc.Rectangle(100,400,290,450); //left leg
- dc.Rectangle(100,450,150,510);//additional left leg
- //-------------------------------------------------------------------------------------------------------
- dc.Rectangle(330,400,540,450); //right leg
- dc.Rectangle(490,450,540,500);//additional right leg
- //-------------------------------------------------------------------------------------------------------
- break;
- case 7:
- dc.Ellipse(260,150,380,30); //Head
- dc.Rectangle(250,150,380,400);//square body
- //-------------------------------------------------------------------------------------------------------
- dc.MoveTo(250,190);
- dc.LineTo(120,50);
- dc.MoveTo(88,80);
- dc.LineTo(120,50);
- dc.MoveTo(88,77);
- dc.LineTo(250,250);
- //left arms
- dc.MoveTo(63,27);
- dc.LineTo(90,80);
- dc.MoveTo(110,7);
- dc.LineTo(65,30);
- dc.MoveTo(110,10);
- dc.LineTo(130,60);
- //left arm joint
- //--------------------------------------------------------------------------------------------------------
- dc.MoveTo(500,48);
- dc.LineTo(380,195);
- dc.MoveTo(500,50);
- dc.LineTo(550,70);
- dc.MoveTo(535,60);
- dc.LineTo(380,255);
- //right arms
- dc.MoveTo(550,15);
- dc.LineTo(520,90);
- dc.MoveTo(500,10);
- dc.LineTo(480,70);
- dc.MoveTo(500,10);
- dc.LineTo(550,20);
- //right arm joint
- //-------------------------------------------------------------------------------------------------------
- dc.Rectangle(100,400,290,450); //left leg
- dc.Rectangle(100,450,150,510);//additional left leg
- //-------------------------------------------------------------------------------------------------------
- dc.Rectangle(330,400,540,450); //right leg
- dc.Rectangle(490,450,540,500);//additional right leg
- //-------------------------------------------------------------------------------------------------------
- break;
- case 8:
- dc.Ellipse(260,150,380,30); //Head
- dc.Rectangle(250,150,380,400);//square body
- //-------------------------------------------------------------------------------------------------------
- dc.MoveTo(250,190);
- dc.LineTo(120,50);
- dc.MoveTo(88,80);
- dc.LineTo(120,50);
- dc.MoveTo(88,77);
- dc.LineTo(250,250);
- //left arms
- dc.MoveTo(63,27);
- dc.LineTo(90,80);
- dc.MoveTo(110,7);
- dc.LineTo(65,30);
- dc.MoveTo(110,10);
- dc.LineTo(130,60);
- //left arm joint
- //--------------------------------------------------------------------------------------------------------
- dc.MoveTo(550,310);
- dc.LineTo(380,170);
- dc.MoveTo(510,350);
- dc.LineTo(555,305);
- dc.MoveTo(510,350);
- dc.LineTo(380,240);
- //right arms
- dc.MoveTo(510,410);
- dc.LineTo(510,350);
- dc.MoveTo(550,405);
- dc.LineTo(510,410);
- dc.MoveTo(550,405);
- dc.LineTo(550,310);
- //right arms joints
- //-------------------------------------------------------------------------------------------------------
- dc.Rectangle(100,400,290,450); //left leg
- dc.Rectangle(100,450,150,510);//additional left leg
- //-------------------------------------------------------------------------------------------------------
- dc.Rectangle(320,400,380,600); //right leg
- dc.Rectangle(380,560,440,600);//additional right leg
- //-------------------------------------------------------------------------------------------------------
- break;
- case 9:
- dc.Ellipse(260,150,380,30); //Head
- dc.Rectangle(250,150,380,400);//square body
- //-------------------------------------------------------------------------------------------------------
- dc.MoveTo(250,170);
- dc.LineTo(90,330);
- dc.MoveTo(90,330);
- dc.LineTo(130,365);
- dc.MoveTo(250,245);
- dc.LineTo(125,368);
- //left arms
- dc.MoveTo(130,420);
- dc.LineTo(130,360);
- dc.MoveTo(90,420);
- dc.LineTo(130, 420);
- dc.MoveTo(90,420);
- dc.LineTo(90,325);
- //left arms joints
- dc.MoveTo(500,48);
- dc.LineTo(380,195);
- dc.MoveTo(500,50);
- dc.LineTo(550,70);
- dc.MoveTo(535,60);
- dc.LineTo(380,255);
- //right arms
- dc.MoveTo(550,15);
- dc.LineTo(520,90);
- dc.MoveTo(500,10);
- dc.LineTo(480,70);
- dc.MoveTo(500,10);
- dc.LineTo(550,20);
- //right arm joint
- dc.Rectangle(250,400,305,600); //left leg
- dc.Rectangle(180,600,250,560);//additional left leg
- dc.Rectangle(330,400,540,450); //right leg
- dc.Rectangle(490,450,540,500);//additional right leg
- break;
- }
- CDialogEx::OnPaint();
- }
- }
- // The system calls this function to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CVideoThreadDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- UINT CVideoThreadDlg::StartThread (LPVOID param)
- {
- /*THREADSTRUCT* ts = (THREADSTRUCT*)param;
- //here is the time-consuming process
- //which interacts with your dialog
- AfxMessageBox (_T("Thread is started!"));
- // TODO: Add your control notification handler code here
- cv::VideoCapture cap(0);
- //VideoCapture cap("C:/Users/lsf-admin/Pictures/Camera Roll/video000.mp4");
- cv::vector<cv::Rect> faces;
- cv::Mat frame;
- cv::Mat graySacleFrame;
- cv::Mat original;
- if (!cap.isOpened())
- {
- AfxMessageBox(_T("Failed to load video file"));
- //return;
- //return exit(1);
- }
- while (true)
- {
- cap >> frame;
- if (!frame.empty()){
- //clone from original frame
- original = frame.clone();
- }
- CDC* vDC;
- vDC =ts->_this->GetDlgItem(IDC_VIDEO)->GetDC();
- CRect rect;
- ts->_this->GetDlgItem(IDC_VIDEO)->GetClientRect(&rect);
- IplImage* image2=cvCloneImage(&(IplImage)original);
- ts->_this->DisplayIplImageToPictureBox(image2, vDC, rect); //img is IplImage* variable.
- ts->_this->ReleaseDC(vDC);
- if (cv::waitKey(30) >= 0) break;
- }
- */
- /* VideoCapture cap(0); // open the video camera no. 0
- cv::vector<cv::Rect> faces;
- cv::Mat frame;
- cv::Mat graySacleFrame;
- cv::Mat original;
- if (!cap.isOpened()) // if not success, exit program
- {
- cout << "Cannot open the video cam" << endl;
- return -1;
- }
- double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
- double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
- cout << "Frame size : " << dWidth << " x " << dHeight << endl;
- namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
- int counter=0;
- char str[80];
- while (1)
- {
- Mat frame;
- bool bSuccess = cap.read(frame); // read a new frame from video
- sprintf(str, "img%d.png",counter++);
- String name(str);//Format("img%04d.png", counter++);; // NEW !
- if (!bSuccess) //if not success, break loop
- {
- cout << "Cannot read a frame from video stream" << endl;
- break;
- }
- // cap >> frame;
- if( frame.empty() ) break; // end of video stream
- CDC* vDC;
- THREADSTRUCT* ts = (THREADSTRUCT*)param;
- vDC =ts->_this->GetDlgItem(IDC_VIDEO)->GetDC();
- CRect rect;
- ts->_this->GetDlgItem(IDC_VIDEO)->GetClientRect(&rect);
- IplImage* image2=cvCloneImage(&(IplImage)original);
- // ts->_this->DisplayIplImageToPictureBox(image2, vDC, rect); //img is IplImage* variable.
- ts->_this->ReleaseDC(vDC);
- imshow("MyVideo", frame); //show the frame in "MyVideo" window
- // imwrite(name, frame);
- if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
- {
- cout << "esc key is pressed by user" << endl;
- break;
- }
- _sleep(10);
- }
- */
- // TODO: 在此添加控件通知处理程序代码
- int counter=0;
- char str[80];
- sprintf(str, "img%d.png",counter++);
- String name(str);//Format("img%04d.png", counter++);; // NEW !
- CRect rect;
- Mat srcImage;
- THREADSTRUCT* ts = (THREADSTRUCT*)param;
- HWND wnd = ts->hWnd;
- VideoCapture Matcapture(0);
- double dWidth = Matcapture.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
- double dHeight = Matcapture.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
- rect.left = 0;
- rect.right=0;
- rect.right=dWidth;
- rect.bottom=dHeight;
- ts->_this->GetDlgItem(IDC_VIDEO)->GetClientRect(rect);//获取Picture control控件区域大小
- // ts->_this->GetDlgCtrlID()->SetClientRect(rect);
- // m_Video.
- // ts->_this->GetDlgItem(IDC_VIDEO)->SetClientRect(rect);
- // pctBox->Size = System::Drawing::Size(200, 150);
- namedWindow("aaa",CV_WINDOW_NORMAL);//创建窗口 正常显示
- resizeWindow("aaa",rect.Width(),rect.Height());//调整窗口大小为Picture control控件区域大小
- HWND hWnd = (HWND)cvGetWindowHandle("aaa");//获得创建的窗口句柄
- HWND hParent = ::GetParent(hWnd);//获得其父句柄
- ::SetParent(hWnd, ts->_this->GetDlgItem(IDC_VIDEO)->m_hWnd);//设置创建窗口的父窗口为Picture control控件
- ::ShowWindow(hParent, SW_HIDE);//将父窗口及其子窗口进行隐藏
- HANDLE hTerminateEvent = ts->hEvent;
- bool terminate = false;
- while (!terminate)
- {
- if(ts->_this->bExitThread){
- ts->_this->bExitThread = false;
- }
- DWORD retVal = WaitForSingleObject(hTerminateEvent, 0);
- switch(retVal)
- {
- case WAIT_OBJECT_0:
- // cout << "Terminate Event signalled" << endl;
- terminate = true;
- break;
- case WAIT_TIMEOUT:
- // cout << "Keep running..." << endl;
- break;
- case WAIT_FAILED:
- // cerr << "WaitForSingleObject() failed" << endl;
- terminate = true;
- break;
- }
- Matcapture>>srcImage;
- imshow("aaa",srcImage);
- if(ts->_this->bCapture){
- ts->_this->bCapture = false;
- // ts->_this->showSavedImage(CString(str));
- ts->_this->DrawPicToHDC(srcImage);
- ts->_this->capturedImage=srcImage;
- }
- if (waitKey(10)>=0)
- {
- break;
- }
- }
- //you can also call AfxEndThread() here
- return 1;
- }
- void CVideoThreadDlg::OnBnClickedOk()
- {
- // TODO: Add your control notification handler code here
- hTerminateEvent = CreateEvent(0, FALSE, FALSE, 0);
- THREADSTRUCT *_param = new THREADSTRUCT;
- _param->_this = this;
- _param->hWnd = m_hWnd;
- _param->hEvent = hTerminateEvent;
- pThread = AfxBeginThread (StartThread, _param);
- pThread->m_bAutoDelete = true;
- pThread->ResumeThread();
- m_Capture.EnableWindow(true);
- // CDialogEx::OnOK();
- }
- void CVideoThreadDlg::OnBnClickedBtnSave()
- {
- // TODO: Add your control notification handler code here
- CString name;
- m_ImgName.GetWindowTextW(name);
- name = _T("image\\")+name+_T(".jpg");
- std::string imgName = CStringA(name);
- imwrite(imgName, capturedImage);
- m_SaveImg.EnableWindow(false);
- bSave = true;
- }
- void CVideoThreadDlg::showSavedImage(CString imgName){
- // Convert a TCHAR string to a LPCSTR
- CT2CA pszConvertedAnsiString (imgName);
- // construct a std::string using the LPCSTR input
- std::string strStd (pszConvertedAnsiString);
- Mat img = imread(strStd);
- imshow("bbb",img);
- }
- void CVideoThreadDlg::OnBnClickedCancel()
- {
- // TODO: Add your control notification handler code here
- if(!SetEvent(hTerminateEvent))
- {
- cerr << "SetEvent() failed" << endl;
- }
- bExitThread = true;
- DWORD retVal = WaitForSingleObject(pThread->m_hThread, INFINITE);
- switch(retVal)
- {
- case WAIT_OBJECT_0:
- // cout << "Thread terminated" << endl;
- break;
- case WAIT_FAILED:
- // cerr << "WaitForSingleObject() failed" << endl;
- break;
- }
- DWORD exit_code= NULL;
- if (pThread != NULL)
- {
- GetExitCodeThread(pThread->m_hThread, &exit_code);
- if(exit_code == STILL_ACTIVE)
- {
- ::TerminateThread(pThread->m_hThread, 0);
- CloseHandle(pThread->m_hThread);
- CloseHandle(hTerminateEvent);
- }
- // pThread->m_hThread = NULL;
- // pThread = NULL;
- }
- CDialogEx::OnCancel();
- }
- void CVideoThreadDlg::AdjustAspectImageSize( const Size& imageSize,
- const Size& destSize,
- Size& newSize )
- {
- double destAspectRatio = float( destSize.width ) / float( destSize.height );
- double imageAspectRatio = float( imageSize.width ) / float( imageSize.height );
- if ( imageAspectRatio > destAspectRatio )
- {
- // Margins on top/bottom
- newSize.width = destSize.width;
- newSize.height = int( imageSize.height *
- ( double( destSize.width ) / double( imageSize.width ) ) );
- }
- else
- {
- // Margins on left/right
- newSize.height = destSize.height;
- newSize.width = int( imageSize.width *
- ( double( destSize.height ) / double( imageSize.height ) ) );
- }
- }
- void CVideoThreadDlg::DrawPicToHDC( Mat cvImg,
- bool bMaintainAspectRatio /* =true*/ )
- {
- // Get the HDC handle information from the ID passed
- CDC* pDC = GetDlgItem(IDC_IMAGE)->GetDC();
- HDC hDC = pDC->GetSafeHdc();
- CRect rect;
- GetDlgItem(IDC_IMAGE)->GetClientRect(rect);
- Size winSize( rect.right, rect.bottom );
- // Calculate the size of the image that
- // will fit in the control rectangle.
- Size origImageSize( cvImg.cols, cvImg.rows );
- Size imageSize;
- int offsetX;
- int offsetY;
- if ( ! bMaintainAspectRatio )
- {
- // Image should be the same size as the control's rectangle
- imageSize = winSize;
- }
- else
- {
- Size newSize;
- AdjustAspectImageSize( origImageSize,
- winSize,
- imageSize );
- }
- offsetX = ( winSize.width - imageSize.width ) / 2;
- offsetY = ( winSize.height - imageSize.height ) / 2;
- // Resize the source to the size of the destination image if necessary
- Mat cvImgTmp;
- resize( cvImg,
- cvImgTmp,
- imageSize,
- 0,
- 0,
- INTER_AREA );
- // To handle our Mat object of this width, the source rows must
- // be even multiples of a DWORD in length to be compatible with
- // SetDIBits(). Calculate what the correct byte width of the
- // row should be to be compatible with SetDIBits() below.
- int stride = ( ( ( ( imageSize.width * 24 ) + 31 ) & ~31 ) >> 3 );
- // Allocate a buffer for our DIB bits
- uchar* pcDibBits = (uchar*) malloc( imageSize.height * stride );
- if ( pcDibBits != NULL )
- {
- // Copy the raw pixel data over to our dibBits buffer.
- // NOTE: Can setup cvImgTmp to add the padding to skip this.
- for ( int row = 0; row < cvImgTmp.rows; ++row )
- {
- // Get pointers to the beginning of the row on both buffers
- uchar* pcSrcPixel = cvImgTmp.ptr<uchar>(row);
- uchar* pcDstPixel = pcDibBits + ( row * stride );
- // We can just use memcpy
- memcpy( pcDstPixel,
- pcSrcPixel,
- stride );
- }
- // Initialize the BITMAPINFO structure
- BITMAPINFO bitInfo;
- bitInfo.bmiHeader.biBitCount = 24;
- bitInfo.bmiHeader.biWidth = cvImgTmp.cols;
- bitInfo.bmiHeader.biHeight = -cvImgTmp.rows;
- bitInfo.bmiHeader.biPlanes = 1;
- bitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- bitInfo.bmiHeader.biCompression = BI_RGB;
- bitInfo.bmiHeader.biClrImportant = 0;
- bitInfo.bmiHeader.biClrUsed = 0;
- bitInfo.bmiHeader.biSizeImage = 0; //winSize.height * winSize.width * * 3;
- bitInfo.bmiHeader.biXPelsPerMeter = 0;
- bitInfo.bmiHeader.biYPelsPerMeter = 0;
- // Add header and OPENCV image's data to the HDC
- StretchDIBits( hDC,
- offsetX,
- offsetY,
- cvImgTmp.cols,
- cvImgTmp.rows,
- 0,
- 0,
- cvImgTmp.cols,
- cvImgTmp.rows,
- pcDibBits,
- & bitInfo,
- DIB_RGB_COLORS,
- SRCCOPY );
- free(pcDibBits);
- }
- ReleaseDC(pDC);
- }
- void CVideoThreadDlg::OnBnClickedBtnCapture()
- {
- // TODO: Add your control notification handler code here
- bCapture = true;
- m_SaveImg.EnableWindow(true);
- }
- LONG CVideoThreadDlg::OnThreadFinished (WPARAM wParam, LPARAM lParam)
- {
- CDialogEx::OnCancel();
- return 0;
- }
- void CVideoThreadDlg::OnTimer(UINT_PTR nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- char buffer[20];//store a string up to 20 characters long.
- //the size of 20.
- //passing the data
- int buf_size = 20;//only positive vaule (char in some compilers)
- serial ->readSerialPort(buffer,20);//(char *buffer, unsigned int buf_size) //(3)
- control = atoi(buffer);
- Invalidate();
- CDialogEx::OnTimer(nIDEvent);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement