|
|
@@ -4,6 +4,9 @@
|
|
|
#include "elog.h"
|
|
|
|
|
|
|
|
|
+#define QRC_RESIZE
|
|
|
+
|
|
|
+
|
|
|
#if 1
|
|
|
#define LOGD log_d
|
|
|
#define LOGE log_e
|
|
|
@@ -73,41 +76,115 @@ static int base64_encode(uint8_t *src, size_t slen, uint8_t *dst, size_t dlen)
|
|
|
return (p - dst);
|
|
|
}
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
-#define ARGB(a,r,g,b) (((a)<<24) | ((r)<<16) | ((g)<<8) | (b))
|
|
|
+#if 1
|
|
|
+static inline int in_array(int x, int y, int w, int h)
|
|
|
+{
|
|
|
+ if (x >= 0 && x < w && y >= 0 && y < h)
|
|
|
+ return 1;
|
|
|
+ else
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+static uint32_t* rgb_resize(uint32_t* in, int w, int h, int w2, int h2)
|
|
|
+{
|
|
|
+ int i,j,x1, y1, x2, y2;
|
|
|
+ double f11, f12, f21, f22;
|
|
|
+ double a,b,c,d,e,f;
|
|
|
+ double h_times = (double)h2 / (double)h,
|
|
|
+ w_times = (double)w2 / (double)w;
|
|
|
+
|
|
|
+ uint32_t *out=(uint32_t*)malloc(h2*w2*4);
|
|
|
+ if(out) {
|
|
|
+ for (i=0; i<h2; i++){
|
|
|
+ for (j=0; j<w2; j++) {
|
|
|
+#if 0
|
|
|
+ double x = j / w_times;
|
|
|
+ double y = i / h_times;
|
|
|
+
|
|
|
+ x1 = (int)(x - 1);
|
|
|
+ x2 = (int)(x + 1);
|
|
|
+ y1 = (int)(y - 1);
|
|
|
+ y2 = (int)(y + 1);
|
|
|
+ f11 = in_array(x1, y1, h, w) ? in[y1*w+x1] : 0;
|
|
|
+ f12 = in_array(x1, y2, h, w) ? in[y2*w+x1] : 0;
|
|
|
+ f21 = in_array(x2, y1, h, w) ? in[y1*w+x2] : 0;
|
|
|
+ f22 = in_array(x2, y2, h, w) ? in[y2*w+x2] : 0;
|
|
|
+
|
|
|
+ a = f11 * (x2 - x) * (y2 - y);
|
|
|
+ b = f12 * (x2 - x) * (y - y1);
|
|
|
+ c = f21 * (x - x1) * (y2 - y);
|
|
|
+ d = f22 * (x - x1) * (y - y1);
|
|
|
+ e = (x2 - x1) * (y2 - y1);
|
|
|
+ f = (a +b + c + d) / e;
|
|
|
+
|
|
|
+ out[i*w2+j] = (uint32_t)f;
|
|
|
+#else
|
|
|
+ int x = j / w_times;
|
|
|
+ int y = i / h_times;
|
|
|
+ out[i*w2+j] = in[y*w+x];
|
|
|
+#endif
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return out;
|
|
|
+}
|
|
|
+#else
|
|
|
+
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+#define RGBA(r,g,b,a) (((a)<<24) | ((r)<<16) | ((g)<<8) | (b))
|
|
|
int qrc_to_png(char *txt, char *path)
|
|
|
{
|
|
|
int r,x,y;
|
|
|
QRCode qrc;
|
|
|
-
|
|
|
- int ver=6,ecc=ECC_QUARTILE;
|
|
|
+ int size,size2;
|
|
|
+ int ver=8,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);
|
|
|
|
|
|
r = qrcode_initText(&qrc, pQRC, ver, ecc, txt);
|
|
|
- if(!pQRC || !pRGB || r<0) {
|
|
|
+ if(!pQRC || r<0) {
|
|
|
if(pQRC) free(pQRC);
|
|
|
- if(pRGB) free(pRGB);
|
|
|
return -1;
|
|
|
}
|
|
|
-
|
|
|
- for (y=0; y<qrc.size; y++) { // row
|
|
|
- for (x=0; x<qrc.size; x++) {// module pixel (height)
|
|
|
+ size = qrc.size;
|
|
|
+
|
|
|
+ uint32_t *pRGB=(uint32_t*)malloc(size*size*4);
|
|
|
+ if(!pRGB) {
|
|
|
+ free(pQRC);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (y=0; y<size; y++) { // row
|
|
|
+ for (x=0; x<size; x++) {// module pixel (height)
|
|
|
if (qrcode_getModule(&qrc, x, y)) {
|
|
|
- pRGB[y*qrc.size+x] = ARGB(255,0,0,0);
|
|
|
+ pRGB[y*size+x] = RGBA(0,0,0,255);
|
|
|
}
|
|
|
else {
|
|
|
- pRGB[y*qrc.size+x] = ARGB(255,255,255,255);
|
|
|
+ pRGB[y*qrc.size+x] = RGBA(255,255,255,255);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- int pngSize;
|
|
|
- char *pPNG = rpng_save_image_to_memory((const char*)pRGB, qrc.size, qrc.size, 4, 8, &pngSize);
|
|
|
+#ifdef QRC_RESIZE
|
|
|
+ size2 = size*6;
|
|
|
+ uint32_t *pRGB2 = rgb_resize(pRGB, size, size, size2, size2);
|
|
|
+ if(!pRGB2) {
|
|
|
+ free(pQRC); free(pRGB);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ free(pRGB);
|
|
|
+ pRGB = pRGB2;
|
|
|
+ size = size2;
|
|
|
+#endif
|
|
|
+
|
|
|
+ int pngSize=0;
|
|
|
+ char *pPNG = rpng_save_image_to_memory((const char*)pRGB, size, size, 4, 8, &pngSize);
|
|
|
if(pPNG) {
|
|
|
r = file_save(path, pPNG, pngSize);
|
|
|
|
|
|
-#if 0
|
|
|
+#if 0 //def QRC_BASE64
|
|
|
char *p64buf=NULL;
|
|
|
int b64len = base64_len(pngSize)+8;
|
|
|
p64buf = (char*)malloc(b64len);
|