c++qtcameraqbuffer

Capture To Buffer QT


I'm trying to capture an Image using QT, and write the image buffer into an object of the QBuffer class instead of saving it to a file.

This is my code so far:

mBuffer->open(QBuffer::ReadWrite);
mCameraImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
QImageEncoderSettings imageEncoderSettings;
imageEncoderSettings.setCodec("image/BMP");
mCameraImageCapture->setEncodingSettings(imageEncoderSettings);
mCamera->setCaptureMode(QCamera :: CaptureStillImage);
mCamera->start();
mCamera->searchAndLock();
mCamera->unlock();
mBuffer->close();

Is it possible to capture the image into a buffer without saving it to file?


Solution

  • Maybe something like this?

    QApplication app(argc, argv);
    QCamera *cam = new QCamera;
    
    cam->setCaptureMode(QCamera::CaptureStillImage);
    
    QCameraViewfinder *viewfinder = new QCameraViewfinder;
    
    viewfinder->show();
    
    QCameraImageCapture *cap = new QCameraImageCapture(cam);
    cap->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
    
    cam->setViewfinder(viewfinder);
    
    QObject::connect(cap, &QCameraImageCapture::imageCaptured, [=] (int id, QImage img) {
        QByteArray buf;
        QBuffer buffer(&buf);
        buffer.open(QIODevice::WriteOnly);
        img.save(&buffer, "PNG");
    });
    
    QObject::connect(cap, &QCameraImageCapture::readyForCaptureChanged, [=] (bool state) {
       if(state == true) {
           cam->searchAndLock();
           cap->capture();
           cam->unlock();
       }
    });
    
    cam->start();
    
    app.exec();