LoginSignup
31
30

More than 5 years have passed since last update.

Rubyで簡単にQRコード画像を生成する方法

Last updated at Posted at 2014-02-25

Rubyで簡単にQRコード画像を生成する方法

gemをインストール

$ gem install rqrcode_png

依存関係のあるライブラリ「rqrcode」と「chunky_png」もインストールされます。

実装例

「Hello World!!」という情報をQRコードに埋め込み、PNGファイルで保存します。

# -*- encoding: sjis -*-
require 'rqrcode'
require 'rqrcode_png'
require 'chunky_png'

# 「Hello Wolrd!!」いう文字列、サイズは3、誤り訂正レベルHのQRコードを生成する
qr = RQRCode::QRCode.new( "Hello World!!", :size => 3, :level => :h )
png = qr.to_img

#200×200にリサイズして「hello_world.png」というファイル名で保存する
png.resize(200, 200).save("hello_world.png")

メモ:エンコードをShift-JISに指定しないと上手くQRコードの画像が生成されませんでした。

出力結果

hello_world_size_mini.png

「hello_world.png」という名前のQRコードの画像ファイルが出力されます。

31
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
30