Creating QR Code in Ruby

in #utopian-io7 years ago (edited)

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.

  • csv comes 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.
image.png

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

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

Violated Rule:

  • Design or video editing related tutorials, gameplay, simple on-screen instructions, ubiquitous functions (Save, Open, Print, etc.) or basic programming concepts (variables, operators, loops, etc.) will not be accepted.

My Opinion:

  • It mainly shows ubiquitous functions and simple on-screen instructions.

  • A tutorial must be informative and explanatory, but also "tutor". This tutorial lacks "tutor"ing, and it is nothing more than an explanation of a documentation.

  • We prefer tutorials as a whole as possible instead of dozens of parts. So, next time, please merge some topics together.

You can contact us on Discord.

[utopian-moderator]

Congratulations @aadeshere1! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @aadeshere1! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Congratulations @aadeshere1! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You got more than 10 replies. Your next target is to reach 50 replies.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @hivebuzz:

Feedback from the January 1st Hive Power Up Day