Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

rqrcode

> 编程语言
Open source

A Ruby library that encodes QR Codes

2.0K stars0 likes1 views
WebsiteGitHub

About

A Ruby library that encodes QR Codes

RQRCode

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.

  • QR code is trademarked by Denso Wave inc
  • Minimum Ruby version is >= 3.2.0

Installing

Add this line to your application's Gemfile:

ruby
gem "rqrcode", "~> 3.0"

or install manually:

ruby
gem install rqrcode

Basic usage example

ruby
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.

Advanced Options

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

ruby
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 }
])

Render types

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_svg

The SVG renderer will produce a stand-alone SVG as a String

…

Example

ruby
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_png

The will produce a PNG using the ChunkyPNG gem. The result will be a ChunkyPNG::Image instance.

…

Example

ruby
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_ansi

The 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

ruby
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
)

API Documentation

http://www.rubydoc.info/gems/rqrcode

Tests

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

Linting

The project uses standardrb and can be used with:

$ bundle install
$ rake standard # checks
$ rake standard:fix # fixes

Benchmarks

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.

Contributing

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.

  • Fork the project
  • Send a pull request
  • Don't touch the .gemspec, I'll do that when I release a new version

Thanks D.

Authors

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.

Resources

  • wikipedia:: http://en.wikipedia.org/wiki/QR_Code
  • Denso-Wave website:: http://www.denso-wave.com/qrcode/index-e.html
  • kaywa:: http://qrcode.kaywa.com

Copyright

MIT License (http://www.opensource.org/licenses/mit-license.html)

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyqrcodeqrcode-generatorruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言