-
Notifications
You must be signed in to change notification settings - Fork 0
/
devcon_ticket.cpp
178 lines (125 loc) · 4.51 KB
/
devcon_ticket.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// Created by Kaung Zaw Htet on 2019-10-30.
//
#include "devcon_ticket.h"
#include <iostream>
#include "csv_writer.h"
#include <random>
#include "qr/QrCode.hpp"// chrono::system_clock
#include <ctime>
#include <iomanip>
#include <opencv2/opencv.hpp>
#include <Magick++.h>
#include <fstream>
#include <stdlib.h>
#include <string>
using namespace qrcodegen;
using namespace cv;
void ticket::generateID() {
mt19937 generatorObject(static_cast<unsigned int>(time(nullptr)));
std::uniform_int_distribution<long> idDistribution(8000000000, 8999999999);
long generatedID = idDistribution(generatorObject);
ostringstream strStream;
strStream << generatedID;
ticket::id = "DC" + strStream.str();
}
void ticket::generateQRCode(std::string key) {
std::cout << key << '\n';
QrCode qr0 = QrCode::encodeText(key.c_str(), QrCode::Ecc::HIGH);
std::string svg = qr0.toSvgString(4);
fstream file;
ticket::qr_filename_svg = key + ".svg";
file.open(ticket::qr_filename_svg, ios::out);
file << svg;
file.close();
ticket::qr_filename_png = key + ".png";
std::string commend = "./convert -density 450 " + ticket::qr_filename_svg + " " + ticket::qr_filename_png;
system(commend.c_str());
/*Magick::Geometry geometry(135,135,0,0);
Magick::ColorspaceType sp();
Magick::Image svgImage(qr_filename_svg);
// svgImage.fillColor(Magick::Color(0,0,0));
// svgImage.backgroundColor(Magick::Color(255,255,255));
svgImage.magick("png");
svgImage.write(qr_filename_png);*/
}
int ticket::mergeQRCodeNtemplate(std::string key) {
Mat ticket_template, qr_image;
ticket_template = imread("lib/workspace_template.jpg", 1);
qr_image = imread(ticket::qr_filename_png, 1);
if (!ticket_template.data) {
cout << "Error loading src1" << endl;
return -1;
}
if (!qr_image.data) {
cout << "Error loading src2" << endl;
return -1;
}
qr_image.copyTo(ticket_template(cv::Rect(1000, 400, qr_image.cols, qr_image.rows)));
qr_image.copyTo(ticket_template(cv::Rect(1450, 300, qr_image.cols, qr_image.rows)));
if (ticket::is_ticket_id_displayed) {
putText(ticket_template, key, Point(1000, 590),
FONT_HERSHEY_COMPLEX_SMALL, 1, Scalar(255, 255, 255), 2);
putText(ticket_template, key, Point(1450, 265),
FONT_HERSHEY_COMPLEX_SMALL, 1, Scalar(255, 255, 255), 2);
}
std::string ticket = key + ".jpg";
imwrite(ticket, ticket_template);
return 0;
}
void ticket::writeCSV() {
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %H:%M:%S");
string line;
int id_count[2]; //user first, ticket second
ifstream file_in("lib/persistence.txt");
if (file_in.is_open()) {
int i = 0;
while (getline(file_in, line)) {
id_count[i] = stoi(line);
i++;
}
file_in.close();
}
CSVWriter csv;
csv << id_count[0] << id_count[1] << "NULL" << ticket::id << "0" << ss.str() << ss.str();
csv.writeToFile("ticket_id.csv", true);
ofstream file_out;
file_out.open("lib/persistence.txt", ios::out);
file_out << ++id_count[0] << '\n' << ++id_count[1];
file_out.close();
}
void ticket::generateRawTicket() {
ticket::generateID();
ticket::generateQRCode(ticket::id);
ticket::writeCSV();
int err_flag = ticket::mergeQRCodeNtemplate(ticket::id);
if (err_flag == -1) exit(err_flag);
std::remove(ticket::qr_filename_svg.c_str());
std::remove(ticket::qr_filename_png.c_str());
}
void ticket::generateTicketByCSV(std::string csv_file_name) {
std::ifstream data(csv_file_name);
// std::cout<<csv_file_name;
std::string line;
while (std::getline(data, line)) {
int i = 0;
std::stringstream lineStream(line);
std::string cell;
std::string parsedRow[6];
while (std::getline(lineStream, cell, ',')) {
std::string actual_data(cell.begin() + 1, cell.end() - 1);
parsedRow[i] = actual_data;
// std::cout<<actual_data<<',';
i++;
}
ticket::ticket_id = parsedRow[2];
ticket::generateQRCode(ticket::ticket_id);
int err_flag = ticket::mergeQRCodeNtemplate(ticket::ticket_id);
if (err_flag == -1) exit(err_flag);
std::remove(ticket::qr_filename_svg.c_str());
std::remove(ticket::qr_filename_png.c_str());
}
}