How to save image using timestamp in qt [closed] - qt

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am creating a GUI for windows PC. I want to store a series of images on the PC. The name of the images is identical. But I want to append them with timestamps. So, how to save image using timestamp in Qt?
in addition to that, the images shud be saved something like this example:
referenceImage<.date.><.time.>jpg
where date and time correspond to the date on and time at which the image was saved on the windows PC. I have tried the following too
Here i have implemented this at the click of a push button:-
void MainWindow::on_generateImagePushButton_clicked()
{
QDate date = QDate::currentDate();
QString dateString = date.toString();
QString path = QString("E:\\QT1\\timeStampTrial\\goldenRefImg[%1].jpg").arg(dateString);
qDebug()<<"path: "<<path;
/*display current time*/
QTime time = QTime::currentTime();
QString timeString = time.toString();
QString path2 = QString("E:\\QT1\\timeStampTrial\\goldenRefImg[%1 %2].jpg").arg(dateString).arg(timeString);
qDebug()<<"path2: "<<path2;
/*converting from QString to char* */
QByteArray bA = path2.toLocal8Bit();
const char *c_charArray = bA.data();
/*saving image*/
IplImage *imgWithTimeStamp = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
cvSaveImage(c_charArray, imgWithTimeStamp);
}
The image gets saved with dateStamp, ie eg. goldenRefImg[Wed Feb 5 2014].jpg when I use string-path. But when I use string-path2, it does NOT save with dateStamp & timeStamp as i expect it to, i.e. goldenRefImg[Wed Feb 5 2014 10:47:32].jpg
But the qDebug statements showing path and path2 are displayed correctly.
Application Output:
Starting E:\QT1\timepass-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2010__Qt_SDK__Debug\debug\timepass.exe...
path: "E:\QT1\timeStampTrial\goldenRefImg[Wed Feb 5 2014].jpg"
path2: "E:\QT1\timeStampTrial\goldenRefImg[Wed Feb 5 2014 10:47:23].jpg"
Now i have just recollected that an image cannot be saved with special characters such as the colon : which's there in timeStamp. Can the time format be changed?
I tried this way:
path2.replace(":","-");
But the E:\ also gets converted into E-.Please guide. Thank u.

const QDateTime now = QDateTime::currentDateTime();
const QString timestamp = now.toString(QLatin1String("yyyyMMdd-hhmmsszzz"));
const QString filename = QString::fromLatin1("/some/path/someimage-%1.jpg").arg(timestamp);
This takes the current date/time, converts it to a string using QDateTime::toString() (the documentation lists the formatting options) and constructs the file name out of it.
Then just use filename with QImage::save() or QImageWriter.

/*display current date*/
QDate date = QDate::currentDate();
QString dateString = date.toString();
QString path = QString("E:\\QT1\\timeStampTrial\\goldenRefImg[%1].jpg").arg(dateString);
qDebug()<<"path: "<<path;
/*display current time*/
QTime time = QTime::currentTime();
QString timeString = time.toString();
QString path2 = QString("E:\\QT1\\timeStampTrial\\goldenRefImg[%1 %2].jpg").arg(dateString).arg(timeString);
qDebug()<<"path2: "<<path2;
path2.replace(":","-");
path2.replace(1,1,":");
QByteArray bA = path2.toLocal8Bit();
const char *c_charArray = bA.data();
IplImage *imgWithTimeStamp = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
cvSaveImage(c_charArray, imgWithTimeStamp);
Thank you for all your suggestions #Dmitri Sazonov and #Frank Osterfeld

Related

How to convert the image format to ARG1555 in QT?

I'm new at qt, would like to write a new program with it that will be first. I want to change the image format and size after a image was loaded. However I can not find the ARGB1555 format in the supported formats. How can I convert its format to ARGB1555 ? I want this format, because will use it on hmi project that based on bare metal mcu, so will need the less memory.
It is possible to access the raw pixeldata of a QImage
const uchar * QImage::bits() const
Cast this to int ( 4bytes = pixel ) and use this function for conversion.
unsigned short ARGB8888toARGB1555(unsigned int c)
{
return (unsigned short)(((c>>16)&0x8000 | (c>>9)&0x7C00 | (c>>6)&0x03E0 | (c>>3)&0x1F));
}
Reference: https://cboard.cprogramming.com/c-programming/118698-color-conversion.html

How to Populate QDateEdit Stored in QString again into a QDateEdit

I have 2 QDateEdit which are Date_dob and Date_doj.
I am storing the value using a Qstring shown below.
QString str_dob(ui->DATE_dob->text());
QString str_doj(ui->DATE_doj->text());
Now i want to populate the same into ui->Date_dob and ui->Date_doj (after some editing event takes place). I have used ,
ui->DATE_dob->setText(s.at(2));
ui->DATE_doj->setText(s.at(5)); //where s is a string having data
but the data doesn't populate.
Any kind of suggestion will be extremely appreciated.
Thanks in advance
For convert QString to QDate you can use QDate::fromString(). Then you can set date in QDateEdit with QDate::setDate(const QDate &date).
Hope it help.
You use wrong way the conversion.
QDate to QString
QString str_dob = ui->DATE_dob->toString("dd MM yyyy");
in the date format you should specify it else your conversation is default format. Known Format you can use
QString to QDate
if( ui->DATE_dob->setDate(QDate::fromString(str_dob,"dd MM yyyy").year()\
,QDate::fromString(str_dob,"dd MM yyyy").month()\
,QDate::fromString(str_dob,"dd MM yyyy").day()){
// Your Conversation Succes
}
when QString to QDate you have to know date format in string else your conversation fail or wrong value you get.
Example: if Qstring is : 19/12/2017 than your format is "dd/MM/yyyy"

Converting byte[] to string [duplicate]

This question already has an answer here:
convert byte[] to string when upload a file in asp.net
(1 answer)
Closed 8 years ago.
I have a byte[] and want to convert it to string in asp.net.
Here is my code:
for (int loop1 = 0; loop1 < size; loop1++)
{
displayString = displayString + input[loop1].ToString();
}
But this code takes too long, is there another solution?
Just use the Encoding.GetString method. This will do all the work for you:
string s = Encoding.UTF8.GetString(input);
Make sure the encoding you use is the correct one for the string array. See the MSDN reference of the Encoding class to see all the encodings available.

Expiration date from text file in Qt

I have a text file with article numbers with their expiration date.
Example:
123456789 21-9-2014
987654321 7-12-2014
112233445 3-2-2015
But I want a list of all articles that are going to expire in 6 weeks (42 days) in a QTextEdit widget. So I tested this with just a QDate widget to check if something is almost expired or not.
QDate ExpireDate = ui->date->date();
if (ExpireDate.toJulianDay() - today.toJulianDay() <= 42)
{
qDebug() << "Expired";
}
This worked. But I want all the articles that are almost expiring (6 weeks before) and stored in text file to be displayed in a textEdit widget. But I don't know how to 'find' these articles in a text file and to display them.
For the sample file you provided, I would do the following:
QFile file1("test.txt");
if (!file1.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QDate today = QDate::currentDate();
while (!file1.atEnd()) {
QByteArray line = file1.readLine();
QList<QByteArray> tokens = line.split(' '); // Depends on the file format.
Q_ASSERT(tokens.size() == 2);
QString dateString = tokens.at(1).trimmed();
QDate date = QDate::fromString(dateString, "d-M-yyyy");
Q_ASSERT(date.isValid());
if (date.toJulianDay() - today.toJulianDay() <= 42) {
fprintf(stdout, "Expired\n");
}
}

String creation of file pathin char format with Qt (VC++)

I want to create a GUI application using Qt. For that I need to get the filename using:
QString fileName=getOpenFileName(.....)
I am using Windows and want to have the filename path in C:\a\b\c format and pass it to a function accepting char variables. How can I implement this?
According to the Qt FAQ
QString path = QFileDialog::getOpenFileName(...);
QByteArray byteArray = path.toLocal8Bit();
const char *charPath = byteArray.data();

Resources