|
@@ -1,6 +1,8 @@
|
|
|
-#include "elog.h"
|
|
|
|
|
#include "qrcode.h"
|
|
#include "qrcode.h"
|
|
|
#include "rpng.h"
|
|
#include "rpng.h"
|
|
|
|
|
+#include "file.h"
|
|
|
|
|
+#include "elog.h"
|
|
|
|
|
+
|
|
|
|
|
|
|
|
#if 1
|
|
#if 1
|
|
|
#define LOGD log_d
|
|
#define LOGD log_d
|
|
@@ -12,20 +14,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-int qrc_make(char *txt, )
|
|
|
|
|
|
|
+static const uint8_t base64_enc_map[64] =
|
|
|
{
|
|
{
|
|
|
- QRCode qrcode;
|
|
|
|
|
- uint8_t qrcodeBytes[qrcode_getBufferSize()];
|
|
|
|
|
-
|
|
|
|
|
- qrcode_initText(&qrcode, qrcodeBytes, 3, ECC_LOW, txt;
|
|
|
|
|
|
|
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
|
|
|
|
|
+ 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
|
|
|
|
|
+ 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
|
|
|
|
|
+ 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
|
|
|
|
+ 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
|
|
|
|
|
+ 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
|
+ '8', '9', '+', '/'
|
|
|
|
|
+};
|
|
|
|
|
+static int base64_len(int n)
|
|
|
|
|
+{
|
|
|
|
|
+ return ((n / 3) + (n % 3 ? 1 : 0)) * 4 + 1;
|
|
|
|
|
+}
|
|
|
|
|
+static int base64_encode(uint8_t *src, size_t slen, uint8_t *dst, size_t dlen)
|
|
|
|
|
+{
|
|
|
|
|
+ size_t i, n;
|
|
|
|
|
+ int C1, C2, C3;
|
|
|
|
|
+ uint8_t *p;
|
|
|
|
|
+
|
|
|
|
|
+ if( slen == 0 ) {
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ n = (slen / 3 + ( slen % 3 != 0 )) * 4;
|
|
|
|
|
+ if( ( dlen < n + 1 ) || ( NULL == dst ) ) {
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ n = ( slen / 3 ) * 3;
|
|
|
|
|
+ for( i = 0, p = dst; i < n; i += 3 ) {
|
|
|
|
|
+ C1 = *src++;
|
|
|
|
|
+ C2 = *src++;
|
|
|
|
|
+ C3 = *src++;
|
|
|
|
|
+
|
|
|
|
|
+ *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
|
|
|
|
|
+ *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
|
|
|
|
|
+ *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
|
|
|
|
|
+ *p++ = base64_enc_map[C3 & 0x3F];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if( i < slen ) {
|
|
|
|
|
+ C1 = *src++;
|
|
|
|
|
+ C2 = ( ( i + 1 ) < slen ) ? *src++ : 0;
|
|
|
|
|
+
|
|
|
|
|
+ *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
|
|
|
|
|
+ *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
|
|
|
|
|
+
|
|
|
|
|
+ if( ( i + 1 ) < slen )
|
|
|
|
|
+ *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
|
|
|
|
|
+ else *p++ = '=';
|
|
|
|
|
+
|
|
|
|
|
+ *p++ = '=';
|
|
|
|
|
+ }
|
|
|
|
|
+ *p = 0;
|
|
|
|
|
+
|
|
|
|
|
+ return (p - dst);
|
|
|
|
|
+}
|
|
|
|
|
+//////////////////////////////////////////////////////////////
|
|
|
|
|
+#define ARGB(a,r,g,b) (((a)<<24) | ((r)<<16) | ((g)<<8) | (b))
|
|
|
|
|
+char* qrc_txt_to_png(char *txt)
|
|
|
|
|
+{
|
|
|
|
|
+ QRCode qrc;
|
|
|
|
|
+ int x,y;
|
|
|
|
|
+ char *p64buf=NULL;
|
|
|
|
|
+ uint8_t blackPixel=0x00,whitePixel=0xFF;
|
|
|
|
|
|
|
|
- for (uint8 y = 0; y < qrcode.size; y++) {
|
|
|
|
|
- for (uint8 x = 0; x < qrcode.size; x++) {
|
|
|
|
|
- if (qrcode_getModule(&qrcode, x, y) {
|
|
|
|
|
|
|
+ int ver=6,ecc=ECC_QUARTILE;
|
|
|
|
|
+ uint16_t bufsz=qrcode_getBufferSize(ver);
|
|
|
|
|
+ uint8_t *pQRC=(uint8_t*)malloc(bufsz);
|
|
|
|
|
+ uint32_t *pRGB=(uint32_t*)malloc(qrc.size*qrc.size);
|
|
|
|
|
+
|
|
|
|
|
+ if(!pQRC || !pRGB) {
|
|
|
|
|
+ if(pQRC) free(pQRC);
|
|
|
|
|
+ if(pRGB) free(pRGB);
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ }
|
|
|
|
|
+ qrcode_initText(&qrc, pQRC, ver, ecc, txt);
|
|
|
|
|
+
|
|
|
|
|
+ for (y=0; y<qrc.size; y++) { // row
|
|
|
|
|
+ for (x=0; x<qrc.size; x++) {// module pixel (height)
|
|
|
|
|
+ if (qrcode_getModule(&qrc, x, y)) {
|
|
|
|
|
+ pRGB[y*qrc.size+x] = ARGB(255,0,0,0);
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ pRGB[y*qrc.size+x] = ARGB(200,255,255,255);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ int pngSize;
|
|
|
|
|
+ char *pPNG = rpng_save_image_to_memory((const char*)pRGB, qrc.size, qrc.size, 4, 8, &pngSize);
|
|
|
|
|
+ if(pPNG) {
|
|
|
|
|
+ //file_save("/tmp/xxx.png", pPNG, pngSize);
|
|
|
|
|
+
|
|
|
|
|
+ int b64len = base64_len(pngSize)+8;
|
|
|
|
|
+ p64buf = (char*)malloc(b64len);
|
|
|
|
|
+ if(p64buf) {
|
|
|
|
|
+
|
|
|
|
|
+ extern char *rpng_save_image_to_memory(const char *data, int width, int height, int color_channels, int bit_depth, int *output_size);
|
|
|
|
|
+ int d64len = base64_encode((uint8_t*)pPNG, pngSize, (uint8_t*)p64buf, b64len);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+ RPNG_FREE(pPNG);
|
|
|
|
|
+ }
|
|
|
|
|
+ free(pRGB);
|
|
|
|
|
+ free(pQRC);
|
|
|
|
|
|
|
|
|
|
+ return p64buf;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+int qrc_free(void *p)
|
|
|
|
|
+{
|
|
|
|
|
+ if(!p) {
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+ free(p);
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|