Skip to content

Commit

Permalink
Fix parsing QR code
Browse files Browse the repository at this point in the history
this fixes #374
  • Loading branch information
paolostivanin committed Jul 29, 2024
1 parent 17dfdc2 commit 5c2235d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/common/parse-uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,11 @@ parse_parameters (const gchar *modified_uri,
gchar *escaped_issuer_and_label = g_uri_unescape_string (tokens[0], NULL);
gchar *mod_uri_copy_utf8 = g_utf8_offset_to_pointer (cleaned_uri, g_utf8_strlen (tokens[0], -1) + 1);
g_strfreev (tokens);
g_free (cleaned_uri);

tokens = g_strsplit (escaped_issuer_and_label, ":", -1);
if (tokens[0] && tokens[1]) {
otp->issuer = g_strdup (g_strstrip (tokens[0]));
otp->account_name = g_strdup (g_strstrip (tokens[1]));

} else {
otp->account_name = g_strdup (g_strstrip (tokens[0]));
}
Expand Down Expand Up @@ -226,6 +224,7 @@ parse_parameters (const gchar *modified_uri,
i++;
}
g_strfreev (tokens);
g_free (cleaned_uri);
}


Expand All @@ -237,4 +236,4 @@ remove_null_encoding (const gchar *uri)
g_regex_unref (regex);

return cleaned_uri;
}
}
20 changes: 15 additions & 5 deletions src/gui/qrcode-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,29 @@ parse_qrcode (const gchar *png_path,
gint width = gdk_pixbuf_get_width (pbuf);
gint height = gdk_pixbuf_get_height (pbuf);
guchar *raw_data = gdk_pixbuf_get_pixels (pbuf);
gint rowstride = gdk_pixbuf_get_rowstride (pbuf);
gint n_channels = gdk_pixbuf_get_n_channels (pbuf);

guchar *gray_data = g_malloc0 (width * height);

// we need to convert RGB data to grayscale, otherwise QR parsing will fail
for (gint y = 0; y < height; y++) {
for (gint x = 0; x < width; x++) {
guchar *p = raw_data + y * rowstride + x * n_channels;
gray_data[y * width + x] = (p[0] * 0.299) + (p[1] * 0.587) + (p[2] * 0.114);
}
}
g_object_unref (pbuf);

zbar_image_t *image = zbar_image_create ();
zbar_image_set_format (image, zbar_fourcc ('Y','8','0','0'));
zbar_image_set_size (image, width, height);
zbar_image_set_data (image, raw_data, width * height, zbar_image_free_data);
zbar_image_set_data (image, gray_data, width * height, zbar_image_free_data);

gint n = zbar_scan_image (scanner, image);
if (n < 1) {
zbar_image_destroy (image);
zbar_image_scanner_destroy (scanner);
g_object_unref (pbuf);
return g_strdup ("Couldn't find a valid qrcode");
}

Expand All @@ -41,7 +53,5 @@ parse_qrcode (const gchar *png_path,
zbar_image_destroy (image);
zbar_image_scanner_destroy (scanner);

g_object_unref (pbuf);

return NULL;
}
}

0 comments on commit 5c2235d

Please sign in to comment.