How to generate a QR code as a PNG file in Ruby

Let’s look at how to use a Cloudmersive API to save time in generating QR codes. First up, add this snippet to your Gemfile to install the API client.

gem 'cloudmersive-barcode-api-client', '~> 1.2.9'

Then run the generate_barcode_qr_code function, as you see here.

# load the gem
require 'cloudmersive-barcode-api-client'
# setup authorization
CloudmersiveBarcodeApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
end
api_instance = CloudmersiveBarcodeApiClient::GenerateBarcodeApi.newvalue = "value_example" # String | QR code text to convert into the QR code barcodebegin
#Generate a QR code barcode as a PNG file, a type of 2D barcode which can encode free-form text information
result = api_instance.generate_barcode_qr_code(value)
p result
rescue CloudmersiveBarcodeApiClient::ApiError => e
puts "Exception when calling GenerateBarcodeApi->generate_barcode_qr_code: #{e}"
end

Annnnd we’re done. Your output will be a PNG of your QR code. If you work with other types of barcodes, we have separate functions in this API that generate the most popular formats.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store