Skip to content
This repository was archived by the owner on Apr 27, 2020. It is now read-only.

Commit 8c34f17

Browse files
committed
增加例子,结合OpenCV进行图片拼接
1 parent d5d84d6 commit 8c34f17

File tree

11 files changed

+431
-34
lines changed

11 files changed

+431
-34
lines changed

CMakeLists.txt

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,53 @@ include_directories(
4848

4949
find_library(dl dl)
5050
find_library(pthread pthread)
51-
if (APPLE)
52-
find_package(libevent REQUIRED)
53-
find_package(openssl REQUIRED)
54-
else ()
55-
find_package(libevent)
56-
find_library(libevent libevent)
57-
find_package(openssl)
58-
find_library(openssl openssl)
51+
52+
53+
find_package(OpenCV)
54+
if (OpenCV_FOUND)
55+
add_definitions(-DENABLE_OPENCV)
56+
# If the package has been found, several variables will
57+
# be set, you can find the full list with descriptions
58+
# in the OpenCVConfig.cmake file.
59+
# Print some message showing some of them
60+
message(STATUS "OpenCV library status:")
61+
message(STATUS " version: ${OpenCV_VERSION}")
62+
message(STATUS " libraries: ${OpenCV_LIBS}")
63+
message(STATUS " libraries: ${OpenCV_LIBRARIES}")
64+
message(STATUS " lib_dir: ${OpenCV_LIB_DIR}")
65+
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
66+
link_directories(${OpenCV_DIR})
67+
include_directories(
68+
${OpenCV_INCLUDE_DIRS}
69+
)
70+
71+
endif ()
72+
73+
find_package(PkgConfig REQUIRED)
74+
pkg_search_module(OPENSSL REQUIRED openssl)
75+
if (OPENSSL_FOUND)
76+
add_definitions(-DENABLE_OPENSSL)
77+
78+
include_directories(${OPENSSL_INCLUDE_DIRS})
79+
message(STATUS "OPENSSL library status:")
80+
message(STATUS " ${OPENSSL_VERSION}")
81+
message(STATUS " libraries: ${OPENSSL_LIBRARIES}")
82+
message(STATUS " lib_dir: ${OPENSSL_LIBRARY_DIRS}")
83+
message(STATUS " include path: ${OPENSSL_INCLUDE_DIRS}")
84+
include_directories(${OPENSSL_INCLUDE_DIRS})
85+
link_directories(${OPENSSL_LIBRARY_DIRS})
86+
5987
endif ()
60-
include_directories(
61-
${LIBEVENT_INCLUDE_DIRS}
62-
${OPENSSL_INCLUDE_DIR}
63-
)
6488
if (APPLE)
6589
link_directories(/usr/local/lib/)
6690
endif ()
67-
if (LIBEVENT_LIBRARIES MATCHES "")
68-
# message("${LIBEVENT_STATIC_LIBRARIES} ${LIBEVENT_SHARED_LIBRARIES}")
69-
set(LIBEVENT_LIBRARIES event)
70-
endif ()
7191

7292

93+
# get_cmake_property(_variableNames VARIABLES)
94+
# foreach (_variableName ${_variableNames})
95+
# message(STATUS "${_variableName}=${${_variableName}}")
96+
# endforeach()
97+
7398
#aux_source_directory(kHttpd srcSource)
7499
FILE(GLOB_RECURSE srcSource
75100
"kProxy/*.c"
@@ -92,8 +117,7 @@ target_compile_definitions(
92117
target_link_libraries(kProxy
93118
${dl_LIBRARIES}
94119
${OPENSSL_LIBRARIES} ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY}
95-
${LIBEVENT_LIBRARIES} ${LIBEVENT_SHARED_LIBRARIES}
96-
${libevhtp_LIBRARIES}
120+
${OpenCV_LIBS}
97121
${libTools_LIBRARIES}
98122
${wsock32_LIBS}
99123
)

Example/MergePhoto.html

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<!DOCTYPE html>
2+
<html lang="zh">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>OpenCV合并图片</title>
6+
<script>
7+
let data = {};
8+
9+
function onChange(type) {
10+
let file = this.files[0];
11+
let reader = new FileReader();
12+
reader.onload = function (event) {
13+
// 图片的base64编码会在这里被打印出来
14+
data[type] = (event.target.result);
15+
let img = document.querySelector(`#${type}_img`);
16+
img.src = data[type];
17+
};
18+
reader.readAsDataURL(file)
19+
}
20+
21+
function MergePhoto() {
22+
let httpRequest = new XMLHttpRequest();//第一步:创建需要的对象
23+
httpRequest.open('POST', '/MergePhoto', true); //第二步:打开连接/***发送json格式文件必须设置请求头 ;如下 - */
24+
httpRequest.setRequestHeader("Content-type", "application/json");
25+
httpRequest.send(JSON.stringify(data));//发送请求 将json写入send中
26+
/**
27+
* 获取数据后的处理程序
28+
*/
29+
httpRequest.onreadystatechange = function () {//请求后的回调接口,可将请求成功后要执行的程序写在其中
30+
if (httpRequest.readyState === 4 && httpRequest.status === 200) {//验证请求是否发送成功
31+
let json = httpRequest.responseText;//获取到服务端返回的数据
32+
try {
33+
json = JSON.parse(json);
34+
}catch (e) {
35+
36+
}
37+
if(json.code===0) {
38+
document.querySelector("#MergePhoto").src = json.photo;
39+
}else{
40+
alert(json.message);
41+
}
42+
}
43+
};
44+
}
45+
</script>
46+
<style>
47+
img {
48+
max-height: calc(100% - 1.5em);
49+
max-width: 100%;
50+
}
51+
52+
img[src=""] {
53+
opacity: 0;
54+
}
55+
</style>
56+
</head>
57+
<body>
58+
<div style="position: fixed;left: 0;right: 0;top:0;bottom: 50%;">
59+
<div style="padding:0.5em;box-sizing: border-box;border:1px solid #CCCCCC;position: absolute;left: 0;right: 50%;top:0;bottom:0;">
60+
<label>左边图片:<input type="file" onchange="onChange.call(this,'left')"></label><br/>
61+
<img id="left_img" src="" alt="左边图片"/>
62+
</div>
63+
<div style="padding:0.5em;box-sizing: border-box;border:1px solid #CCCCCC;position: absolute;left: 50%;right: 0;top:0;bottom: 0;">
64+
<label>右边图片:<input type="file" onchange="onChange.call(this,'right')"></label><br/>
65+
<img id="right_img" src="" alt="右边图片"/>
66+
</div>
67+
</div>
68+
<div style="padding:0.5em;box-sizing: border-box;border:1px solid #CCCCCC;position: fixed;left: 0;right: 0;top: 50%;bottom: 0;">
69+
<button onclick="MergePhoto()">合并</button>
70+
<br/>
71+
<img id="MergePhoto" src="" alt="合并之后图片">
72+
73+
</div>
74+
</body>
75+
</html>

Example/left.png

140 KB
Loading

Example/right.png

122 KB
Loading

Example/timg.jpeg

13.5 KB
Loading

kProxy/kHttpd/kHttpd.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ kHttpd::check_host_path(class kWebSocketClient *_kWebSocketClient, int type, con
222222
return _kWebSocketClient->send(data, type) >= 0;
223223
}
224224

225+
void kHttpd::set_cb(
226+
const std::string &method,
227+
const std::string &url_path,
228+
url_cb task,
229+
const std::string &host) {
230+
set_cb(task, url_path, method, host);
231+
}
232+
225233
void
226234
kHttpd::set_cb(kHttpd::url_cb task, const std::string &url_path, const std::string &method, const std::string &host) {
227235
string key;
@@ -250,6 +258,7 @@ void kHttpd::init_php(const char *ip, unsigned short port) {
250258
PhpPort = port;
251259
}
252260
}
261+
253262
void kHttpd::RunPhpCGI(const string &filePath, kHttpdName::kCGI &kCgi,
254263
kHttpdClient *httpdClient,
255264
map<string, string> &header,

kProxy/kHttpd/kHttpd.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class _kProxy_HEADER_Export kHttpd {
4242
void init_php(const char *ip, unsigned short port);
4343

4444
static void RunPhpCGI(const std::string &filePath, kHttpdName::kCGI &kCgi,
45-
kHttpdClient *httpdClient,
46-
std::map<std::string, std::string> &header,
47-
std::vector<unsigned char> &data);
45+
kHttpdClient *httpdClient,
46+
std::map<std::string, std::string> &header,
47+
std::vector<unsigned char> &data);
4848

4949
/**
5050
* 是否是 WebSocket
@@ -67,6 +67,12 @@ class _kProxy_HEADER_Export kHttpd {
6767
const std::string &method = "",
6868
const std::string &host = "");
6969

70+
void set_cb(
71+
const std::string &method,
72+
const std::string &url_path,
73+
url_cb task,
74+
const std::string &host = "");
75+
7076
void set_gencb(gen_cb task);
7177

7278
private:

kProxy/kHttpd/kHttpdClient.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "kHttpd.h"
88
#include <CGI/kCGI.h>
99
#include <string>
10+
#include <utility>
1011
#include <vector>
1112
#include <cstring>
1213
#include <unistd.h>
@@ -55,14 +56,17 @@ kHttpdClient::kHttpdClient(kHttpd *parent, int fd) {
5556
}
5657

5758
kHttpdClient::kHttpdClient(kHttpd *parent, int fd, const std::map<std::string, std::string> &header,
59+
std::vector<unsigned char> data, unsigned long int split_index, bool is_split_n,
5860
std::string method,
5961
std::string url_path,
6062
std::string http_version) {
6163
init(parent, fd);
62-
this->url_path = url_path;
63-
this->method = method;
64-
this->http_version = http_version;
64+
this->url_path = std::move(url_path);
65+
this->method = std::move(method);
66+
this->http_version = std::move(http_version);
6567
this->header = header;
68+
/********* 读取body数据内容 *********/
69+
body_data.insert(body_data.end(), data.begin() + split_index, data.end());
6670
}
6771

6872
void kHttpdClient::init(kHttpd *_parent, int _fd) {
@@ -92,6 +96,7 @@ int kHttpdClient::run() {
9296
unsigned long int split_index = 0;
9397
bool is_split_n = true;
9498

99+
ssize_t ContentLength = 0;
95100
if (header.empty()) {
96101
/********* 读取数据内容 *********/
97102
do {
@@ -151,15 +156,21 @@ int kHttpdClient::run() {
151156
} while (split_index == 0);
152157
/********* 初始化http头 *********/
153158
init_header((const char *) data.data(), split_index, is_split_n);
159+
160+
if (header.find("Content-Length") != header.end()) {
161+
ContentLength = stoll(header["Content-Length"]);
162+
} else if (header.find("content-length") != header.end()) {
163+
ContentLength = stoll(header["content-length"]);
164+
}
165+
/********* 读取body数据内容 *********/
166+
body_data.insert(body_data.end(), data.begin() + split_index, data.end());
167+
}else {
168+
if (header.find("Content-Length") != header.end()) {
169+
ContentLength = stoll(header["Content-Length"]);
170+
} else if (header.find("content-length") != header.end()) {
171+
ContentLength = stoll(header["content-length"]);
172+
}
154173
}
155-
ssize_t ContentLength = 0;
156-
if (header.find("Content-Length") != header.end()) {
157-
ContentLength = stoll(header["Content-Length"]);
158-
} else if (header.find("content-length") != header.end()) {
159-
ContentLength = stoll(header["content-length"]);
160-
}
161-
/********* 读取body数据内容 *********/
162-
body_data.insert(body_data.end(), data.begin() + split_index, data.end());
163174
if (ContentLength > 0) {
164175
while (body_data.size() < ContentLength) {
165176
vector<unsigned char> buffer;

kProxy/kHttpd/kHttpdClient.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class kHttpdClient {
3333

3434
kHttpdClient(kHttpd *parent, int fd);
3535

36-
kHttpdClient(kHttpd *parent, int fd, const std::map<std::string, std::string> &header, std::string method,
36+
kHttpdClient(kHttpd *parent, int fd, const std::map<std::string, std::string> &header,
37+
std::vector<unsigned char> data, unsigned long int split_index, bool is_split_n,
38+
std::string method,
3739
std::string url_path,
3840
std::string http_version);
3941

kProxy/kHttpd/kWebSocketClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ int kWebSocketClient::run() {
130130
}
131131
_logger->i(TAG, __LINE__, "%s", SecWebSocketAccept.c_str());
132132
if ((connection != string("upgrade")) || (upgrade != string("websocket"))) {
133-
return kHttpdClient(parent, fd, header, method, url_path, http_version).run();
133+
return kHttpdClient(parent, fd, header, data, split_index, is_split_n,method, url_path, http_version).run();
134134
}
135135

136136
SecWebSocketKey = logger::trim(header["sec-websocket-key"]);

0 commit comments

Comments
 (0)