三种方式:
1.页面table
gem 'rqrcode'
controller:
url = "www.baidu.com"
@qr = RQRCode::QRCode.new(url, :size => 3, :level => 'l'.to_sym)
view:
<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: 10px;
height: 10px;
}
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>
2、直接在页面引用
gem 'rqrcode-rails3'
gem 'mini_magick'
controller:
respond_to do |format|
format.html
format.svg { render :qrcode => url, :level => :l, :unit => 10 }
format.png { render :qrcode => url }
format.gif { render :qrcode => url }
format.jpeg { render :qrcode => url }
end
view
<img src="action_path.jpeg" />
3、生成图片
controller:
format = :png
size = 3
level = :h
url = "ytuuiuouo"
qrcode = RQRCode::QRCode.new(url, :size => size, :level => level)
svg = RQRCode::Renderers::SVG::render(qrcode, {})
p_w_picpath = MiniMagick::Image.read(svg) { |i| i.format "svg" }
p_w_picpath.format "png" if format == :png
path=p_w_picpath.path
转载于:https://blog.51cto.com/5941840/1363676