2

我正在使用 Qt 中的 OpenCv 在 USB WebCamera 上工作。该代码适用于拍摄图像、保存图像等。但它随机生成错误“VIDIOC_QUERYCTRL:输入/输出错误”,导致我的整个 qt 应用程序关闭。

实际上我的应用程序的场景如下:

在 MainWindow.c 中,我有一个按钮,按下时会启动相机:

void MainWindow::on_pushButton_continue_pressed()
{
    camcapture = new CamCapture( imgResolution, defaultPath, this);
    connect(camcapture, SIGNAL(imgChange(QString)), this, SLOT(changeImg(QString)));
    camcapture->show();
}

这是我的 CamCapture 类的一部分:

CamCapture::CamCapture( int Resolution, QString path, QWidget *parent) :
    QFrame(parent),
    ui(new Ui::CamCapture), timer_(this), counter_(0)
{
    ui->setupUi(this);

    /*----some variable initialization---*/

    camInit();

    timer_.start(100);
    connect(&timer_,SIGNAL(timeout()), this,SLOT(captureLoop()));
}

void CamCapture::camInit()
{
    capture_ = cvCaptureFromCAM(0); 
    if (!capture_)
    {
        qDebug()<<"cannot get webcam...";
        ui->label_image->setText("No webcam!");
        ui->label_image->setAlignment(Qt::AlignCenter);
        buttonActiveStatus( false, false, false );
        return;
    }

    cvGrabFrame(capture_); // Grabs frame from camera or file

    image_ = cvRetrieveFrame(capture_); // Gets the image grabbed with cvGrabFrame

    qDebug() << "image size : " << image_->width << "x" << image_->height;

    qDebug() << "\n" <<  ui->label_image->x();
    qDebug() << "\n" <<  ui->label_image->y();
}


QImage CamCapture::Ipl2QImage(const IplImage *newImage)
{
    QImage qtemp;
    if (newImage && cvGetSize(newImage).width > 0)
    {
        int x;
        int y;
        char* data = newImage->imageData;

        qtemp= QImage(newImage->width, newImage->height,QImage::Format_RGB32 );
        for( y = 0; y < newImage->height; y++, data +=newImage->widthStep )
            for( x = 0; x < newImage->width; x++)
            {
                uint *p = (uint*)qtemp.scanLine (y) + x;
                *p = qRgb(data[x * newImage->nChannels+2],
                          data[x * newImage->nChannels+1],data[x * newImage-  >nChannels]);
            }
    }
    return qtemp;
}

void CamCapture::captureLoop()
{
    if ( !capture_ )
    {
        return;
    }

    cvGrabFrame(capture_);
    image_ = cvRetrieveFrame(capture_);
    if (image_)
    {
        counter_ ++;
        qImage_ = Ipl2QImage(image_);
        ui->label_image-    >setPixmap(QPixmap::fromImage(qImage_.scaled(width(),height()),Qt::AutoColor));
    }
}


void CamCapture::on_pushButton_cancel_pressed()
{

      cvReleaseCapture( &capture_ );
      cvDestroyAllWindows();
      delay(100);
      qDebug() << "nb frames :" << counter_;
      CamCapture::close();
}

void CamCapture::on_pushButton_capture_pressed()
{
    savePath = _imgPath;
    timer_.stop();

    frame = cvQueryFrame(capture_); // Gets the image grabbed with cvGrabFrame
}

void CamCapture::on_pushButton_retake_pressed()
{
    timer_.start(100);
    captureLoop();
}

void CamCapture::on_pushButton_save_pressed()
{
    int iwidth  = _imgWidth  * _imgResolution * scaledFactor;
    int iheight = _imgHeight * _imgResolution * scaledFactor;

    QString str = savePath;

    IplImage *small;

    small = cvCreateImage(cvSize(iwidth,iheight), 8, 3);
    cvResize(frame, small);
    cvSaveImage(savePath.toUtf8().constData(),small);

    delay(100);
    _imgNum++;
    emit imgChange(savePath);
    on_pushButton_cancel_pressed(); 
}

上面的代码随机生成错误(导致我的应用程序关闭)如下:

   Debugging starts
   &"warning: GDB: Failed to set controlling terminal: Invalid argument\n"
   Corrupt JPEG data: bad Huffman code
   image size :  640 x 480 

   5 

   10 
   nb frames : 11 
   VIDIOC_QUERYCTRL: Input/output error
   Debugging has finished

通常,在按下 MainWindow.c 中的 pushButton_continue 或按下 CamCapture.c 中的 pushButton_Save 时会生成错误

在谷歌上找不到任何解决方案。

需要一些建议/帮助/链接来解决这个问题。

提前致谢。

4

0 回答 0