2017-11-04 7 views
3

私はSVG形式でQRコードを生成しようとしています。残念ながら、R qrencoderパッケージはラスタ画像のみを生成する。 PNGなどです。したがって、これを最初に生成し、それを画面にプロットし、画面をキャプチャし、最後にSVGとして画面に書き込みます。おそらくこれを達成するためのより簡単な方法がありますか?QRを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形式でQRを生成する簡単な方法はありますか?おそらくより簡単なAPIは、好ましくは画面にプロットする必要はない、画面をキャプチャし、ファイルを保存し、それをリロードする?

は、そうでなければ誰もがこのエラーの修正を提案することができますか?

+0

は、単純に何かがあるかもしれませんが、あなたは 'BMP'または** NetPBMの**' PGM'ファイルを生成するために 'qrencoder'を説得できるかどうか、あなたは' SVG'にそれを変換するために、 'potrace'を使用することができます。 'potrace'は** homebrew **から利用できます。 –

+0

これを問題として投稿するか、このページに追加できますか?https://github.com/hrbrmstr/qrencoder/issues/5を作成しましたか?あなたの望む機能を明示的に述べると助けになります。 'magick'パッケージの出現により、' qrencoder'はコードリフレッシュを使用し、ディスクの往復の必要性を完全に緩和する可能性があります。 – hrbrmstr

答えて

1

バージョンqrencoderの0.2.0は今(この質問へのTHX)予備SVGをサポートしています。これは、SVGはCで構築を行います。

# 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を微調整する傾向があります。

私はqrencodeライブラリとその著者が自分の仕事のための信用に値するし、私が使用して(と広く属性)てるので、そこでのコメント「で作成した」せるのに夢中です。しかし、私はそれをオフにするオプションにすることもできます。

+0

うわー! –

+0

BtwはこれはSVGカイロ形式ですか? RのSVGにアクセスするために私が知っているAPIは主にAFAIKであるため、質問する価値があります。このパッケージで再び素晴らしい仕事を! –

+0

標準に準拠したSVGです。これは、Rライブラリ、SVGタグだけで構築されたものではありません。 – hrbrmstr

2

サイズが大きすぎる場合

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によって生成されたインラインpngのコードから(単なるテキストファイルです)、SVGを自分で作ることができます私は完全性のためにここに投稿しています。あるいは、誰かがそれを改善する方法を知っているかもしれません。

関連する問題