PDA

View Full Version : QCameraImageCapture is saving image only on program exit



carobnodrvo
1st March 2016, 10:34
Recently I've been testing my Qt 5.5 C++ (Win7) program on one older laptop and found a weird bug, namely I'm displaying camera image and on some action (when letter P is pressed on keyboard) save that image in Pictures folder. Everything works fine (on P click image is saved) until I start new thread (for doing some background process). After the new thread is started, on "take a picture" event nothing happens (image is not visible in Pictures folder; onImageError[/ is not called) and after I close the application all images that are being captured are saved.

I'm not sure if I made myself clear so here is one more time steps I perform:
Open app -> take a photo -> photo is saved -> start new thread -> take couple of pictures (they are not saved) -> close app -> app closed (ALL pictures are saved)

The important part I have to mention is that all object I use (QCamera, QCameraImageCapture and thread that I start) are pointers.
And I am not 100% sure that starting this thread cause the issue but every time I start the thread, images are not saved.

Here are source that I am using:

Capturing camera image:

void MyClass::onCameraSeleced(QCameraInfo camera_info) {
camera_ = new QCamera(camera_info);
pture(camera_);
camera_image_capture_->setCaptureDestination(QCameraImageCapture::Capture ToFile);

camera_image_capture_ = new QCameraImageCa
// connect to capture signals
connect(camera_image_capture_, SIGNAL(imageCaptured(int, const QImage&)),
this, SLOT(onImageCaptured(int, const QImage&)));
connect(camera_image_capture_, SIGNAL(imageSaved(int, const QString&)),
this, SLOT(onImageSaved(int, const QString&)));
connect(camera_image_capture_, SIGNAL(error(int,QCameraImageCapture::Error,QStrin g)), this,
SLOT(onImageError(int,QCameraImageCapture::Error,Q String)));

camera_->setCaptureMode(QCamera::CaptureStillImage);
camera_->setViewfinder(ui->cameraWidget);
camera_->start();
}

void MyClass::takeScreenShot(){
camera_->searchAndLock();

camera_image_capture_->capture(name);

camera_->unlock();
}

MyClass::~MyClass()
{
if(camera_ != NULL)
{
camera_->unload();
delete camera_;
}
}

Starting new thread that cause this issue:

my_thread_ = new MyThread();

QThreadPool::globalInstance()->start(my_thread_);

MyThread:

void MyThread::run()
{
while(run_)
{
.
.
.
QThread::msleep(static_speed_yaw_data_->getTimeInterval());
}
}

Lesiok
1st March 2016, 12:35
Method MyThread::run() is blocking thread event loop. Use standard QThread::run() and create a method fired by QTimer with static_speed_yaw_data_->getTimeInterval() period.

carobnodrvo
1st March 2016, 12:38
Method MyThread::run() is blocking thread event loop. Use standard QThread::run() and create a method fired by QTimer with static_speed_yaw_data_->getTimeInterval() period.

Thread which captures image is different from MyThread. Do I still need to do stuff you mentioned above? (it's not really clear to me why is image not captured and why should it be with our response)

anda_skoa
1st March 2016, 13:41
You probably shouldn't call "MyThread" like that since it is obviously a QRunnable and not a QThread.

Or maybe better make it an actual thread, the while loop suggests that you want to control it, not hand over to some thread pool.

Cheers,
_

carobnodrvo
7th March 2016, 17:40
You were right!

I have no idea how that part slipped me.

After changing QRunnable to QThread everything worked (tested it today).

Thanks!