Open hikcamera
-
I want to open hikcamera (usbv3 camera) but showPicture() function is not launched. help me please!!

@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? -
@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?@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 ; } }}
-
@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 ; } }}
-
@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.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 ; } }}
-
@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.@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"); }}