Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Open hikcamera
Forum Updated to NodeBB v4.3 + New Features

Open hikcamera

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 417 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lanhha
    wrote on last edited by
    #1

    I want to open hikcamera (usbv3 camera) but showPicture() function is not launched. help me please!!904c4cb4-17de-479d-bd73-11e4ac6901d0-image.png be6916f3-458e-4b83-beec-2a127333ec9f-image.png

    jsulmJ 1 Reply Last reply
    0
    • L lanhha

      I want to open hikcamera (usbv3 camera) but showPicture() function is not launched. help me please!!904c4cb4-17de-479d-bd73-11e4ac6901d0-image.png be6916f3-458e-4b83-beec-2a127333ec9f-image.png

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @lanhha Please post code as text, not pictures!
      I don't know why all this code. If you want to capture an image from the camera why don't you simply use https://doc.qt.io/qt-5/qcameraimagecapture.html?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      0
      • jsulmJ jsulm

        @lanhha Please post code as text, not pictures!
        I don't know why all this code. If you want to capture an image from the camera why don't you simply use https://doc.qt.io/qt-5/qcameraimagecapture.html?

        L Offline
        L Offline
        lanhha
        wrote on last edited by
        #3

        @jsulm void
        MainWindow::showPicture()
        {
        std::cout<<"ok.."<<std::endl;
        QPixmap pix("C:/LanhHa/C++/HIKCAMERA/OIP.jfif");
        ui->picture->setPixmap(pix);
        cv::Mat mat = _camera_ptr->getImg() ;
        if ( mat.data ) {
        auto rect = ui->picture->getRect();
        static cv::Ptrcv::Tracker track ;
        static cv::Rect2d roi ;
        static bool bLastValid = false ;
        static size_t count = 0 ;
        cv::Mat rgb = mat.clone();
        bool bRGB = false ;
        double px = (mat.size().width) / (ui->picture->width());
        double py = (mat.size().height) / (ui->picture->height());
        static bool bRoiValid = false ;
        if ( !bLastValid && ui->picture->isValid() ) {
        if ( QRect{0, 0, 1, 1} != rect ) {
        roi = cv::Rect2d( rect.left() * px, rect.top() * py, px*(rect.right()-rect.left()), py*(rect.bottom()-rect.top()) ) ;

                    if (roi.width> 0 && roi.height > 0) {
                        track->init(mat, roi) ;
                        rgb = mat;
                        bRoiValid = true ;
                    }
                    else {
                        bRoiValid = false ;
                    }
                }
                bLastValid = true ;
            }
        

        // else if ( ui->picture->isValid() && bRoiValid){
        // cv::cvtColor(mat,rgb, cv::COLOR_GRAY2RGB) ;
        // cv::rectangle(rgb, roi, cv::Scalar(0,255,0), 2,1);
        // bRGB = true ;
        // }
        if ( !ui->picture->isValid() ) {
        bLastValid = false ;
        }
        auto fmt = QImage::Format_RGB888 ;
        if ( !bRGB ) {
        fmt = QImage::Format_Indexed8 ;
        }

            QImage image((uchar*)rgb.data, rgb.cols, rgb.rows, rgb.step, fmt) ;
            QPixmap pixmap = QPixmap::fromImage(image);
            ui->picture->setPixmap(pixmap);
            time_t tm = time( nullptr ) ;
            static time_t tmLast = tm ;
            ++count ;
            if ( tmLast != tm ) {
                tmLast = tm ;
                count = 0 ;
            }
        }
        

        }

        jsulmJ 1 Reply Last reply
        0
        • L lanhha

          @jsulm void
          MainWindow::showPicture()
          {
          std::cout<<"ok.."<<std::endl;
          QPixmap pix("C:/LanhHa/C++/HIKCAMERA/OIP.jfif");
          ui->picture->setPixmap(pix);
          cv::Mat mat = _camera_ptr->getImg() ;
          if ( mat.data ) {
          auto rect = ui->picture->getRect();
          static cv::Ptrcv::Tracker track ;
          static cv::Rect2d roi ;
          static bool bLastValid = false ;
          static size_t count = 0 ;
          cv::Mat rgb = mat.clone();
          bool bRGB = false ;
          double px = (mat.size().width) / (ui->picture->width());
          double py = (mat.size().height) / (ui->picture->height());
          static bool bRoiValid = false ;
          if ( !bLastValid && ui->picture->isValid() ) {
          if ( QRect{0, 0, 1, 1} != rect ) {
          roi = cv::Rect2d( rect.left() * px, rect.top() * py, px*(rect.right()-rect.left()), py*(rect.bottom()-rect.top()) ) ;

                      if (roi.width> 0 && roi.height > 0) {
                          track->init(mat, roi) ;
                          rgb = mat;
                          bRoiValid = true ;
                      }
                      else {
                          bRoiValid = false ;
                      }
                  }
                  bLastValid = true ;
              }
          

          // else if ( ui->picture->isValid() && bRoiValid){
          // cv::cvtColor(mat,rgb, cv::COLOR_GRAY2RGB) ;
          // cv::rectangle(rgb, roi, cv::Scalar(0,255,0), 2,1);
          // bRGB = true ;
          // }
          if ( !ui->picture->isValid() ) {
          bLastValid = false ;
          }
          auto fmt = QImage::Format_RGB888 ;
          if ( !bRGB ) {
          fmt = QImage::Format_Indexed8 ;
          }

              QImage image((uchar*)rgb.data, rgb.cols, rgb.rows, rgb.step, fmt) ;
              QPixmap pixmap = QPixmap::fromImage(image);
              ui->picture->setPixmap(pixmap);
              time_t tm = time( nullptr ) ;
              static time_t tmLast = tm ;
              ++count ;
              if ( tmLast != tm ) {
                  tmLast = tm ;
                  count = 0 ;
              }
          }
          

          }

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @lanhha Please format your code properly with code tags.
          Why do you use QTimer to call showPicture?
          Use "new" Qt5 connect syntax to detect errors in your connects during build time.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          L 2 Replies Last reply
          1
          • jsulmJ jsulm

            @lanhha Please format your code properly with code tags.
            Why do you use QTimer to call showPicture?
            Use "new" Qt5 connect syntax to detect errors in your connects during build time.

            L Offline
            L Offline
            lanhha
            wrote on last edited by
            #5

            void MainWindow::showPicture()

            {

            cv::Mat mat = _camera_ptr->getImg();
            
            if ( mat.data ) {
            
                auto rect = ui->picture->getRect();
            
                static cv::Ptr<cv::Tracker> track ;
            
                static cv::Rect2d roi ;
            
                static bool bLastValid = false ;
            
                static size_t count = 0 ;
            
                cv::Mat rgb = mat.clone();
            
                bool bRGB = false ;
            
                double px = (mat.size().width) / (ui->picture->width());
                double py = (mat.size().height) / (ui->picture->height());
                static bool bRoiValid = false ;
            
                if ( !bLastValid && ui->picture->isValid() ){
                    cout<<"E1" <<endl;
            
                    if ( QRect{0, 0, 1, 1} != rect ){
            
                        roi = cv::Rect2d( rect.left() * px, rect.top() * py, px*(rect.right()-rect.left()), py*(rect.bottom()-rect.top()));
            
                        if (roi.width> 0 && roi.height > 0){
                            track->init(mat, roi) ;
                            rgb = mat;
                            bRoiValid = true ;
                        }
                        else {
                            bRoiValid = false ;
                        }
                    }
                    bLastValid = true ;
                }
                else if ( ui->picture->isValid() && bRoiValid){
                    cv::cvtColor(mat,rgb, cv::COLOR_GRAY2RGB) ;
                    cv::rectangle(rgb, roi, cv::Scalar(0,255,0), 2,1);
                    bRGB = true ;
                }
                if ( !ui->picture->isValid() ) {
                    bLastValid = false ;
                }
                auto fmt = QImage::Format_RGB888 ;
                if ( !bRGB ) {
                    fmt = QImage::Format_Indexed8 ;
                }
            
                QImage image((uchar*)rgb.data, rgb.cols, rgb.rows, rgb.step, fmt);
                QPixmap pixmap = QPixmap::fromImage(image);
                ui->picture->setPixmap(pixmap);
                time_t tm = time( nullptr ) ;
                static time_t tmLast = tm ;
                ++count ;
                if ( tmLast != tm ) {
                    tmLast = tm ;
                    count = 0 ;
                }
            } 
            

            }

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @lanhha Please format your code properly with code tags.
              Why do you use QTimer to call showPicture?
              Use "new" Qt5 connect syntax to detect errors in your connects during build time.

              L Offline
              L Offline
              lanhha
              wrote on last edited by
              #6

              @jsulm @jsulm
              void MainWindow::on_openCamera_clicked()

              {

              try {
                  static QTimer qTimer ;
              
                  if ( bContinue ) {
                      qTimer.stop();
                      QObject::disconnect(&qTimer, SIGNAL(timeout()),this, SLOT(showPicture())) ;
                      ui->openCamera->setText("open");
                      ui->status_label->setText("Trạng thái máy ảnh: Tắt");
                  }
                  else {
                      QObject::connect(&qTimer, SIGNAL(timeout()),this, SLOT(showPicture())) ;
                      qTimer.start(100);
                      ui->openCamera->setText("close");
                      ui->status_label->setText("Trạng thái máy ảnh: Bật");
                  }
                  bContinue = !bContinue ;
              }
              
              catch( exception const &e ){
                  ui->status_label->setText("ERROR");
              }
              

              }

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved