How to increment Image name ?
-
@Cobra91151 it returns the image number, let's say we took 8 pictures, "id" will b equal to 8. it increments with the number of images taken, but its problem is whenever i want to reset it to 0 it doesn't do that it keeps its last value.
@hamzaelazizi I would suggest to not to use the id passed from the signal. Use your own id variable (as class member).
-
Your solution works well but counts from the second picture
2.png, since1 + (id % 11), otherwise the second count would start from0. So, I have improved it a bit by removing the1 +from modulo operation and adding the check for< 1.Your solution using class member variable as identifier would also work nice.
My code:
QCamera *camera = new QCamera(this); QCameraImageCapture *imageCapture = new QCameraImageCapture(camera); connect(imageCapture, &QCameraImageCapture::imageCaptured, [=](int id, QImage img) { qDebug() << "ID: " << id; int imgID = id % 11; if (imgID < 1) { imgID = 1; } QString path = QString("%1/%2.png").arg(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation), QString::number(imgID)); qDebug() << path; img.save(path, "PNG"); }); camera->setCaptureMode(QCamera::CaptureStillImage); camera->start(); ui->pushButton_4->setText("Capture"); connect(ui->pushButton_4, &QPushButton::clicked, [camera, imageCapture, this]() { camera->searchAndLock(); imageCapture->capture(); camera->unlock(); });Result:
ID: 1 "C:/Users/cobra/Pictures/1.png" ID: 2 "C:/Users/cobra/Pictures/2.png" ID: 3 "C:/Users/cobra/Pictures/3.png" ID: 4 "C:/Users/cobra/Pictures/4.png" ID: 5 "C:/Users/cobra/Pictures/5.png" ID: 6 "C:/Users/cobra/Pictures/6.png" ID: 7 "C:/Users/cobra/Pictures/7.png" ID: 8 "C:/Users/cobra/Pictures/8.png" ID: 9 "C:/Users/cobra/Pictures/9.png" ID: 10 "C:/Users/cobra/Pictures/10.png" ID: 11 "C:/Users/cobra/Pictures/1.png" ID: 12 "C:/Users/cobra/Pictures/1.png" ID: 13 "C:/Users/cobra/Pictures/2.png" ID: 14 "C:/Users/cobra/Pictures/3.png" ID: 15 "C:/Users/cobra/Pictures/4.png" ID: 16 "C:/Users/cobra/Pictures/5.png" ID: 17 "C:/Users/cobra/Pictures/6.png" ID: 18 "C:/Users/cobra/Pictures/7.png" ID: 19 "C:/Users/cobra/Pictures/8.png" ID: 20 "C:/Users/cobra/Pictures/9.png" ID: 21 "C:/Users/cobra/Pictures/10.png" ID: 22 "C:/Users/cobra/Pictures/1.png" ID: 23 "C:/Users/cobra/Pictures/1.png" ID: 24 "C:/Users/cobra/Pictures/2.png" ID: 25 "C:/Users/cobra/Pictures/3.png" ID: 27 "C:/Users/cobra/Pictures/5.png" ID: 29 "C:/Users/cobra/Pictures/7.png" ID: 31 "C:/Users/cobra/Pictures/9.png" ID: 32 "C:/Users/cobra/Pictures/10.png" ID: 33 "C:/Users/cobra/Pictures/1.png"Solution by class member variable:
.h file
class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = nullptr); ~Dialog(); private: Ui::Dialog *ui; int imgID; };.cpp file
Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog), imgID(0) { QCamera *camera = new QCamera; QCameraImageCapture *imageCapture = new QCameraImageCapture(camera); connect(imageCapture, &QCameraImageCapture::imageCaptured, [=](int id, QImage img) { qDebug() << "ID: " << id; imgID++; if (imgID >= 11) { imgID = 1; } QString path = QString("%1/%2.png").arg(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation), QString::number(imgID)); qDebug() << path; img.save(path, "PNG"); }); camera->setCaptureMode(QCamera::CaptureStillImage); camera->start(); ui->pushButton_4->setText("Capture"); connect(ui->pushButton_4, &QPushButton::clicked, [camera, imageCapture, this]() { //on half pressed shutter button camera->searchAndLock(); //on shutter button pressed imageCapture->capture(); //on shutter button released camera->unlock(); }); }Output:
ID: 1 "C:/Users/cobra/Pictures/1.png" ID: 2 "C:/Users/cobra/Pictures/2.png" ID: 3 "C:/Users/cobra/Pictures/3.png" ID: 4 "C:/Users/cobra/Pictures/4.png" ID: 5 "C:/Users/cobra/Pictures/5.png" ID: 6 "C:/Users/cobra/Pictures/6.png" ID: 7 "C:/Users/cobra/Pictures/7.png" ID: 8 "C:/Users/cobra/Pictures/8.png" ID: 9 "C:/Users/cobra/Pictures/9.png" ID: 10 "C:/Users/cobra/Pictures/10.png" ID: 11 "C:/Users/cobra/Pictures/1.png" ID: 12 "C:/Users/cobra/Pictures/2.png" ID: 13 "C:/Users/cobra/Pictures/3.png" ID: 14 "C:/Users/cobra/Pictures/4.png" ID: 15 "C:/Users/cobra/Pictures/5.png" ID: 16 "C:/Users/cobra/Pictures/6.png" ID: 18 "C:/Users/cobra/Pictures/7.png" ID: 20 "C:/Users/cobra/Pictures/8.png" ID: 21 "C:/Users/cobra/Pictures/9.png" ID: 23 "C:/Users/cobra/Pictures/10.png" ID: 25 "C:/Users/cobra/Pictures/1.png" ID: 26 "C:/Users/cobra/Pictures/2.png" ID: 28 "C:/Users/cobra/Pictures/3.png" ID: 30 "C:/Users/cobra/Pictures/4.png" ID: 31 "C:/Users/cobra/Pictures/5.png" ID: 33 "C:/Users/cobra/Pictures/6.png" ID: 35 "C:/Users/cobra/Pictures/7.png" ID: 36 "C:/Users/cobra/Pictures/8.png" ID: 37 "C:/Users/cobra/Pictures/9.png" ID: 39 "C:/Users/cobra/Pictures/10.png" ID: 41 "C:/Users/cobra/Pictures/1.png" ID: 42 "C:/Users/cobra/Pictures/2.png" ID: 43 "C:/Users/cobra/Pictures/3.png" ID: 44 "C:/Users/cobra/Pictures/4.png" ID: 45 "C:/Users/cobra/Pictures/5.png"@Cobra91151 it didn't work, it gave me this error at first "error: assignment of read-only variable ‘imgID’
imgID = id;" but when i took "imgID" inside the method, it kept only overwriting on image N°1. -
@Cobra91151 it didn't work, it gave me this error at first "error: assignment of read-only variable ‘imgID’
imgID = id;" but when i took "imgID" inside the method, it kept only overwriting on image N°1.@hamzaelazizi The method suggested by @Cobra91151 will not work as changing a parameter passed by value has NO effect on the original variable! Use your own id as I suggested above.
-
Hello Everyone, as i was continuing to work on an application, i came across this issue an i still couldn't solve it for more than 4 days now. The issue is that i cannot increment the naming of the saved images to a certain number and then comeback to the number 0 and start overwriting those images until i reach than number again and so on repeatedly!
let me give u an example : i want to save 10 Images and i want them to be names "1.png", "2.png"......"10.png", but i want the 11th one to overwrite the "1.png", 12th to overwrite the "2.png" and so on.i've tried multiple approaches but none of them worked! :(
here's my code, thank you
QObject::connect(_image_capture.data(), &QCameraImageCapture::imageCaptured, [=] (int id, QImage img) { QString fileName = QString::number(id)+ ".png"; QString path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/" + fileName; img.save(path, "PNG"); });@hamzaelazizi said in How to increment Image name ?:
i've tried multiple approaches but none of them worked! :(
I don't know how you are generating the ID, if it is a free running counter why not use the modulo function?
QString fileName = QString("%1.png").arg(1 + (id % 11)); -
@hamzaelazizi said in How to increment Image name ?:
i've tried multiple approaches but none of them worked! :(
I don't know how you are generating the ID, if it is a free running counter why not use the modulo function?
QString fileName = QString("%1.png").arg(1 + (id % 11));@KroMignon The id is comming from https://doc.qt.io/qt-5/qcameraimagecapture.html#imageCaptured
-
@KroMignon The id is comming from https://doc.qt.io/qt-5/qcameraimagecapture.html#imageCaptured
@jsulm said in How to increment Image name ?:
The id is comming from https://doc.qt.io/qt-5/qcameraimagecapture.html#imageCaptured
I see! :D
So this would also not work!
Then I don't see another solution as what @jsulm already suggested: use class member to store the counter value -
@hamzaelazizi said in How to increment Image name ?:
i've tried multiple approaches but none of them worked! :(
I don't know how you are generating the ID, if it is a free running counter why not use the modulo function?
QString fileName = QString("%1.png").arg(1 + (id % 11));@KroMignon I wanted to use it, in fact i was trying it over and over but since i'm new to Qt i didn't know how to get it to work :( (i feel ashamed now), when i tried your solution, it seems like it's working! i don't know if i will be running into some issues in the futur, but i guess that would be a problem for another day, Thank you
-
@jsulm said in How to increment Image name ?:
The id is comming from https://doc.qt.io/qt-5/qcameraimagecapture.html#imageCaptured
I see! :D
So this would also not work!
Then I don't see another solution as what @jsulm already suggested: use class member to store the counter value@KroMignon said in How to increment Image name ?:
So this would also not work!
Actually it should :-)
-
@KroMignon said in How to increment Image name ?:
So this would also not work!
Actually it should :-)
@jsulm said in How to increment Image name ?:
Actually it should :-)
Okay, I never used
QCameraImageCapture, so I supposed the ID would be the ID of the used camera and not if the image...
Maybe I should avoid the reply to posts for topics I not experimented in! -
@jsulm said in How to increment Image name ?:
Actually it should :-)
Okay, I never used
QCameraImageCapture, so I supposed the ID would be the ID of the used camera and not if the image...
Maybe I should avoid the reply to posts for topics I not experimented in!@KroMignon Sir, your solution helped me, so i think you should keep relying to this kind of posts :D, i have one last question, @jsulm how can i use my own id variable ? should i create a variable inside this class ? i don't understand !
-
@KroMignon Sir, your solution helped me, so i think you should keep relying to this kind of posts :D, i have one last question, @jsulm how can i use my own id variable ? should i create a variable inside this class ? i don't understand !
@hamzaelazizi said in How to increment Image name ?:
should i create a variable inside this class ?
Yes and you initialize it with 0. Each time your slot is called you increment it and check whether it is bigger than the max id value you want to use, if it is you set it back to 0.
-
@hamzaelazizi said in How to increment Image name ?:
should i create a variable inside this class ?
Yes and you initialize it with 0. Each time your slot is called you increment it and check whether it is bigger than the max id value you want to use, if it is you set it back to 0.
@jsulm something like this ?
QObject::connect(_image_capture.data(), &QCameraImageCapture::imageCaptured, [=] (int id=0, QImage img) { int idx = 0; if(idx>=11) { idx=0; } else { idx++; } QString fileName = QString::number(idx) + ".png"; QString path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/" + fileName; img.save(path, "PNG"); });Because i've tried this and it didn't work :/
-
@jsulm something like this ?
QObject::connect(_image_capture.data(), &QCameraImageCapture::imageCaptured, [=] (int id=0, QImage img) { int idx = 0; if(idx>=11) { idx=0; } else { idx++; } QString fileName = QString::number(idx) + ".png"; QString path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/" + fileName; img.save(path, "PNG"); });Because i've tried this and it didn't work :/
@hamzaelazizi said in How to increment Image name ?:
something like this ?
No.
In your code idx is a local variable and not a class member... -
@jsulm something like this ?
QObject::connect(_image_capture.data(), &QCameraImageCapture::imageCaptured, [=] (int id=0, QImage img) { int idx = 0; if(idx>=11) { idx=0; } else { idx++; } QString fileName = QString::number(idx) + ".png"; QString path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/" + fileName; img.save(path, "PNG"); });Because i've tried this and it didn't work :/
I have updated my previous post. Please, try it again.
-
@hamzaelazizi said in How to increment Image name ?:
something like this ?
No.
In your code idx is a local variable and not a class member...@jsulm i see, but whenever i declare a variable outside the method i get this error saying that the variable i created is a Read-only
-
@jsulm i see, but whenever i declare a variable outside the method i get this error saying that the variable i created is a Read-only
@hamzaelazizi Depends what you write and where.
-
@jsulm i see, but whenever i declare a variable outside the method i get this error saying that the variable i created is a Read-only
@hamzaelazizi said in How to increment Image name ?:
outside the method
I never suggested to declare it outside of the method. I suggested to declare it as a class member:
class YOUR_CLASS: { private: int idx{0}; -
@hamzaelazizi said in How to increment Image name ?:
outside the method
I never suggested to declare it outside of the method. I suggested to declare it as a class member:
class YOUR_CLASS: { private: int idx{0};@jsulm i understand now, thank you for your explanation