forked from Arachnid/pyqrencode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr_encode.c
40 lines (33 loc) · 1.06 KB
/
qr_encode.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
#include <Python.h>
#include <stdlib.h>
#include <qrencode.h>
static PyObject *qr_encode(PyObject *self, PyObject *args)
{
char *str;
int i, version, level, hint, case_sensitive, num_pixels;
QRcode *code;
PyObject *ret;
if(!PyArg_ParseTuple(args, "siiii", &str, &version, &level, &hint,
&case_sensitive))
return NULL;
code = QRcode_encodeString(str, version, level, hint, case_sensitive);
num_pixels = code->width * code->width;
for(i = 0; i < num_pixels; i++)
code->data[i] = 255 - (code->data[i] & 0x1) * 0xFF;
ret = Py_BuildValue("(iis#)", code->version, code->width,
code->data, num_pixels);
QRcode_free(code);
return ret;
};
static PyMethodDef qr_encode_methods[] =
{
{"encode", qr_encode, METH_VARARGS, "Encodes a string as a QR-code. Returns a tuple of (version, width, data)"},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initqr_encode(void)
{
PyObject *m = Py_InitModule("qr_encode", qr_encode_methods);
if(m == NULL)
return;
}