Skip to content

Instantly share code, notes, and snippets.

@sbounmy
Created December 19, 2023 10:15
Show Gist options
  • Save sbounmy/41987e269338be4fae93a65fbf14fba4 to your computer and use it in GitHub Desktop.
Save sbounmy/41987e269338be4fae93a65fbf14fba4 to your computer and use it in GitHub Desktop.
Generate a QRCode BIP 21 with Bitcoin logo at its center
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
require 'image_processing/vips'
require 'rqrcode'
qrcode_uri = "bitcoin:my-btc-address?amount=0.000015"
qrcode = RQRCode::QRCode.new(qrcode_uri)
options = { bit_depth: 1,
border_modules: 4,
color_mode: ChunkyPNG::COLOR_GRAYSCALE,
color: 'black',
file: 'qqrcode.png',
fill: 'white',
module_px_size: 6,
resize_exactly_to: false,
resize_gte_to: false,
size: 1200 }
dir = File.expand_path(File.dirname(__FILE__))
logo_path = File.join(dir, 'bitcoin.svg')
logo_image = ImageProcessing::Vips.source(logo_path).resize_to_fit(300, 300).call(save: false)
tmp = Tempfile.new(['qrcode_image', '.png'])
qrcode.as_png(options).save(tmp.path)
qr = ImageProcessing::Vips.source(tmp).call(save: false)
center_x = (qr.width - logo_image.width) / 2
center_y = (qr.height - logo_image.height) / 2
qrcode_image = ImageProcessing::Vips.source(qr).composite(logo_image, gravity: 'south-west', offset: [center_x, center_y]).call(save: false)
result_path = File.join(File.dirname(__FILE__), 'qrcode_with_logo.jpg')
qrcode_image.write_to_file(result_path)
puts "QR code with logo saved to #{result_path}"
tmp.close
tmp.unlink
@sbounmy
Copy link
Author

sbounmy commented Dec 19, 2023

result
CleanShot 2023-12-19 at 11 16 23

To test scan with a wallet (I tried with Binance)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment