-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathluaqr.c
234 lines (195 loc) · 5.96 KB
/
luaqr.c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#define LUA_LIB
#include <core.h>
#include "qr.h"
static int QR_VERSION = 10;
static int QR_MODE = QR_EM_8BIT;
static int QR_LEVEL = 2;
static int QR_TYPE = -1;
// 二维码白边边距
static int MAX_SEP = 5;
// 二维码内容大小
static int MAX_MAG = 10;
static inline QRCode* qr_init(const char* text, size_t *tsize) {
int err = QR_ERR_NONE;
QRCode *qr = qrInit(QR_VERSION, QR_MODE, QR_LEVEL, QR_TYPE, &err);
if (!qr)
return NULL;
qrAddData(qr, (const qr_byte_t *)text, *tsize);
if (!qrFinalize(qr)){
qrDestroy(qr);
return NULL;
}
return qr;
}
static inline int qr_writefile(const char * filename, char* buf, size_t bsize) {
FILE *fp = fopen(filename, "wb");
if (!fp) {
free(buf);
return 0;
}
// 写入内容到文件
fwrite((const char*)buf, 1, bsize, fp); fflush(fp); fclose(fp); free(buf);
return 1;
}
static inline qr_byte_t* qr_to_buffer(QRCode* qr, qr_format_t qr_fmt, int *wsize) {
//两个5分别表示:像素之间的距离和二维码图片的放大倍数,范围都是1-16
switch(qr_fmt) {
case QR_FMT_PNG:
return qrSymbolToPNG(qr, MAX_SEP, MAX_MAG, wsize);
case QR_FMT_BMP:
return qrSymbolToBMP(qr, MAX_SEP, MAX_MAG, wsize);
case QR_FMT_SVG:
return qrSymbolToSVG(qr, MAX_SEP, MAX_MAG, wsize);
case QR_FMT_JSON:
return qrSymbolToJSON(qr, MAX_SEP, MAX_MAG, wsize);
default:
return NULL;
}
return NULL;
}
static int lwritePNG(lua_State *L) {
const char* filename = luaL_checkstring(L, 1);
size_t tsize = 0;
QRCode *qr = qr_init(luaL_checklstring(L, 2, &tsize), &tsize);
if (!qr)
return luaL_error(L, "[QR ERROR]: Can't create QR object.");
int wsize = 0;
qr_byte_t *buffer = qr_to_buffer(qr, QR_FMT_PNG, &wsize);
if (!buffer){
lua_pushnil(L);
lua_pushfstring(L, "[QR ERROR]: %s", qrGetErrorInfo(qr));
qrDestroy(qr);
return 2;
}
qrDestroy(qr);
int ok = qr_writefile(filename, (char *)buffer, wsize);
if (!ok) {
lua_pushnil(L);
lua_pushfstring(L, "[QR ERROR]: Failed to write to file[%s].", filename);
return 2;
}
return 1;
}
static int lwriteBMP(lua_State *L) {
const char* filename = luaL_checkstring(L, 1);
size_t tsize = 0;
QRCode *qr = qr_init(luaL_checklstring(L, 2, &tsize), &tsize);
if (!qr)
return luaL_error(L, "[QR ERROR]: Can't create QR object.");
int wsize = 0;
qr_byte_t *buffer = qr_to_buffer(qr, QR_FMT_BMP, &wsize);
if (!buffer){
lua_pushnil(L);
lua_pushfstring(L, "[QR ERROR]: %s", qrGetErrorInfo(qr));
qrDestroy(qr);
return 2;
}
qrDestroy(qr);
int ok = qr_writefile(filename, (char *)buffer, wsize);
if (!ok) {
lua_pushnil(L);
lua_pushfstring(L, "[QR ERROR]: Failed to write to file[%s].", filename);
return 2;
}
return 1;
}
static int lwriteSVG(lua_State *L) {
const char* filename = luaL_checkstring(L, 1);
size_t tsize = 0;
QRCode *qr = qr_init(luaL_checklstring(L, 2, &tsize), &tsize);
if (!qr)
return luaL_error(L, "[QR ERROR]: Can't create QR object.");
int wsize = 0;
qr_byte_t *buffer = qr_to_buffer(qr, QR_FMT_SVG, &wsize);
if (!buffer){
lua_pushnil(L);
lua_pushfstring(L, "[QR ERROR]: %s", qrGetErrorInfo(qr));
qrDestroy(qr);
return 2;
}
qrDestroy(qr);
int ok = qr_writefile(filename, (char *)buffer, wsize);
if (!ok) {
lua_pushnil(L);
lua_pushfstring(L, "[QR ERROR]: Failed to write to file[%s].", filename);
return 2;
}
return 1;
}
static int lwriteJSON(lua_State *L) {
const char* filename = luaL_checkstring(L, 1);
size_t tsize = 0;
QRCode *qr = qr_init(luaL_checklstring(L, 2, &tsize), &tsize);
if (!qr) {
return luaL_error(L, "[QR ERROR]: Can't create QR object.");
}
int wsize = 0;
qr_byte_t *buffer = qr_to_buffer(qr, QR_FMT_JSON, &wsize);
if (!buffer){
lua_pushnil(L);
lua_pushfstring(L, "[QR ERROR]: %s", qrGetErrorInfo(qr));
qrDestroy(qr);
return 2;
}
qrDestroy(qr);
int ok = qr_writefile(filename, (char *)buffer, wsize);
if (!ok) {
lua_pushnil(L);
lua_pushfstring(L, "[QR ERROR]: Failed to write to file[%s].", filename);
return 2;
}
return 1;
}
static int lsetMAG(lua_State *L) {
lua_Integer sep = luaL_checkinteger(L, 1);
if (sep <= 0 || sep > QR_SEP_MAX)
return luaL_error(L, "[QR ERROR]: `SEP` parameter must be greater than or equal to 1 and less than or equal to %d.", QR_SEP_MAX);
MAX_SEP = sep;
return 0;
}
static int lsetSEP(lua_State *L) {
lua_Integer mag = luaL_checkinteger(L, 1);
if (mag <= 0 || mag > QR_MAG_MAX)
return luaL_error(L, "[QR ERROR]: `MAG` parameter must be greater than or equal to 1 and less than or equal to %d.", QR_MAG_MAX);
MAX_MAG = mag;
return 0;
}
static int lsetVERSION(lua_State *L) {
lua_Integer version = luaL_checkinteger(L, 1);
if (version < 1 || version > QR_VER_MAX)
return luaL_error(L, "[QR ERROR]: `QR_VERSION` parameter must be greater than or equal to 1 and less than or equal to %d.", QR_VER_MAX);
QR_VERSION = version;
return 0;
}
static int lsetLEVEL(lua_State *L) {
lua_Integer level = luaL_checkinteger(L, 1);
if (level < QR_ECL_L || level > QR_ECL_H)
return luaL_error(L, "[QR ERROR]: `QR_LEVEL` parameter must be greater than or equal to %d and less than or equal to %d.", QR_ECL_L, QR_ECL_H);
QR_LEVEL = level;
return 0;
}
static int lsetTYPE(lua_State *L) {
lua_Integer type = luaL_checkinteger(L, 1);
if (type < -1 || type >= QR_MPT_MAX)
return luaL_error(L, "[QR ERROR]: `QR_TYPE` parameter must be greater than or equal to %d and less than or equal to %d.", -1, QR_MPT_MAX);
QR_TYPE = type;
return 0;
}
LUAMOD_API int luaopen_qr(lua_State* L) {
luaL_checkversion(L);
luaL_Reg qr_libs[] = {
{ "toPNG", lwritePNG },
{ "toBMP", lwriteBMP },
{ "toSVG", lwriteSVG },
{ "toJSON", lwriteJSON },
// 全局配置
{ "setMAG", lsetMAG },
{ "setSEP", lsetSEP },
{ "setTYPE", lsetTYPE },
{ "setLEVEL", lsetLEVEL },
{ "setVERSION", lsetVERSION },
{ NULL, NULL }
};
luaL_newlib(L, qr_libs);
return 1;
}