今天准备记录一个用起来比较简单的gem,因为时间有点太晚了,:P
好,下面还是先做一个简单的介绍。
功能简介
rQRCode 是一个Ruby封装的二维码生成gem。其中包括所有标准二维码操作的简单接口。首先呢,这个gem是一个独立的类库,不依赖于任何其他类库。其中只用到了Ruby。其次,这是一个编码的gem,只能转换成二维码,不能将二维码进行解码。最后,这个gem的接口很简单,并且假设你只想把一个string转换成二维码。
基本使用
安装
加载
1
2
require 'rubygems'
require 'rqrcode'
输出二维码
1
2
3
4
5
6
7
8
qr = RQRCode : :QRCode . new ( 'my string to generate' , :size => 4 , :level => :h )
puts qr . to_s
#
# Prints:
# xxxxxxx x x x x x xx xxxxxxx
# x x xxx xxxxxx xxx x x
# x xxx x xxxxx x xx x xxx x
# ... etc
在Ruby on Rails中生成template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Controller
@qr = RQRCode : :QRCode . new ( 'my string to generate' , :size => 4 , :level => :h )
# View: (minimal styling added)
< style type = "text/css" >
table {
border - width : 0 ;
border - style : none ;
border - color : #0000ff;
border - collapse : collapse ;
}
td {
border - width : 0 ;
border - style : none ;
border - color : #0000ff;
border - collapse : collapse ;
padding : 0 ;
margin : 0 ;
width : 10 px ;
height : 10 px ;
}
td . black { background - color : #000; }
td . white { background - color : #fff; }
< /style>
<table>
<% @qr.modules.each_index do |x| %>
<tr>
<% @qr.modules.each_index do |y| %>
<% if @qr.dark?(x,y) %>
<td class="black"/ >
< % else %>
<td class="white"/>
< % end %>
<% end %>
< /tr>
<% end %>
</ table >
更多信息
基本的调用方法就这些,还算简单吧。更多周边信息可以查看原文档 。