Unsolved Snap Picture Not Working
-
I'm trying to make a simple application that snaps a picture from the webcam, displays the picture, sends a request with the data for image recognition to a server, and displays the user's estimated age and gender. Right now, I manage to grab the picture, but it displays oddly (behind the camera stream, or just a blank space). Once I manage to resolve this, what would be the best way to get the binary data to send in a REST API request?
MainWindow.h:
namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_SnapButton_clicked(); private: Ui::MainWindow *ui; QCamera *camera; QCameraViewfinder *camView; QLabel testLabel; };
MainWindow.c
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //qDebug() << "QCameraInfo: " << checkCameraAvailability(); QList<QCameraInfo> cameras = QCameraInfo::availableCameras(); camera = new QCamera(cameras.first()); camView = new QCameraViewfinder(); camera->setViewfinder(camView); camView->show(); camera->start(); ui->horizontalLayout->addWidget(camView); ui->horizontalLayout->addWidget(&testLabel); } void MainWindow::on_SnapButton_clicked() { testLabel.setPixmap(camView->grab()); }
The result:
-
-
Hi,
I think you should take a look at the QCameraImageCapture class.
-
I'm aware of that class, I just wanted to prototype with the easy way before messing with anything more complicated. If you could give me insight as to why my initial approach doesn't work, I'd appreciate it. In the mean time, I've also tried to start doing this the "right way", but alas ran into trouble rather fast. I connected the image capture to my class for when I snap a photo, but it's not getting into the function (which prints a simple id through qDebug). Here's my code:
MainWindow.c:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //qDebug() << "QCameraInfo: " << checkCameraAvailability(); QList<QCameraInfo> cameras = QCameraInfo::availableCameras(); camera = new QCamera(cameras.first()); imageCapture = new QCameraImageCapture(camera); connect(imageCapture,&QCameraImageCapture::imageAvailable,this,&MainWindow::testFunc); camView = new QCameraViewfinder(); camera->setViewfinder(camView); camView->show(); camera->start(); ui->horizontalLayout->addWidget(camView); ui->horizontalLayout->addWidget(&testLabel); } MainWindow::~MainWindow() { delete ui; } void MainWindow::testFunc(int id, QVideoFrame buffer) { qDebug() << id; } void MainWindow::on_SnapButton_clicked() { imageCapture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer); imageCapture->setBufferFormat(QVideoFrame::Format_Jpeg); camera->setCaptureMode(QCamera::CaptureStillImage); camera->searchAndLock(); imageCapture->capture(); camera->unlock(); //testLabel.setPixmap(camView->grab()); }
-
I looked around and read that "imageCapture->capture();" will only capture to a file (and filled up my picture directory). I haven't found any way to trigger a capture to buffer. Can anyone help?
-
-
My code set that value already. You can see it in the last function definition 'on_SnapButton_clicked()'
-
Actually, inspired by your post, I looked at isCaptureDestinationSupported. Apparently a buffer is not supported... Does anyone know why this could be?
-
Did you try connecting the imageCaptured signal (forgive the old syntax) ?
connect(m_pImageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(processCapturedImage(int,QImage)));
Then the slot:
void xxxxx::processCapturedImage(int requestId, const QImage& img)You can extract from the QImage passed to the slot.
-
Since you want to send it to a REST service, why not exploit the fact that having that temporary file allows you to retry a failed upload without clogging the memory used by your application. You can delete the file once you're done with it.
-
I looked into using the QImage, but I would either have to convert it to a QVideoFrame (and I couldn't find how), or perform a tedious manual conversion. I'd rather find out why the buffer option isn't supported and fix it rather than hack my way around. I'm going to try on my Linux machine with a spare webacam I found and see if that works.
@SGaist
I don't see why a temporary image would fare better than a buffer. I would have to read the file and re-fill my buffer if it fails, resulting in more time lost. -
Apparently it works on Linux but not Windows. Anybody have some insight as to why?
-
Because the windows platform camera plugin doesn't implement that option.
-
Is there a file I can download that implements it for Windows, or is this a problem of QT having to get around to it?
-
If there was, it would be part of the plugin. See the DSCameraSession class.