【问题标题】:Using qdatetime- cannot write image to path使用 qdatetime- 无法将图像写入路径
【发布时间】:2018-07-21 12:24:56
【问题描述】:

我的目标是保存图像(opencv 2.4.13)并在图像名称中包含当前日期时间(类似于:current-datetime.jpg)。我试过下面的代码-

string d_t = 
QDateTime::currentDateTime().toString(Qt::ISODate).toLocal8Bit().constData();
string output_file = 
"F:\\WorkSpace\\QtPractice\\resource\\face_db\\"+d_t+".jpg";
cv::imwrite(output_file, image);
cout << output_file << endl; //  to check the file location

但我在目标位置什么也没得到。

当我尝试下面的代码来检查它是否可以正常工作以保存图像时-

string d_t = to_string(3.1415926)
string output_file = "F:\\WorkSpace\\QtPractice\\resource\\face_db\\"+d_t+".jpg";
cv::imwrite(output_file, image);
cout << output_file << endl;

而且效果很好。所以,我认为我在转换字符串时做错了,因为第二个代码正常工作。有人可以帮我用当前日期时间写图像吗?我做错了什么?谢谢。

【问题讨论】:

  • QDateTime::currentDateTime().toString(Qt::ISODate).toStdString();
  • @Miki ,也试过了。没用。仍然没有得到要保存的图像。
  • IsoDate 的冒号“:”对路径无效。使用其他日期格式
  • @miki,现在我不明白我可以使用哪种格式(基本上我需要带冒号的 IsoDate 格式)。根据此文档,其他格式要么有冒号“:”,要么有空格。你能建议我如何纠正这个问题吗? doc.qt.io/qt-5/qtime.html#toString-1

标签: c++ qt opencv


【解决方案1】:

感谢@Miki 的指导。为了自己回答这个问题,我已将日期时间格式转换为自定义格式,从而解决了这个问题。以下是完整代码-

QDateTime dateTime = dateTime.currentDateTime();
string d_t = dateTime.toString("yyyy-MM-dd-hh-mm-ss").toStdString();
string output_file = "F:\\WorkSpace\\QtPractice\\resource\\face_db\\"+d_t+".jpg";
cv::imwrite(output_file, image);

【讨论】:

    猜你喜欢
    • 2017-02-26
    • 2020-03-29
    • 2019-02-25
    • 2020-05-07
    • 2016-07-24
    • 2023-01-09
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    相关资源
    最近更新 更多