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

标签 r svg qr-code

我正在尝试生成 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") 

输出文件将是包含 QR 码的 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")

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

关于r - 如何生成 SVG 格式的二维码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47110197/

相关文章:

R匹配2个以上条件并返回响应值

R as.POSIXct 解析错误

java - 在java中解压字节数组

android - 在移动网站中创建将打开二维码扫描仪的链接

Tibble 数据类型的行求和

R pheatmap : change annotation colors and prevent graphics window from popping up

javascript - 如何向 SVG 中的椭圆元素添加轮廓/边框?

javascript - 如何使用 snap.svg 创建线条 - s.line 不是函数

javascript - 将 SVG 元素拖到新位置

c# - 报表查看器可以从表单传递图像吗?