gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:28:36 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com