Creating QR Code in Ruby

in #utopian-io7 years ago (edited)

What Will I Learn?

<p dir="auto">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. <ul> <li>We will learn to read CSV file <li>We will learn to generate QR code <h4>Requirements <p dir="auto">We need the following gems in order to complete this tutorial. <ul> <li><code>csv comes pre-installed with ruby. <li><a href="https://github.com/whomwah/rqrcode" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">rQrcode, for generating QR code <h4>Difficulty <ul> <li>Intermediate <h4>Tutorial Contents <p dir="auto">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. <p dir="auto">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. <a href="https://en.wikipedia.org/wiki/Comma-separated_values" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">CSV, please refer to this link to read more about CSV. <p dir="auto">Our project structure will look like the image.<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519616322/xwujbcg1jla4jyaesojb.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519616322/xwujbcg1jla4jyaesojb.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519616322/xwujbcg1jla4jyaesojb.png 2x" /> <p dir="auto">In the Gemfile, we will describe the external gems we will use in this. <p dir="auto">Gemfile <pre><code>source 'https://rubygems.org' gem 'rqrcode' <p dir="auto">Run <code>bundle install.<br /> For reference CSV, we’ll be using this <a href="https://github.com/aadeshere1/csv_qrcode/blob/master/data.csv" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">CSV structure. <p dir="auto">rqrcode.rb <pre><code>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 <p dir="auto">First, we are requiring <code>csv and <code>rqrcode gems, then we open the CSV data file and read each line with <code>CSV.foreach.<br /> For every row in the file, we encode data to QR code with <code>RQRCode::QRCode.new. We then generate QR code as png after encoding the data and finally, we save the file. <p dir="auto">rQrcode can generate QR code in different file formats such as png, svg, html or even ansi and string. <p dir="auto">Use <code>.as_svg(offset: 0, color: '000', shape_rendering: 'crispEdges', module_size: 11) to generate qr as svg. <p dir="auto">To try the qr code in console, we can try the following code <pre><code>qr = RQRCode::QRCode.new( 'my string to generate', :size => 4, :level => :h ) puts qr.to_s <h4>Curriculum <p dir="auto">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. <p dir="auto"><br /><hr /><em>Posted on <a href="https://utopian.io/utopian-io/@aadeshere1/creating-qr-code-in-ruby" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Utopian.io - Rewarding Open Source Contributors<hr /><p>
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) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@aadeshere1/replies.png?202101081530" /><td>You got more than 10 replies. Your next target is to reach 50 replies. <p dir="auto"><sub><em>You can view your badges on <a href="https://hivebuzz.me/@aadeshere1" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">your board and compare yourself to others in the <a href="https://hivebuzz.me/ranking" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Ranking<br /> <sub><em>If you no longer want to receive notifications, reply to this comment with the word <code>STOP <p dir="auto"><strong><span>Do not miss the last post from <a href="/@hivebuzz">@hivebuzz: <table><tr><td><a href="/hivebuzz/@hivebuzz/pud-202101-feedback"><img src="https://images.hive.blog/64x128/https://i.imgur.com/xQGM37X.png" /><td><a href="/hivebuzz/@hivebuzz/pud-202101-feedback">Feedback from the January 1st Hive Power Up Day