我正在尝试使用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();

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

最佳答案

也许是这样的吗?

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

08-25 09:01