欢迎来到cool的博客
7

Music box

Click to Start

点击头像播放音乐
新博客链接

ruby 使用gem rqrcode 把微信的code_url变成微信可以扫码付款的二维码

example: http://whomwah.github.io/rqrcode/

github官方:https://github.com/whomwah/rqrcode

rQRCode

A Ruby library for encoding QR Codes

rQRCode is a library for encoding QRCodes in Ruby. It has a simple interface with all the standard qrcode options. It was adapted from the Javascript library by Kazuhiko Arase.

sudo gem install rqrcode

The documentation is available over on the RubyGems website.

A quick simple example in the console:

=> require 'rubygems'
=> require 'rqrcode'
=> qr = RQRCode::QRCode.new('hello world')
>> #<RQRCode::QRCode:0x32a9 ......
=> puts qr.to_s
>>
xxxxxxx    xxxx   xxxxxxx
x     x x   xxx   x     x
x xxx x x  xx xx  x xxx x
x xxx x xx x  x   x xxx x
.....

Also, here is a quick example of using it in a Ruby on Rails project:

# Controller
require 'rqrcode'
 
def qrcode
 @qr = RQRCode::QRCode.new('hello world')
end
 
# 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>

返回列表