如何生成 SVG 格式的二维码?

How to generate a QR in SVG format?

我正在尝试生成 SVG 格式的二维码。不幸的是,R qrencoder 包只生成光栅图像,例如PNG 等。因此,这是我第一次尝试生成它,在屏幕上绘制它,捕获屏幕,最后将屏幕写成 SVG。也许有更简单的方法来完成这个?

library(rsvg)
library(raster)
library(qrencoder)
library(svglite)

tmp <- tempfile()
svglite::svglite(tmp, width = 10, height = 7)
#==============================================
old_mar <- par()$mar
par(mar=c(0,0,0,0))
image(qrencoder::qrencode_raster("http://rud.is/b"), asp=1, col=c("white", "black"),
  axes=FALSE, xlab="", ylab="")
par(mar=old_mar)
#==============================================
dev.off()
rsvg::rsvg_svg(tmp, "out.svg") 

输出文件将 out.svg 包含二维码。但是,执行最后一行 rsvg_svg 会导致以下错误:

(process:733): GdkPixbuf-WARNING **: Cannot open pixbuf loader module file '/usr/local/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache': No such file or directory

This likely means that your installation is broken.
Try running the command
  gdk-pixbuf-query-loaders > /usr/local/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache
to make things work again for the time being.

(process:733): GdkPixbuf-WARNING **: Cannot open pixbuf loader module file '/usr/local/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache': No such file or directory

This likely means that your installation is broken.
Try running the command
  gdk-pixbuf-query-loaders > /usr/local/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache
to make things work again for the time being.

(process:733): GdkPixbuf-WARNING **: Cannot open pixbuf loader module file '/usr/local/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache': No such file or directory

This likely means that your installation is broken.
Try running the command
  gdk-pixbuf-query-loaders > /usr/local/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache
to make things work again for the time being.

我看到 Pixbuf 已正确安装在 macports 中:

/var/root$ port installed | grep pixbuf
gdk-pixbuf2 @2.36.9_0+x11
gdk-pixbuf2 @2.36.11_0+x11 (active)

我不知道真正的问题是什么...有没有更简单的方法来生成 SVG 格式的二维码?也许更简单 API 最好不要涉及必须绘制到屏幕、捕获屏幕、保存文件并重新加载?

否则有人可以建议修复此错误吗?

您也可以从 qrencoder 生成的内联 png 代码中自己制作 svg(这只是一个文本文件),如

library(qrencoder)

qrencode_svg <- function(string, filename, width = "2cm", height = "2cm") {
  inline_png <- qrencode_png(string)
  con <- file(filename, open = "w+")
  cat("<?xml version='1.0' encoding='UTF-8' ?>\n", file = con)
  cat("<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' ",
      "width='", width, "' height='", height, "'>\n", file = con, sep = "")
  cat("<image width='", width, "' height='", height, "' x='0' y='0' xlink:href='", inline_png, "'/>\n",
      file = con, sep = "")
  cat("</svg>\n", file = con)
  close(con)
}
qrencode_svg("http://rud.is/b", "test.svg", width = "2cm", height = "2cm")

但是,当尺寸太大时,结果会变得模糊,所以我只是为了完整性而在这里发布。或者也许有人知道如何改进它。

qrencoder 的 0.2.0 版现在初步支持 SVG(感谢这个问题)。它在 C:

中构建 SVG
# devtools::install_github("hrbrmstr/qrencoder")

library(qrencoder)

cat(qrencode_svg("https://rud.is/b"))
#> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
#> <!-- Created with qrencode 3.9.0 (http://fukuchi.org/works/qrencode/index.html.en) -->
#> <svg width="3.07cm" height="3.07cm" viewBox="0 0 29 29" preserveAspectRatio="none" version="1.1" xmlns="http://www.w3.org/2000/svg">
#>  <g id="QRcode">
#>      <rect x="0" y="0" width="29" height="29" fill="#ffffff" />
#>      <g id="Pattern">
#>          <rect x="4" y="4" width="7" height="1" fill="#000000" />
#>          <rect x="13" y="4" width="1" height="1" fill="#000000" />
#>          <rect x="16" y="4" width="1" height="1" fill="#000000" />
#>          <rect x="18" y="4" width="7" height="1" fill="#000000" />
#>          <rect x="4" y="5" width="1" height="1" fill="#000000" />
...

实例(将 ^^ 写入文件):

让我知道您想要什么 API 改变。我可能会将其他参数(如边距 - 因此下面的空格 - 以及其他参数)添加到其他函数中。我在输出方面有很大的灵活性,并且倾向于只在看到 GH 问题或 SO 问题时调整 pkg。

我很乐意让 "Created with" 发表评论,因为我正在使用(并广泛归因于)qrencode 库,并且作者的工作值得称赞。但我也可以将其设为关闭选项。