一个用于编码 QR 码的 Ruby 库
RQRCode is a library for creating and rendering QR codes into various formats. It has a simple interface with all the standard QR code options. It was adapted from the Javascript library by Kazuhiko Arase.
>= 3.2.0Add this line to your application's Gemfile:
gem "rqrcode", "~> 3.0"
or install manually:
gem install rqrcode
require "rqrcode"
qr = RQRCode::QRCode.new("https://kyan.com")
puts qr.to_s
# to_s( dark: "x", light: " " ) # defaults
xxxxxxx xxxxxxx xxx xxxxxxx
x x x xxx xx x x
x xxx x xx x x xx x xxx x
x xxx x xx xx xx x xxx x
x xxx x x x xxx x xxx x
x x xxx x xx x x x x
...
Easy, but unlikely to be readable. For this you will need to use one of the many rendering options below.
These are the various QR code generation options provided by the underlying rqrcode_core. You may actually only need this library if you don't need the various rendering options rqrcode provides, but just need the data structure.
…
Example
simple_qrcode = RQRCodeCore::QRCode.new("https://kyan.com", size: 2, level: :m, mode: :byte_8bit)
segment_qrcode = RQRCodeCore::QRCode.new([{ data: "foo", mode: :byte_8bit }])
multi_qrcode = RQRCodeCore::QRCode.new([
{ data: 'foo', mode: :byte_8bit },
{ data: 'BAR1', mode: :alphanumeric }
])
You probably want to output your QR code in a specific format. We make this easy by providing a bunch of formats to choose from below, each with their own set of options:
as_svgThe SVG renderer will produce a stand-alone SVG as a String
…
Example
require "rqrcode"
qrcode = RQRCode::QRCode.new("http://github.com/")
# NOTE: showing with default options specified explicitly
svg = qrcode.as_svg(
color: "000",
shape_rendering: "crispEdges",
module_size: 11,
standalone: true,
use_path: true
)
as_pngThe will produce a PNG using the ChunkyPNG gem. The result will be a ChunkyPNG::Image instance.
…
Example
require "rqrcode"
qrcode = RQRCode::QRCode.new("http://github.com/")
# NOTE: showing with default options specified explicitly
png = qrcode.as_png(
bit_depth: 1,
border_modules: 4,
color_mode: ChunkyPNG::COLOR_GRAYSCALE,
color: "black",
file: nil,
fill: "white",
module_px_size: 6,
resize_exactly_to: false,
resize_gte_to: false,
size: 120
)
IO.binwrite("/tmp/github-qrcode.png", png.to_s)
as_ansiThe ANSI renderer will produce as a string with ANSI color codes.
Options:
light - Foreground ANSI code
(default "\033[47m")
dark - Background ANSI code
(default "\033[40m")
fill_character - The written character
(default ' ')
quiet_zone_size - Padding around the edge
(default 4)
Example
require "rqrcode"
qrcode = RQRCode::QRCode.new("http://github.com/")
# NOTE: showing with default options specified explicitly
svg = qrcode.as_ansi(
light: "\033[47m", dark: "\033[40m",
fill_character: " ",
quiet_zone_size: 4
)
http://www.rubydoc.info/gems/rqrcode
You can run the test suite using:
$ bundle install
$ rake # runs specs and standard:fix
$ rake spec # just runs the specs
or try the lib from the console with:
$ ./bin/console
The project uses standardrb and can be used with:
$ bundle install
$ rake standard # checks
$ rake standard:fix # fixes
RQRCode includes comprehensive performance benchmarks for tracking export format performance over time. See the benchmark README for details on running benchmarks, interpreting results, and current performance baselines.
I am not currently accepting any new renderers as the current as_png, as_svg and as_ansi work for most cases. If you need something different from what's available, the rqrcode_core gem gives you access to all the QR Code information you will need so makes it simple to generate your own.
The motivation for the above is because the rendering side of this gem takes up the most time. It seems that many people want a slightly different version of a QR Code so supporting all the variations would be hard. The easiest way is to empower people to create their own versions which they can manage and share. This is what rqrcode_core does.
Any contribution PR's will be greatly accepted. It's important that they are well tested and backwards compatible.
Thanks D.
Original RQRCode author: Duncan Robertson
A massive thanks to all the contributors of the library over the years. It wouldn't exist if it wasn't for you all.
Oh, and thanks to my bosses at https://kyan.com for giving me time to maintain this project.
MIT License (http://www.opensource.org/licenses/mit-license.html)
暂无开放 Issues,或尚未同步最近议题。