Skip to content

Commit eafecfd

Browse files
committed
auto select image format for still capture
1 parent 3666295 commit eafecfd

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

src/libcameracamera.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,9 +634,37 @@ bool LibCameraWorker::configureCamera(libcamera::StreamRole role) {
634634
mCurrentWidth = cfg.size.width;
635635
mCurrentHeight = cfg.size.height;
636636
} else if (role == StreamRole::StillCapture) {
637-
// Don't force MJPEG - use the camera's default format for still capture
638-
// Pi cameras typically use YUV420 which we'll convert
639-
// Only set buffer count, keep default format and resolution
637+
std::vector<PixelFormat> pixelFormats = cfg.formats().pixelformats();
638+
// Search for the best available pixel format in order of preference
639+
PixelFormat selectedFormat = formats::RGB888; // fallback
640+
bool formatFound = false;
641+
642+
// Priority order: BGR888, RGB888, YUYV, MJPEG, YUV420
643+
std::vector<PixelFormat> preferredFormats = {
644+
formats::RGB888,
645+
formats::BGR888,
646+
formats::YUYV,
647+
formats::MJPEG,
648+
formats::YUV420
649+
};
650+
651+
for (const auto &preferred : preferredFormats) {
652+
for (const auto &available : pixelFormats) {
653+
if (available == preferred) {
654+
selectedFormat = preferred;
655+
formatFound = true;
656+
qDebug() << "[INFO] Selected pixel format:" << QString::fromStdString(selectedFormat.toString());
657+
break;
658+
}
659+
}
660+
if (formatFound) break;
661+
}
662+
663+
if (!formatFound) {
664+
qDebug() << "[WARNING] None of the preferred formats available, using default";
665+
}
666+
667+
cfg.pixelFormat = selectedFormat;
640668
cfg.bufferCount = 1;
641669
mCurrentWidth = cfg.size.width;
642670
mCurrentHeight = cfg.size.height;

0 commit comments

Comments
 (0)