Advertisement
Guest User

rac robotic

a guest
Jan 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 30.18 KB | None | 0 0
  1.  
  2. // VideoThreadDlg.cpp : implementation file
  3. //
  4.  
  5. #include "stdafx.h"
  6. #include<iostream>
  7. #include<stdlib.h>
  8. #include "SerialPort.h"
  9.  
  10. using namespace std;
  11.  
  12. #include "VideoThread.h"
  13. #include "VideoThreadDlg.h"
  14. #include "afxdialogex.h"
  15. //
  16. //#include <opencv\cv.h>
  17. //#include <opencv\highgui.h>
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #endif
  22.  
  23. //using namespace cv;    
  24. //using namespace std;
  25. //
  26. // CAboutDlg dialog used for App About
  27.  
  28.             class CAboutDlg : public CDialogEx
  29.             {
  30.             public:
  31.                 CAboutDlg();
  32.  
  33.             // Dialog Data
  34.                 enum { IDD = IDD_ABOUTBOX };
  35.  
  36.                 protected:
  37.                 virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  38.  
  39.             // Implementation
  40.             protected:
  41.                 DECLARE_MESSAGE_MAP()
  42.             };
  43.  
  44.             CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
  45.             {
  46.             }
  47.  
  48.             void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  49.             {
  50.                 CDialogEx::DoDataExchange(pDX);
  51.             }
  52.  
  53.             BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
  54.             END_MESSAGE_MAP()
  55.  
  56.  
  57. // CVideoThreadDlg dialog
  58.  
  59.  
  60.  
  61.  
  62.             CVideoThreadDlg::CVideoThreadDlg(CWnd* pParent /*=NULL*/)
  63.                 : CDialogEx(CVideoThreadDlg::IDD, pParent)
  64.             {
  65.                 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  66.             }
  67.  
  68.             void CVideoThreadDlg::DoDataExchange(CDataExchange* pDX)
  69.             {
  70.                 CDialogEx::DoDataExchange(pDX);
  71.                 DDX_Control(pDX, IDC_VIDEO, m_Video);
  72.                 DDX_Control(pDX, IDC_EDIT_NAME, m_ImgName);
  73.                 DDX_Control(pDX, IDOK, m_OK);
  74.                 DDX_Control(pDX, IDC_BTN_CAPTURE, m_Capture);
  75.                 DDX_Control(pDX, IDC_BTN_SAVE, m_SaveImg);
  76.             //  DDX_Control(pDX, IDC_BTN_INCREASE, m_Increase);
  77.             }
  78.  
  79.             BEGIN_MESSAGE_MAP(CVideoThreadDlg, CDialogEx)
  80.                 ON_WM_SYSCOMMAND()
  81.                 ON_WM_PAINT()
  82.                 ON_WM_QUERYDRAGICON()
  83.                 ON_BN_CLICKED(IDOK, &CVideoThreadDlg::OnBnClickedOk)
  84.             //  ON_BN_CLICKED(IDC_BTN_INCREASE, &CVideoThreadDlg::OnBnClickedBtnIncrease)
  85.                 ON_BN_CLICKED(IDC_BTN_SAVE, &CVideoThreadDlg::OnBnClickedBtnSave)
  86.                 ON_BN_CLICKED(IDCANCEL, &CVideoThreadDlg::OnBnClickedCancel)
  87.                 ON_BN_CLICKED(IDC_BTN_CAPTURE, &CVideoThreadDlg::OnBnClickedBtnCapture)
  88.                 ON_MESSAGE (WM_USER_THREAD_FINISHED, OnThreadFinished)
  89.                 ON_WM_TIMER()
  90.             END_MESSAGE_MAP()
  91.  
  92.  
  93. // CVideoThreadDlg message handlers
  94.  
  95.                 BOOL CVideoThreadDlg::OnInitDialog()
  96.                 {
  97.                     CDialogEx::OnInitDialog();
  98.  
  99.                     // Add "About..." menu item to system menu.
  100.  
  101.                     // IDM_ABOUTBOX must be in the system command range.
  102.                     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  103.                     ASSERT(IDM_ABOUTBOX < 0xF000);
  104.  
  105.                     CMenu* pSysMenu = GetSystemMenu(FALSE);
  106.                     if (pSysMenu != NULL)
  107.                     {
  108.                         BOOL bNameValid;
  109.                         CString strAboutMenu;
  110.                         bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
  111.                         ASSERT(bNameValid);
  112.                         if (!strAboutMenu.IsEmpty())
  113.                         {
  114.                             pSysMenu->AppendMenu(MF_SEPARATOR);
  115.                             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  116.                         }
  117.                     }
  118.  
  119.                     // Set the icon for this dialog.  The framework does this automatically
  120.                     //  when the application's main window is not a dialog
  121.                     SetIcon(m_hIcon, TRUE);         // Set big icon
  122.                     SetIcon(m_hIcon, FALSE);        // Set small icon
  123.                     SetTimer(1,1000,NULL);
  124.                     // TODO: Add extra initialization here
  125.                     control = 0;
  126.                     num = 0;
  127.                     bSave = false;
  128.                     bImageSaved = false;
  129.                     bCapture = false;
  130.                     bExitThread = false;
  131.                     m_SaveImg.EnableWindow(false);
  132.                     m_Capture.EnableWindow(false);
  133.  
  134.                 portName = "\\\\.\\COM7";
  135.                 serial = new SerialPort(portName); //SerialPort::SerialPort(char*portName)  //(1)
  136.            
  137.                
  138.                 if(serial->isConnected()) //bool SerialPort::isConnected()    //(2)
  139.                 {
  140.                     cout << "Mission accomplish"<< endl;
  141.  
  142.                 }
  143.                 else
  144.                 {
  145.                     cout << "Check the connection"<<endl;
  146.                 }
  147.  
  148.                 //while (serial->isConnected())
  149.                 //{
  150.                 //  int read_result = serial ->readSerialPort(incomingData, MAX_DATA_LENGTH); //check if data has been read or not
  151.                 //  puts(incomingData); //print out data
  152.                 //  Sleep(10); //Wait for a bit
  153.                 // 
  154.                 //}
  155.            
  156.                     return TRUE;  // return TRUE  unless you set the focus to a control
  157.                 }
  158.  
  159.  
  160.  
  161.  
  162.                 void CVideoThreadDlg::OnSysCommand(UINT nID, LPARAM lParam)
  163.                 {
  164.                     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  165.                     {
  166.                         CAboutDlg dlgAbout;
  167.                         dlgAbout.DoModal();
  168.                     }
  169.                     else
  170.                     {
  171.                         CDialogEx::OnSysCommand(nID, lParam);
  172.                     }
  173.                 }
  174.  
  175.  
  176.  
  177.  
  178.  
  179. // If you add a minimize button to your dialog, you will need the code below
  180. //  to draw the icon.  For MFC applications using the document/view model,
  181. //  this is automatically done for you by the framework.
  182.  
  183.         void CVideoThreadDlg::OnPaint()
  184.         {
  185.             if (IsIconic())
  186.             {
  187.                
  188.                 CPaintDC dc(this); // device context for painting
  189.  
  190.                 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  191.  
  192.                 // Center icon in client rectangle
  193.                 int cxIcon = GetSystemMetrics(SM_CXICON);
  194.                 int cyIcon = GetSystemMetrics(SM_CYICON);
  195.                 CRect rect;
  196.                 GetClientRect(&rect);
  197.                 int x = (rect.Width() - cxIcon + 1) / 2;
  198.                 int y = (rect.Height() - cyIcon + 1) / 2;
  199.  
  200.         switch(control)
  201.             {
  202.  
  203.                 case 0:
  204.        
  205.                     dc.Ellipse(260,150,380,30); //Head
  206.                     dc.Rectangle(250,150,380,400);//square body
  207.                     dc.Rectangle(70,170,250,220);//left arm
  208.                     dc.Rectangle(70,280,130,220); //additional left arm
  209.                     dc.Rectangle(380,170,570,220);//right arm
  210.                     dc.Rectangle(510,280,570,220);//additional right arm
  211.                     dc.Rectangle(250,400,305,600); //left leg
  212.                     dc.Rectangle(180,600,250,560);//additional left leg
  213.                     dc.Rectangle(320,400,380,600); //right leg
  214.                     dc.Rectangle(380,560,440,600);//additional right leg
  215.                     break;
  216.  
  217.                     case 1:
  218.                    
  219.                     dc.Ellipse(260,150,380,30); //Head
  220.                     dc.Rectangle(250,150,380,400);//square body
  221. //--------------------------------------------------------------------------------------------------------
  222.                     dc.MoveTo(250,190);
  223.                     dc.LineTo(120,50);
  224.                     dc.MoveTo(88,80);
  225.                     dc.LineTo(120,50);
  226.                     dc.MoveTo(88,77);
  227.                     dc.LineTo(250,250);
  228.                     //left arms
  229.                     dc.MoveTo(63,27);
  230.                     dc.LineTo(90,80);
  231.                     dc.MoveTo(110,7);
  232.                     dc.LineTo(65,30);
  233.                     dc.MoveTo(110,10);
  234.                     dc.LineTo(130,60);
  235.                     //left arm joint
  236. //--------------------------------------------------------------------------------------------------------
  237.                     dc.MoveTo(500,48);
  238.                     dc.LineTo(380,195);
  239.                     dc.MoveTo(500,50);
  240.                     dc.LineTo(550,70);
  241.                     dc.MoveTo(535,60);
  242.                     dc.LineTo(380,255);
  243.                     //right arms
  244.                     dc.MoveTo(550,15);
  245.                     dc.LineTo(520,90);
  246.                     dc.MoveTo(500,10);
  247.                     dc.LineTo(480,70);                 
  248.                     dc.MoveTo(500,10);
  249.                     dc.LineTo(550,20);
  250.                     //right arm joint
  251.                    
  252. //--------------------------------------------------------------------------------------------------------                 
  253.                     dc.MoveTo(105,590);
  254.                     dc.LineTo(252,395);
  255.                     dc.MoveTo(148,605);
  256.                     dc.LineTo(110,580);
  257.                     dc.MoveTo(145,610);
  258.                     dc.LineTo(304,400);
  259.                     //left leg
  260.                     dc.MoveTo(150,610);
  261.                     dc.LineTo(80,640);
  262.                     dc.MoveTo(50,610);
  263.                     dc.LineTo(80,640);
  264.                     dc.MoveTo(120,580);
  265.                     dc.LineTo(50,615);
  266.                     //left leg joint
  267. //--------------------------------------------------------------------------------------------------------
  268.                     dc.MoveTo(505,540);
  269.                     dc.LineTo(380,400);
  270.                     dc.MoveTo(515,535);
  271.                     dc.LineTo(480,580);
  272.                     dc.MoveTo(323,400);
  273.                     dc.LineTo(477,580);
  274.                     //right leg
  275.                     dc.MoveTo(580,570);
  276.                     dc.LineTo(505,540);
  277.                     dc.MoveTo(580,560);
  278.                     dc.LineTo(545,615);
  279.                     dc.MoveTo(550,605);
  280.                     dc.LineTo(465,570);
  281.                     //right leg joint
  282.                     break;
  283. //--------------------------------------------------------------------------------------------------------
  284.                     case 2:
  285.                     dc.Ellipse(260,150,380,30); //Head
  286.                     dc.Rectangle(250,150,380,400);//square body
  287. //-------------------------------------------------------------------------------------------------------
  288.                     dc.MoveTo(250,170);
  289.                     dc.LineTo(90,330);
  290.                     dc.MoveTo(90,330);
  291.                     dc.LineTo(130,365);            
  292.                     dc.MoveTo(250,245);
  293.                     dc.LineTo(125,368);
  294.                     //left arms
  295.                     dc.MoveTo(130,420);
  296.                     dc.LineTo(130,360);
  297.                     dc.MoveTo(90,420);
  298.                     dc.LineTo(130, 420);
  299.                     dc.MoveTo(90,420);
  300.                     dc.LineTo(90,325);
  301.                     //left arms joints
  302. //-------------------------------------------------------------------------------------------------------              
  303.                     dc.MoveTo(550,310);
  304.                     dc.LineTo(380,170);
  305.                     dc.MoveTo(510,350);
  306.                     dc.LineTo(555,305);
  307.                     dc.MoveTo(510,350);
  308.                     dc.LineTo(380,240);
  309.                     //right arms
  310.                     dc.MoveTo(510,410);
  311.                     dc.LineTo(510,350);
  312.                     dc.MoveTo(550,405);
  313.                     dc.LineTo(510,410);
  314.                     dc.MoveTo(550,405);
  315.                     dc.LineTo(550,310);
  316.                     //right arms joints
  317. //-------------------------------------------------------------------------------------------------------
  318.                     dc.Rectangle(250,400,305,600); //left leg
  319.                     dc.Rectangle(180,600,250,560);//additional left leg                
  320. //--------------------------------------------------------------------------------------------------------
  321.                     dc.Rectangle(320,400,380,600); //right leg
  322.                     dc.Rectangle(380,560,440,600);//additional right leg
  323.                     break;
  324. //--------------------------------------------------------------------------------------------------------
  325.                
  326.                     case 3:
  327.                     dc.Ellipse(260,150,380,30); //Head
  328.                     dc.Rectangle(250,150,380,400);//square body
  329. //-------------------------------------------------------------------------------------------------------
  330.                     dc.Rectangle(70,170,250,220);//left arm
  331.                     dc.Rectangle(70,280,130,220); //additional left arm
  332.                     dc.Rectangle(380,170,570,220);//right arm
  333.                     dc.Rectangle(510,110,570,170);//additional right arm
  334. //-------------------------------------------------------------------------------------------------------
  335.                     dc.Rectangle(100,400,290,450); //left leg
  336.                     dc.Rectangle(100,450,150,510);//additional left leg
  337. //-------------------------------------------------------------------------------------------------------
  338.                     dc.Rectangle(320,400,380,600); //right leg
  339.                     dc.Rectangle(380,560,440,600);//additional right leg
  340. //-------------------------------------------------------------------------------------------------------
  341.                     break;
  342.  
  343.                     case 4:
  344.                     dc.Ellipse(260,150,380,30); //Head
  345.                     dc.Rectangle(250,150,380,400);//square body
  346. //-------------------------------------------------------------------------------------------------------
  347.                     dc.Rectangle(70,170,250,220);//left arm
  348.                     dc.Rectangle(70,170,130,100); //additional left arm
  349.                     dc.Rectangle(380,170,570,220);//right arm
  350.                     dc.Rectangle(510,280,570,220);//additional right arm       
  351.                     dc.Rectangle(250,400,305,600); //left leg
  352.                     dc.Rectangle(180,600,250,560);//additional left leg
  353.                     dc.Rectangle(330,400,540,450); //right leg
  354.                     dc.Rectangle(490,450,540,500);//additional right leg
  355.                     break;
  356.                    
  357.                     case 5:
  358.                     dc.Ellipse(260,150,380,30); //Head
  359.                     dc.Rectangle(250,150,380,400);//square body
  360. //-------------------------------------------------------------------------------------------------------
  361.                     dc.Rectangle(70,170,250,220);//left arm
  362.                     dc.Rectangle(70,170,130,110); //additional left arm
  363.                     dc.Rectangle(380,170,570,220);//right arm
  364.                     dc.Rectangle(510,110,570,170);//additional right arm
  365. //-------------------------------------------------------------------------------------------------------                  
  366.                     dc.Rectangle(100,400,290,450); //left leg
  367.                     dc.Rectangle(100,450,150,510);//additional left leg
  368. //-------------------------------------------------------------------------------------------------------              
  369.                     dc.Rectangle(330,400,540,450); //right leg
  370.                     dc.Rectangle(490,450,540,500);//additional right leg
  371. //-------------------------------------------------------------------------------------------------------
  372.                     break;
  373.  
  374.                     case 6:
  375.                     dc.Ellipse(260,150,380,30); //Head
  376.                     dc.Rectangle(250,150,380,400);//square body
  377. //-------------------------------------------------------------------------------------------------------
  378.                     dc.MoveTo(250,170);
  379.                     dc.LineTo(90,330);
  380.                     dc.MoveTo(90,330);
  381.                     dc.LineTo(130,365);
  382.                     dc.MoveTo(250,245);
  383.                     dc.LineTo(125,368);
  384.                     //left arms
  385.                     dc.MoveTo(130,420);
  386.                     dc.LineTo(130,360);
  387.                     dc.MoveTo(90,420);
  388.                     dc.LineTo(130, 420);
  389.                     dc.MoveTo(90,420);
  390.                     dc.LineTo(90,325);
  391.                     //left arms joints
  392.                     dc.MoveTo(550,310);
  393.                     dc.LineTo(380,170);
  394.                     dc.MoveTo(510,350);
  395.                     dc.LineTo(555,305);
  396.                     dc.MoveTo(510,350);
  397.                     dc.LineTo(380,240);
  398.                     //right arms
  399.                     dc.MoveTo(510,410);
  400.                     dc.LineTo(510,350);
  401.                     dc.MoveTo(550,405);
  402.                     dc.LineTo(510,410);
  403.                     dc.MoveTo(550,405);
  404.                     dc.LineTo(550,310);
  405.                     //right arms joints
  406. //-------------------------------------------------------------------------------------------------------
  407.                     dc.Rectangle(100,400,290,450); //left leg
  408.                     dc.Rectangle(100,450,150,510);//additional left leg
  409. //-------------------------------------------------------------------------------------------------------  
  410.                     dc.Rectangle(330,400,540,450); //right leg
  411.                     dc.Rectangle(490,450,540,500);//additional right leg
  412. //-------------------------------------------------------------------------------------------------------
  413.                     break;
  414.  
  415.                     case 7:
  416.                     dc.Ellipse(260,150,380,30); //Head
  417.                     dc.Rectangle(250,150,380,400);//square body
  418. //-------------------------------------------------------------------------------------------------------
  419.                     dc.MoveTo(250,190);
  420.                     dc.LineTo(120,50);
  421.                     dc.MoveTo(88,80);
  422.                     dc.LineTo(120,50);
  423.                     dc.MoveTo(88,77);
  424.                     dc.LineTo(250,250);
  425.                     //left arms
  426.                     dc.MoveTo(63,27);
  427.                     dc.LineTo(90,80);
  428.                     dc.MoveTo(110,7);
  429.                     dc.LineTo(65,30);
  430.                     dc.MoveTo(110,10);
  431.                     dc.LineTo(130,60);
  432.                     //left arm joint
  433. //--------------------------------------------------------------------------------------------------------
  434.                     dc.MoveTo(500,48);
  435.                     dc.LineTo(380,195);
  436.                     dc.MoveTo(500,50);
  437.                     dc.LineTo(550,70);
  438.                     dc.MoveTo(535,60);
  439.                     dc.LineTo(380,255);
  440.                     //right arms
  441.                     dc.MoveTo(550,15);
  442.                     dc.LineTo(520,90);
  443.                     dc.MoveTo(500,10);
  444.                     dc.LineTo(480,70);             
  445.                     dc.MoveTo(500,10);
  446.                     dc.LineTo(550,20);
  447.                     //right arm joint
  448. //-------------------------------------------------------------------------------------------------------
  449.                     dc.Rectangle(100,400,290,450); //left leg
  450.                     dc.Rectangle(100,450,150,510);//additional left leg
  451. //-------------------------------------------------------------------------------------------------------
  452.                     dc.Rectangle(330,400,540,450); //right leg
  453.                     dc.Rectangle(490,450,540,500);//additional right leg
  454. //-------------------------------------------------------------------------------------------------------
  455.                     break;
  456.                    
  457.                     case 8:
  458.                     dc.Ellipse(260,150,380,30); //Head
  459.                     dc.Rectangle(250,150,380,400);//square body
  460. //-------------------------------------------------------------------------------------------------------
  461.                     dc.MoveTo(250,190);
  462.                     dc.LineTo(120,50);
  463.                     dc.MoveTo(88,80);
  464.                     dc.LineTo(120,50);
  465.                     dc.MoveTo(88,77);
  466.                     dc.LineTo(250,250);
  467.                     //left arms
  468.                     dc.MoveTo(63,27);
  469.                     dc.LineTo(90,80);
  470.                     dc.MoveTo(110,7);
  471.                     dc.LineTo(65,30);
  472.                     dc.MoveTo(110,10);
  473.                     dc.LineTo(130,60);
  474.                     //left arm joint
  475. //-------------------------------------------------------------------------------------------------------- 
  476.                     dc.MoveTo(550,310);
  477.                     dc.LineTo(380,170);
  478.                     dc.MoveTo(510,350);
  479.                     dc.LineTo(555,305);
  480.                     dc.MoveTo(510,350);
  481.                     dc.LineTo(380,240);
  482.                     //right arms
  483.                     dc.MoveTo(510,410);
  484.                     dc.LineTo(510,350);
  485.                     dc.MoveTo(550,405);
  486.                     dc.LineTo(510,410);
  487.                     dc.MoveTo(550,405);
  488.                     dc.LineTo(550,310);
  489.                     //right arms joints
  490. //-------------------------------------------------------------------------------------------------------
  491.                     dc.Rectangle(100,400,290,450); //left leg
  492.                     dc.Rectangle(100,450,150,510);//additional left leg
  493. //-------------------------------------------------------------------------------------------------------
  494.                     dc.Rectangle(320,400,380,600); //right leg
  495.                     dc.Rectangle(380,560,440,600);//additional right leg
  496. //-------------------------------------------------------------------------------------------------------
  497.                     break;
  498.  
  499.                     case 9:
  500.                     dc.Ellipse(260,150,380,30); //Head
  501.                     dc.Rectangle(250,150,380,400);//square body
  502. //-------------------------------------------------------------------------------------------------------
  503.                     dc.MoveTo(250,170);
  504.                     dc.LineTo(90,330);
  505.                     dc.MoveTo(90,330);
  506.                     dc.LineTo(130,365);                
  507.                     dc.MoveTo(250,245);
  508.                     dc.LineTo(125,368);
  509.                     //left arms
  510.                     dc.MoveTo(130,420);
  511.                     dc.LineTo(130,360);
  512.                     dc.MoveTo(90,420);
  513.                     dc.LineTo(130, 420);
  514.                     dc.MoveTo(90,420);
  515.                     dc.LineTo(90,325);
  516.                     //left arms joints
  517.                     dc.MoveTo(500,48);
  518.                     dc.LineTo(380,195);
  519.                     dc.MoveTo(500,50);
  520.                     dc.LineTo(550,70);
  521.                     dc.MoveTo(535,60);
  522.                     dc.LineTo(380,255);
  523.                     //right arms
  524.                     dc.MoveTo(550,15);
  525.                     dc.LineTo(520,90);
  526.                     dc.MoveTo(500,10);
  527.                     dc.LineTo(480,70);
  528.                     dc.MoveTo(500,10);
  529.                     dc.LineTo(550,20);
  530.                     //right arm joint
  531.                     dc.Rectangle(250,400,305,600); //left leg
  532.                     dc.Rectangle(180,600,250,560);//additional left leg
  533.                     dc.Rectangle(330,400,540,450); //right leg
  534.                     dc.Rectangle(490,450,540,500);//additional right leg
  535.  
  536.                     break;
  537.                    
  538.                     }
  539.    
  540.         CDialogEx::OnPaint();
  541.     }
  542. }
  543.  
  544. // The system calls this function to obtain the cursor to display while the user drags
  545. //  the minimized window.
  546.             HCURSOR CVideoThreadDlg::OnQueryDragIcon()
  547.             {
  548.                 return static_cast<HCURSOR>(m_hIcon);
  549.             }
  550.  
  551.             UINT CVideoThreadDlg::StartThread (LPVOID param)
  552.             {
  553.  
  554. /*THREADSTRUCT*    ts = (THREADSTRUCT*)param;
  555.  
  556. //here is the time-consuming process
  557. //which interacts with your dialog
  558. AfxMessageBox (_T("Thread is started!"));
  559. // TODO: Add your control notification handler code here
  560. cv::VideoCapture cap(0);
  561. //VideoCapture cap("C:/Users/lsf-admin/Pictures/Camera Roll/video000.mp4");
  562. cv::vector<cv::Rect> faces;
  563. cv::Mat frame;
  564. cv::Mat graySacleFrame;
  565. cv::Mat original;
  566. if (!cap.isOpened())
  567. {
  568.  
  569.     AfxMessageBox(_T("Failed to load video file"));
  570.     //return;
  571.     //return exit(1);
  572. }
  573.  
  574. while (true)
  575. {
  576.     cap >> frame;
  577.     if (!frame.empty()){
  578.  
  579.     //clone from original frame
  580.     original = frame.clone();
  581.     }
  582.     CDC* vDC;
  583.     vDC =ts->_this->GetDlgItem(IDC_VIDEO)->GetDC();
  584.     CRect rect;
  585.     ts->_this->GetDlgItem(IDC_VIDEO)->GetClientRect(&rect);
  586.     IplImage* image2=cvCloneImage(&(IplImage)original);
  587.     ts->_this->DisplayIplImageToPictureBox(image2, vDC, rect); //img is IplImage* variable.
  588.     ts->_this->ReleaseDC(vDC);
  589.     if (cv::waitKey(30) >= 0) break;
  590. }
  591. */
  592.  
  593. /*    VideoCapture cap(0); // open the video camera no. 0
  594.  
  595. cv::vector<cv::Rect> faces;
  596. cv::Mat frame;
  597. cv::Mat graySacleFrame;
  598. cv::Mat original;
  599.  
  600.     if (!cap.isOpened())  // if not success, exit program
  601.     {
  602.         cout << "Cannot open the video cam" << endl;
  603.         return -1;
  604.     }
  605.  
  606.    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
  607.    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
  608.  
  609.     cout << "Frame size : " << dWidth << " x " << dHeight << endl;
  610.  
  611.     namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
  612.     int counter=0;
  613.     char str[80];
  614.  
  615.     while (1)
  616.     {
  617.         Mat frame;
  618.  
  619.         bool bSuccess = cap.read(frame); // read a new frame from video
  620.  
  621.    sprintf(str, "img%d.png",counter++);
  622.     String name(str);//Format("img%04d.png", counter++);; // NEW !
  623.          if (!bSuccess) //if not success, break loop
  624.         {
  625.              cout << "Cannot read a frame from video stream" << endl;
  626.              break;
  627.         }
  628.  
  629.  //         cap >> frame;
  630.           if( frame.empty() ) break; // end of video stream
  631.  
  632.               CDC* vDC;
  633. THREADSTRUCT*    ts = (THREADSTRUCT*)param;
  634. vDC =ts->_this->GetDlgItem(IDC_VIDEO)->GetDC();
  635.     CRect rect;
  636.     ts->_this->GetDlgItem(IDC_VIDEO)->GetClientRect(&rect);
  637.     IplImage* image2=cvCloneImage(&(IplImage)original);
  638.  
  639. //    ts->_this->DisplayIplImageToPictureBox(image2, vDC, rect); //img is IplImage* variable.
  640.     ts->_this->ReleaseDC(vDC);
  641.  
  642.           imshow("MyVideo", frame); //show the frame in "MyVideo" window
  643.  
  644.          
  645. //          imwrite(name, frame);
  646.         if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
  647.        {
  648.             cout << "esc key is pressed by user" << endl;
  649.             break;
  650.        }
  651.         _sleep(10);
  652.     }
  653. */
  654.  
  655.             // TODO: 在此添加控件通知处理程序代码
  656.             int counter=0;
  657.             char str[80];
  658.  
  659.             sprintf(str, "img%d.png",counter++);
  660.             String name(str);//Format("img%04d.png", counter++);; // NEW !
  661.  
  662.             CRect rect;
  663.             Mat srcImage;
  664.             THREADSTRUCT*    ts = (THREADSTRUCT*)param;
  665.  
  666.             HWND wnd = ts->hWnd;
  667.  
  668.             VideoCapture Matcapture(0);
  669.            double dWidth = Matcapture.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
  670.            double dHeight = Matcapture.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
  671.            rect.left = 0;
  672.            rect.right=0;
  673.            rect.right=dWidth;
  674.            rect.bottom=dHeight;
  675.             ts->_this->GetDlgItem(IDC_VIDEO)->GetClientRect(rect);//获取Picture control控件区域大小
  676.         //  ts->_this->GetDlgCtrlID()->SetClientRect(rect);
  677.         //  m_Video.
  678.         //  ts->_this->GetDlgItem(IDC_VIDEO)->SetClientRect(rect);
  679.         //  pctBox->Size = System::Drawing::Size(200, 150);
  680.             namedWindow("aaa",CV_WINDOW_NORMAL);//创建窗口 正常显示
  681.             resizeWindow("aaa",rect.Width(),rect.Height());//调整窗口大小为Picture control控件区域大小
  682.  
  683.             HWND hWnd = (HWND)cvGetWindowHandle("aaa");//获得创建的窗口句柄
  684.             HWND hParent = ::GetParent(hWnd);//获得其父句柄
  685.             ::SetParent(hWnd, ts->_this->GetDlgItem(IDC_VIDEO)->m_hWnd);//设置创建窗口的父窗口为Picture control控件
  686.             ::ShowWindow(hParent, SW_HIDE);//将父窗口及其子窗口进行隐藏
  687.  
  688.             HANDLE hTerminateEvent = ts->hEvent;
  689.  
  690.             bool terminate = false;
  691.  
  692.             while (!terminate)
  693.             {
  694.                 if(ts->_this->bExitThread){
  695.                     ts->_this->bExitThread = false;
  696.                 }
  697.                 DWORD retVal = WaitForSingleObject(hTerminateEvent, 0);
  698.  
  699.                 switch(retVal)
  700.                 {
  701.                     case WAIT_OBJECT_0:
  702.         //                cout << "Terminate Event signalled" << endl;
  703.                         terminate = true;
  704.                         break;
  705.                     case WAIT_TIMEOUT:
  706.         //                cout << "Keep running..." << endl;
  707.                         break;
  708.                     case WAIT_FAILED:
  709.         //                cerr << "WaitForSingleObject() failed" << endl;
  710.                         terminate = true;
  711.                         break;
  712.        
  713.                 }      
  714.        
  715.                 Matcapture>>srcImage;
  716.                 imshow("aaa",srcImage);
  717.  
  718.                 if(ts->_this->bCapture){
  719.                     ts->_this->bCapture = false;
  720.         //          ts->_this->showSavedImage(CString(str));
  721.                     ts->_this->DrawPicToHDC(srcImage);
  722.                     ts->_this->capturedImage=srcImage;
  723.                 }
  724.                 if (waitKey(10)>=0)
  725.                 {
  726.                     break;
  727.                 }
  728.             }
  729.  
  730.  
  731.         //you can also call AfxEndThread() here
  732.             return 1;
  733.         }
  734.  
  735.  
  736.  
  737.             void CVideoThreadDlg::OnBnClickedOk()
  738.             {
  739.                 // TODO: Add your control notification handler code here
  740.                 hTerminateEvent = CreateEvent(0, FALSE, FALSE, 0);
  741.  
  742.                 THREADSTRUCT *_param = new THREADSTRUCT;
  743.                 _param->_this = this;
  744.                 _param->hWnd = m_hWnd;
  745.                 _param->hEvent = hTerminateEvent;
  746.  
  747.                 pThread = AfxBeginThread (StartThread, _param);
  748.  
  749.                 pThread->m_bAutoDelete = true;
  750.                 pThread->ResumeThread();
  751.  
  752.                 m_Capture.EnableWindow(true);
  753.             //  CDialogEx::OnOK();
  754.             }
  755.  
  756.         void CVideoThreadDlg::OnBnClickedBtnSave()
  757.         {
  758.             // TODO: Add your control notification handler code here
  759.             CString name;
  760.             m_ImgName.GetWindowTextW(name);
  761.             name = _T("image\\")+name+_T(".jpg");
  762.             std::string imgName = CStringA(name);
  763.             imwrite(imgName, capturedImage);
  764.             m_SaveImg.EnableWindow(false);
  765.             bSave = true;
  766.         }
  767.  
  768.         void CVideoThreadDlg::showSavedImage(CString imgName){
  769.         // Convert a TCHAR string to a LPCSTR
  770.             CT2CA pszConvertedAnsiString (imgName);
  771.             // construct a std::string using the LPCSTR input
  772.             std::string strStd (pszConvertedAnsiString);   
  773.             Mat img = imread(strStd);
  774.             imshow("bbb",img);
  775.         }
  776.  
  777.  
  778.  
  779.                 void CVideoThreadDlg::OnBnClickedCancel()
  780.                 {
  781.                         // TODO: Add your control notification handler code here
  782.                     if(!SetEvent(hTerminateEvent))
  783.                     {
  784.                         cerr << "SetEvent() failed" << endl;        
  785.                     }
  786.  
  787.                     bExitThread = true;
  788.                     DWORD retVal =      WaitForSingleObject(pThread->m_hThread, INFINITE);
  789.  
  790.                     switch(retVal)
  791.                     {
  792.                         case WAIT_OBJECT_0:
  793.                 //            cout << "Thread terminated" << endl;            
  794.                             break;
  795.                         case WAIT_FAILED:
  796.                  //           cerr << "WaitForSingleObject() failed" << endl;        
  797.                             break;
  798.                     }
  799.                     DWORD exit_code= NULL;
  800.  
  801.                     if (pThread != NULL)
  802.                     {
  803.                         GetExitCodeThread(pThread->m_hThread, &exit_code);
  804.                         if(exit_code == STILL_ACTIVE)
  805.                         {
  806.                             ::TerminateThread(pThread->m_hThread, 0);
  807.                             CloseHandle(pThread->m_hThread);
  808.                             CloseHandle(hTerminateEvent);
  809.                         }
  810.                 //      pThread->m_hThread = NULL;
  811.                 //      pThread = NULL;
  812.                     }
  813.                     CDialogEx::OnCancel();
  814.                 }
  815.  
  816.  
  817.  
  818.  
  819.                     void CVideoThreadDlg::AdjustAspectImageSize( const Size& imageSize,
  820.                                                 const Size& destSize,
  821.                                                       Size& newSize )
  822.                     {
  823.                        double destAspectRatio   =  float( destSize.width  )  /  float( destSize.height  );
  824.                        double imageAspectRatio  =  float( imageSize.width )  /  float( imageSize.height );
  825.  
  826.                        if ( imageAspectRatio > destAspectRatio )
  827.                        {
  828.                           // Margins on top/bottom
  829.                           newSize.width    =   destSize.width;
  830.                           newSize.height   =   int( imageSize.height  *  
  831.                                                         ( double( destSize.width )  /  double( imageSize.width ) ) );
  832.                        }
  833.                        else
  834.                        {
  835.                           // Margins on left/right
  836.                           newSize.height   =   destSize.height;
  837.                           newSize.width    =   int( imageSize.width  *
  838.                                                         ( double( destSize.height )  /  double( imageSize.height ) ) );
  839.                        }
  840.                     }
  841.  
  842.  
  843.                     void CVideoThreadDlg::DrawPicToHDC( Mat  cvImg,
  844.                                        bool bMaintainAspectRatio /* =true*/  )
  845.                     {
  846.                    // Get the HDC handle information from the ID passed
  847.                        CDC* pDC  =  GetDlgItem(IDC_IMAGE)->GetDC();
  848.                        HDC  hDC  =  pDC->GetSafeHdc();
  849.  
  850.                        CRect rect;
  851.                        GetDlgItem(IDC_IMAGE)->GetClientRect(rect);
  852.  
  853.                        Size winSize( rect.right, rect.bottom );
  854.  
  855.                        // Calculate the size of the image that
  856.                        // will fit in the control rectangle.
  857.                        Size origImageSize( cvImg.cols, cvImg.rows );
  858.                        Size imageSize;
  859.                        int  offsetX;
  860.                        int  offsetY;
  861.  
  862.                        if ( ! bMaintainAspectRatio )
  863.                        {
  864.                           // Image should be the same size as the control's rectangle
  865.                           imageSize = winSize;
  866.                        }
  867.                        else
  868.                        {
  869.                           Size newSize;
  870.  
  871.                           AdjustAspectImageSize( origImageSize,
  872.                                                   winSize,
  873.                                                   imageSize );
  874.                        }
  875.  
  876.                        offsetX   =   ( winSize.width  - imageSize.width  )  /  2;
  877.                        offsetY   =   ( winSize.height - imageSize.height )  /  2;
  878.  
  879.                        // Resize the source to the size of the destination image if necessary
  880.                        Mat cvImgTmp;
  881.  
  882.                        resize( cvImg,
  883.                                cvImgTmp,
  884.                                imageSize,
  885.                                0,
  886.                                0,
  887.                                INTER_AREA );
  888.  
  889.                        // To handle our Mat object of this width, the source rows must
  890.                        // be even multiples of a DWORD in length to be compatible with
  891.                        // SetDIBits().  Calculate what the correct byte width of the
  892.                        // row should be to be compatible with SetDIBits() below.
  893.                        int stride  =  ( ( ( ( imageSize.width * 24 )  +  31 )  &  ~31 )  >>  3 );
  894.  
  895.                        // Allocate a buffer for our DIB bits
  896.                        uchar* pcDibBits  =  (uchar*) malloc( imageSize.height * stride );
  897.  
  898.                        if ( pcDibBits != NULL )
  899.                        {
  900.                           // Copy the raw pixel data over to our dibBits buffer.
  901.                           // NOTE: Can setup cvImgTmp to add the padding to skip this.
  902.                           for ( int row = 0;  row < cvImgTmp.rows;  ++row )
  903.                           {
  904.                              // Get pointers to the beginning of the row on both buffers
  905.                              uchar* pcSrcPixel  =  cvImgTmp.ptr<uchar>(row);
  906.                              uchar* pcDstPixel  =  pcDibBits  +  ( row * stride );
  907.  
  908.                              // We can just use memcpy
  909.                              memcpy( pcDstPixel,
  910.                                      pcSrcPixel,
  911.                                      stride );
  912.                           }
  913.  
  914.                           // Initialize the BITMAPINFO structure
  915.                           BITMAPINFO bitInfo;
  916.  
  917.                           bitInfo.bmiHeader.biBitCount       =  24;
  918.                           bitInfo.bmiHeader.biWidth          =   cvImgTmp.cols;
  919.                           bitInfo.bmiHeader.biHeight         =  -cvImgTmp.rows;
  920.                           bitInfo.bmiHeader.biPlanes         =  1;
  921.                           bitInfo.bmiHeader.biSize           =  sizeof(BITMAPINFOHEADER);
  922.                           bitInfo.bmiHeader.biCompression    =  BI_RGB;
  923.                           bitInfo.bmiHeader.biClrImportant   =  0;
  924.                           bitInfo.bmiHeader.biClrUsed        =  0;
  925.                           bitInfo.bmiHeader.biSizeImage      =  0;      //winSize.height * winSize.width * * 3;
  926.                           bitInfo.bmiHeader.biXPelsPerMeter  =  0;
  927.                           bitInfo.bmiHeader.biYPelsPerMeter  =  0;
  928.  
  929.                           // Add header and OPENCV image's data to the HDC
  930.                           StretchDIBits( hDC,
  931.                                          offsetX,
  932.                                          offsetY,
  933.                                          cvImgTmp.cols,
  934.                                          cvImgTmp.rows,
  935.                                          0,
  936.                                          0,
  937.                                          cvImgTmp.cols,
  938.                                          cvImgTmp.rows,
  939.                                          pcDibBits,
  940.                                          & bitInfo,
  941.                                          DIB_RGB_COLORS,
  942.                                          SRCCOPY );
  943.  
  944.                           free(pcDibBits);
  945.                        }
  946.  
  947.                        ReleaseDC(pDC);
  948.                     }
  949.  
  950.  
  951.  
  952.                 void CVideoThreadDlg::OnBnClickedBtnCapture()
  953.                 {
  954.                     // TODO: Add your control notification handler code here
  955.                     bCapture = true;
  956.                     m_SaveImg.EnableWindow(true);
  957.                 }
  958.  
  959.                 LONG CVideoThreadDlg::OnThreadFinished (WPARAM wParam, LPARAM lParam)
  960.                 {
  961.                     CDialogEx::OnCancel();
  962.  
  963.                     return 0;
  964.                 }
  965.  
  966.  
  967.  
  968.  
  969.                 void CVideoThreadDlg::OnTimer(UINT_PTR nIDEvent)
  970.                 {
  971.                     // TODO: Add your message handler code here and/or call default
  972.                     char buffer[20];//store a string up to 20 characters long.
  973.                                                   //the size of 20.
  974.                                                   //passing the data       
  975.                     int buf_size = 20;//only positive vaule (char in some compilers)
  976.                     serial ->readSerialPort(buffer,20);//(char *buffer, unsigned int buf_size)  //(3)
  977.                     control = atoi(buffer);
  978.                     Invalidate();
  979.                     CDialogEx::OnTimer(nIDEvent);
  980.                 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement