派生自 development/c++

pansen
2019-03-07 d3b7bbe7102cd089680a828f5d8f6402c8cf6342
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#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;
}