Creating QR Code in Ruby
What Will I Learn?
In this lesson, we will learn how to generate a QR code with information encoded into the QR. We will use CSV file to read user data and create a corresponding QR code for all the dataset in CSV.
- We will learn to read CSV file
- We will learn to generate QR code
Requirements
We need the following gems in order to complete this tutorial.
csvcomes pre-installed with ruby.- rQrcode, for generating QR code
Difficulty
- Intermediate
Tutorial Contents
QR code stands for Quick Response code, which is a machine-readable label generated to store information. QR code first became popular in the automobile industry and later adapted in other sectors due to its faster readability and greater storage capability compared to the standard barcode.
To generate the QR code, we will use data stored in CSV file. CSV is a comma-separated values file which allows data to be saved in a table structured format. CSV, please refer to this link to read more about CSV.
Our project structure will look like the image.
In the Gemfile, we will describe the external gems we will use in this.
Gemfile
source 'https://rubygems.org'
gem 'rqrcode'
Run bundle install.
For reference CSV, we’ll be using this CSV structure.
rqrcode.rb
require 'rqrcode'
require 'csv'
CSV.foreach('data.csv') do |row|
qr = RQRCode::QRCode.new("Id:#{row[0]}\nCompany: #{row[1]}\nAddress: #{row[2]}\nName: #{row[3]}\nDesignation: #{row[4]}\nMobile no: #{row[5]}\nPhone no: #{row[6]}\nEmail: #{row[7]}\nWebsite: #{row[9]}\n\n\n\n")
puts "Writing data..."
png = qr.as_png(resize_gte_to: false, resize_exactly_to: false, fill: 'white', color: 'black', size: 120, border_modules: 4, module_px_size: 6, file: nil)
puts "Saving as #{row[0]}_#{row[3].split(" ").join("-") unless row[3].nil?}.png"
IO.write("#{row[0]}_#{row[3].split(" ").join("-") unless row[3].nil?}.png", png.to_s)
end
First, we are requiring csv and rqrcode gems, then we open the CSV data file and read each line with CSV.foreach.
For every row in the file, we encode data to QR code with RQRCode::QRCode.new. We then generate QR code as png after encoding the data and finally, we save the file.
rQrcode can generate QR code in different file formats such as png, svg, html or even ansi and string.
Use .as_svg(offset: 0, color: '000', shape_rendering: 'crispEdges', module_size: 11) to generate qr as svg.
To try the qr code in console, we can try the following code
qr = RQRCode::QRCode.new( 'my string to generate', :size => 4, :level => :h )
puts qr.to_s
Curriculum
This is the first lesson of series for working with QR code. In our next lesson, we will be creating QR code with the data we get from the form.
Posted on Utopian.io - Rewarding Open Source Contributors
Leave Creating QR Code in Ruby to:
Read more #utopian-io posts
Best Posts From aadeshere1
We have not curated any of aadeshere1's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From aadeshere1
- Creating QR Code in Ruby
- Baby, can you get me beer?
- Half plate Momo
- Chill it'll be fine, AGAIN
- Building a Static Site with Jekyll
- Install Jekyll on linux
- Looking for suggestions on Ruby topic
- Popular Jhol Momo from Nepal
- One liner server command in ruby
- [d-tube]How to install ubuntu
- Introduction to Ruby on Rails
- Install Ruby on Linux
- Dtube - Resume Design
- Tour to Gosaikunda