Quellcode durchsuchen

添加png缩放,原始的二维码有点小

guohui vor 1 Jahr
Ursprung
Commit
e3aeb5cdef
4 geänderte Dateien mit 95 neuen und 20 gelöschten Zeilen
  1. 91 14
      pro/qrcode/qrc.c
  2. 2 2
      pro/qrcode/rpng.h
  3. 0 2
      pro/src/json_handle.c
  4. 2 2
      pro/src/sys.c

+ 91 - 14
pro/qrcode/qrc.c

@@ -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);

+ 2 - 2
pro/qrcode/rpng.h

@@ -1252,7 +1252,7 @@ char *rpng_load_image_from_memory(const char *buffer, int *width, int *height, i
                 int pixel_size = *color_channels*(*bit_depth/8);
                 data = rpng_inflate_image_data(chunk_image.data, chunk_image.length, *width, *height, pixel_size);
 
-                if (data == NULL) RPNG_LOG("WARNING: IDAT image data ¡decompression failed\n");
+                if (data == NULL) RPNG_LOG("WARNING: IDAT image data decompression failed\n");
             }
             else RPNG_LOG("WARNING: CRC not valid, IDAT chunk image data could be corrupted\n");
         }
@@ -1334,7 +1334,7 @@ char *rpng_load_image_indexed_from_memory(const char *buffer, int *width, int *h
                     int pixel_size = (IHDRData->bit_depth/8); // NOTE: Assume 1 channel
                     data = rpng_inflate_image_data(chunk_image.data, chunk_image.length, *width, *height, pixel_size);
 
-                    if (data == NULL) RPNG_LOG("WARNING: IDAT image data ¡decompression failed\n");
+                    if (data == NULL) RPNG_LOG("WARNING: IDAT image data �decompression failed\n");
                 }
                 else RPNG_LOG("WARNING: CRC not valid, IDAT chunk image data could be corrupted\n");
             }

+ 0 - 2
pro/src/json_handle.c

@@ -1607,8 +1607,6 @@ char* vendor_info_to_json(GlobalDeviceInfo *dev)
         cJSON* data_field = cJSON_CreateObject();
         if(data_field && dev) {
 
-            sys_get_prod(dev);
-
             cJSON_AddStringToObject(data_field, "productName", dev->product.name);
 
             sprintf(temp, "%d", dev->product.id);

+ 2 - 2
pro/src/sys.c

@@ -1471,10 +1471,10 @@ int sys_get_prod(GlobalDeviceInfo *info)
     if(p) LOGD("___%s:%s\n", tok, p);
 
     sys_get_path(path, "../web/img/shdh.png");
-    //r = qrc_to_png(info->service.tele, path);
+    r = qrc_to_png(info->service.tele, path);
 
     sys_get_path(path, "../web/img/sbh.png");
-    //r = qrc_to_png(info->product.number, path);
+    r = qrc_to_png(info->product.number, path);
 
     free(fbuf.buf);
     return 0;