捕获缓冲QT

时间:2017-10-29 15:34:54

标签: c++ qt camera qbuffer

我尝试使用QT捕获图像,并将图像缓冲区写入QBuffer类的对象,而不是将其保存到文件中。

到目前为止,这是我的代码:

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();

是否可以将图像捕获到缓冲区而不将其保存到文件中?

1 个答案:

答案 0 :(得分:1)

也许是这样的?

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();