#include "ImageGrabberI.h"
|
#include <functional>
|
#include <algorithm>
|
#include <dirent.h>
|
#include <basic/debug/Debug.h>
|
#include <cstring>
|
#include <opencv2/opencv.hpp>
|
#include <QtCore/QDateTime>
|
#include <basic/util/opencv/CvUtil.h>
|
#include <basic/util/app/AppPreference.hpp>
|
|
//获取目录下所有文件的名称
|
std::vector<std::string> forEachFile(const std::string &dir_name) {
|
std::vector<std::string> v;
|
auto dir = opendir(dir_name.data());
|
struct dirent *ent;
|
int len = 0;
|
if (dir) {
|
while ((ent = readdir(dir)) != NULL) {
|
std::string p = std::string(ent->d_name);
|
len = strlen(p.data());
|
if (len == 25) {
|
v.emplace_back(p);
|
}
|
}
|
closedir(dir);
|
}
|
return v;
|
}
|
|
//将视频中的指定帧存为图片
|
cv::Mat Video2Imag(std::string VideoName, int msec) {
|
cv::VideoCapture m;
|
cv::Mat img = cv::Mat();
|
|
m.open(VideoName);
|
m.set(CV_CAP_PROP_POS_MSEC, msec);
|
if (!m.isOpened()) {
|
ERR("视频读入错误");
|
return img;
|
}
|
|
m >> img;
|
|
return img;
|
}
|
|
ImageGrabberI::ImageGrabberI() : fdfsClient("./client.conf") {
|
|
}
|
|
RecordVideo::ByteSequence
|
ImageGrabberI::grabImage(const Ice::Int index, const std::string &time, const Ice::Current &) {
|
::RecordVideo::ByteSequence buffer;
|
QDateTime dt = QDateTime::fromString(time.data(), "yyyy-MM-dd hh:mm:ss");
|
QDateTime dtCurrent = QDateTime::currentDateTime();
|
qint64 diff = dtCurrent.toMSecsSinceEpoch() - dt.toMSecsSinceEpoch();
|
if (diff > appPref.getLongData("user.timeout") || diff < appPref.getLongData("user.interval.duration")) {
|
return buffer;
|
}
|
std::string path = appPref.getStringData("user.loop.absolute.path");
|
std::string absPath;
|
|
if (path.empty()) {
|
ERR("路径为空!");
|
return buffer;
|
}
|
|
char last = path.back();
|
if (last != '/') {
|
absPath = path + "/";
|
} else {
|
absPath = path;
|
}
|
|
std::string definitePath = absPath + QString::number(index).toStdString() + "/";
|
|
vec.clear();
|
vec = forEachFile(definitePath);
|
std::sort(vec.begin(), vec.end());
|
|
if (vec.empty()) {
|
ERR("没有找到录制视频");
|
return buffer;
|
}
|
|
for (std::string &s : vec) {
|
std::string temp;
|
temp = s.substr(0, 21);
|
|
QDateTime dtFile = QDateTime::fromString(temp.data(), "yyyyMMdd_hh:mm:ss:zzz");
|
qint64 sub = dt.toMSecsSinceEpoch() - dtFile.toMSecsSinceEpoch();
|
if (sub <= appPref.getLongData("user.interval.duration") && sub > 0) {
|
std::string fullPath = definitePath + s;
|
cv::Mat img = Video2Imag(fullPath, sub);
|
CvUtil::cvMat2Buffer(img, buffer);
|
return buffer;
|
}
|
}
|
|
ERR("没有该时段的录制视频");
|
return buffer;
|
}
|
|
std::string ImageGrabberI::grabImageUrl(const Ice::Int index, const std::string &time, const Ice::Current &) {
|
std::string strHttpImgUrl;
|
::RecordVideo::ByteSequence buffer = grabImage(index, time);
|
|
if (buffer.empty()) {
|
return strHttpImgUrl;
|
}
|
|
std::string strImgUrl;
|
fdfsClient.uploadFile(buffer, strImgUrl, "jpg");
|
strHttpImgUrl = appPref.getStringData("user.ip.port") + "/" + strImgUrl;
|
return strHttpImgUrl;
|
}
|