-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathimgsave.cpp
57 lines (53 loc) · 2.3 KB
/
imgsave.cpp
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
#include "crow_all.h"
#include "base64.h"
#include <iostream>
#include <fstream>
#include <exception>
#include <utility>
#include <opencv2/core/mat.hpp> //cv::Mat
#include <opencv2/imgcodecs.hpp> //cv::IMREAD_UNCHANGED
#include <opencv2/imgproc.hpp> //cv::cvtColor, uchar?
using namespace std;
//compile with:
//export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<boost_installation_dir>/lib
//g++ imgsave.cpp <cpp-base64_installation_dir>/base64.cpp -I <crow_dir>/amalgamate/ -I <cpp-base64_installation_dir> -L <boost_installation_dir>/lib/ -lboost_system `pkg-config --cflags --libs opencv4` -lpthread -std=c++11
//note that imgsave.cpp and base64.cpp should be compiled together(g++ with 2 or more cpp is just like add_executable with 2 or more cpp)
//this service parse the request and save the image from request
int main(int argc, char* argv[]) {
// crow app
crow::SimpleApp app;
CROW_ROUTE(app, "/imgsave").methods("POST"_method)
//CROW_ROUTE(app, "/test").methods(crow::HTTPMethod::Post)
([](const crow::request& req){
crow::json::wvalue result;
result["flag"] = 0;
result["errorMsg"] = "";
std::ostringstream os;
try {
auto info = crow::json::load(req.body);
CROW_LOG_INFO << "parsing info...";
if(!info) CROW_LOG_INFO << "bad request";
CROW_LOG_INFO << "info has img?" << info.has("img");
string base64_string = info["img"].s();
// read in base 64 string
CROW_LOG_INFO << "img string length: " << base64_string.size();
string decoded_image = base64_decode(base64_string);
vector<uchar> data(decoded_image.begin(), decoded_image.end());
cv::Mat frame = imdecode(data, cv::IMREAD_UNCHANGED);
//cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB);
//CROW_LOG_INFO << "convert color successfully.";
cv::imwrite("abc.jpg", frame);
result["flag"] = 1;
os << crow::json::dump(result);
return crow::response{os.str()};
}
catch (exception& e){
CROW_LOG_INFO << "Unpredicted error: " << e.what();
result["errorMsg"] = "Unknowm Error";
os << crow::json::dump(result);
return crow::response(os.str());
}
});
app.port(8181).run();
return 0;
}