[campreview] more messaging, respond to camera image sizes

This commit is contained in:
Jan Grewe 2025-03-12 09:57:29 +01:00
parent 222c3a9ce1
commit 6ec776cd4e

View File

@ -14,28 +14,28 @@ CameraPreview::CameraPreview(QWidget *parent):cameraname(""), camera(nullptr), Q
QWidget *controls = new QWidget(this); QWidget *controls = new QWidget(this);
width = new QSpinBox(controls); width = new QSpinBox(controls);
width->setSingleStep(4); width->setMinimum(32);
width->setMinimum(4);
width->setMaximum(2048); width->setMaximum(2048);
width->setSingleStep(32);
width->setValue(width->maximum()); width->setValue(width->maximum());
connect(width, SIGNAL(textChanged(QString)), SLOT(updateWidth(QString))); connect(width, SIGNAL(textChanged(QString)), SLOT(updateWidth(QString)));
height = new QSpinBox(controls); height = new QSpinBox(controls);
height->setSingleStep(4); height->setSingleStep(32);
height->setMinimum(4); height->setMinimum(32);
height->setMaximum(1536); height->setMaximum(1536);
height->setValue(height->maximum()); height->setValue(height->maximum());
connect(height, SIGNAL(textChanged(QString)), SLOT(updateHeight(QString))); connect(height, SIGNAL(textChanged(QString)), SLOT(updateHeight(QString)));
xoffs = new QSpinBox(controls); xoffs = new QSpinBox(controls);
xoffs->setSingleStep(4); xoffs->setSingleStep(8);
xoffs->setMinimum(0); xoffs->setMinimum(0);
xoffs->setMaximum(2047); xoffs->setMaximum(2047);
xoffs->setValue(0); xoffs->setValue(0);
connect(xoffs, SIGNAL(textChanged(QString)), SLOT(updateXoffs(QString))); connect(xoffs, SIGNAL(textChanged(QString)), SLOT(updateXoffs(QString)));
yoffs = new QSpinBox(controls); yoffs = new QSpinBox(controls);
yoffs->setSingleStep(4); yoffs->setSingleStep(8);
yoffs->setMinimum(0); yoffs->setMinimum(0);
yoffs->setMaximum(1535); yoffs->setMaximum(1535);
yoffs->setValue(0); yoffs->setValue(0);
@ -57,12 +57,11 @@ CameraPreview::CameraPreview(QWidget *parent):cameraname(""), camera(nullptr), Q
this->layout()->addWidget(controls); this->layout()->addWidget(controls);
//setPrimaryCamera(devicename); //setPrimaryCamera(devicename);
qDebug() << "Camera preview constructor"; qDebug() << "Camera preview constructor";
takeStill();
} }
void CameraPreview::setCamera(QString &device){ void CameraPreview::setCamera(QString &device){
qDebug() << "update camera! ";// << device.toStdString(); qDebug() << "update camera! " << device;
cameraname = device; cameraname = device;
if (camera != nullptr) { if (camera != nullptr) {
qDebug() << "camera is not nullptr! "; qDebug() << "camera is not nullptr! ";
@ -81,7 +80,26 @@ void CameraPreview::setCamera(QString &device){
msgBox.exec(); msgBox.exec();
return; return;
} }
int64_t max_height = camera->sensorHeight();
int64_t max_width = camera->sensorWidth();
std::cerr << "set spin box values " << max_width << " " << max_height << std::endl;
width->blockSignals(true);
height->blockSignals(true);
xoffs->blockSignals(true);
yoffs->blockSignals(true);
width->setMaximum(max_width);
width->setValue(max_width);
height->setMaximum(max_height);
height->setValue(max_height);
xoffs->setMaximum(max_width - 1);
xoffs->setValue(0);
yoffs->setMaximum(max_height - 1);
yoffs->setValue(0);
label->setText(device + " - " + camera->userName()); label->setText(device + " - " + camera->userName());
width->blockSignals(false);
height->blockSignals(false);
xoffs->blockSignals(false);
yoffs->blockSignals(false);
takeStill(); takeStill();
} }
@ -89,12 +107,14 @@ void CameraPreview::setCamera(QString &device){
void CameraPreview::takeStill() { void CameraPreview::takeStill() {
qDebug() << "Take Still image!"; qDebug() << "Take Still image!";
if (camera != nullptr && camera->isOpen()) { if (camera != nullptr && camera->isOpen()) {
MyImage mimg; ImageSettings s = camera->getImageSettings();
MyImage mimg(s.width, s.height);
bool valid = camera->grabFrame(mimg); bool valid = camera->grabFrame(mimg);
if (!valid) { if (!valid) {
qWarning() << "Grabbing from camera failed!"; qWarning() << "Grabbing from camera failed!";
return; return;
} }
qDebug() << "Grabbed image from camera succeeded!";
QImage img(static_cast<uchar *>(mimg.data()), mimg.width(), mimg.height(), QImage img(static_cast<uchar *>(mimg.data()), mimg.width(), mimg.height(),
QImage::Format::Format_Grayscale8); QImage::Format::Format_Grayscale8);
QPixmap mpm = QPixmap::fromImage(img); QPixmap mpm = QPixmap::fromImage(img);
@ -115,7 +135,7 @@ void CameraPreview::updateWidth(QString s) {
// if (xoffs->value() + width->value() > 2048) { // if (xoffs->value() + width->value() > 2048) {
// xoffs->setValue(2048 - width->value()); // xoffs->setValue(2048 - width->value());
// } // }
validate(width, xoffs, 2048); validate(width, xoffs, camera->sensorWidth());
updateROI(); updateROI();
} }
@ -123,7 +143,7 @@ void CameraPreview::updateXoffs(QString s) {
// if (xoffs->value() + width->value() > 2048) { // if (xoffs->value() + width->value() > 2048) {
// width->setValue(2048 - xoffs->value()); // width->setValue(2048 - xoffs->value());
// } // }
validate(xoffs, width, 2048); validate(xoffs, width, camera->sensorHeight());
updateROI(); updateROI();
} }
@ -131,7 +151,7 @@ void CameraPreview::updateHeight(QString s) {
// if (height->value() + yoffs->value() > 1536) { // if (height->value() + yoffs->value() > 1536) {
// yoffs->setValue(1536 - height->value()); // yoffs->setValue(1536 - height->value());
// } // }
validate(height, yoffs, 1536); validate(height, yoffs, camera->sensorHeight());
updateROI(); updateROI();
} }
@ -139,16 +159,18 @@ void CameraPreview::updateYoffs(QString s) {
// if (height->value() + yoffs->value() > 1536) { // if (height->value() + yoffs->value() > 1536) {
// height->setValue(1536 - yoffs->value()); // height->setValue(1536 - yoffs->value());
// } // }
validate(yoffs, height, 1536); validate(yoffs, height, camera->sensorWidth());
updateROI(); updateROI();
} }
void CameraPreview::validate(QSpinBox *origin, QSpinBox *dest, int limit){ void CameraPreview::validate(QSpinBox *origin, QSpinBox *dest, int limit){
qDebug() << "validate";
int val = ensureDivbyfour(origin->value()); int val = ensureDivbyfour(origin->value());
origin->setValue(val); origin->setValue(val);
if (origin->value() + dest->value() > limit) { if (origin->value() + dest->value() > limit) {
dest->setValue(limit - origin->value()); dest->setValue(limit - origin->value());
} }
qDebug() << "validate done";
} }
int CameraPreview::ensureDivbyfour(int val) { int CameraPreview::ensureDivbyfour(int val) {
@ -160,6 +182,7 @@ int CameraPreview::ensureDivbyfour(int val) {
} }
void CameraPreview::updateROI(bool emitSignal) { void CameraPreview::updateROI(bool emitSignal) {
qDebug() << "Update roi with signal: " << emitSignal;
QImage img = pm.toImage(); QImage img = pm.toImage();
double scaling = 800.0 / img.width(); double scaling = 800.0 / img.width();
img = img.scaledToWidth(800); img = img.scaledToWidth(800);
@ -176,20 +199,24 @@ void CameraPreview::updateROI(bool emitSignal) {
int rheight = round(height->value() * scaling); int rheight = round(height->value() * scaling);
qPainter.drawRect(rxoffs, ryoffs, rwidth, rheight); qPainter.drawRect(rxoffs, ryoffs, rwidth, rheight);
bool bEnd = qPainter.end(); bool bEnd = qPainter.end();
QPixmap npm = QPixmap::fromImage(img); QPixmap npm = QPixmap::fromImage(img);
setImage(npm); setImage(npm);
if (emitSignal) { if (emitSignal) {
emit roiUpdated(xoffs->value(), yoffs->value(), width->value(), height->value()); emit roiUpdated(xoffs->value(), yoffs->value(), width->value(), height->value());
} }
qDebug() << "update ROI done";
} }
void CameraPreview::setSize(int w, int h) { void CameraPreview::setSize(int w, int h) {
qDebug() << "set size " << w << " " << h;
width->setValue(w); width->setValue(w);
height->setValue(h); height->setValue(h);
validate(width, xoffs, 2048); if (camera != nullptr && camera->isOpen()) {
validate(height, yoffs, 1536); validate(width, xoffs, camera->sensorWidth());
validate(height, yoffs, camera->sensorHeight());
}
updateROI(false); updateROI(false);
qDebug() << "set size done";
} }
QString CameraPreview::device(){ QString CameraPreview::device(){
@ -198,12 +225,14 @@ QString CameraPreview::device(){
ROI CameraPreview::getRoi() { ROI CameraPreview::getRoi() {
ROI r; ROI r;
uint32_t max_height = camera->sensorHeight();
uint32_t max_width = camera->sensorWidth();
r.x = xoffs->value(); r.x = xoffs->value();
r.y = yoffs->value(); r.y = yoffs->value();
r.width = width->value(); r.width = width->value();
r.height = height->value(); r.height = height->value();
r.height = r.height > 1536 ? 1536 - r.y : r.height; r.height = r.height > max_height ? max_height - r.y : r.height;
r.width = r.width > 2048 ? 2048 - r.x : r.width; r.width = r.width > max_width ? max_width - r.x : r.width;
return r; return r;
} }