Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Backup file into QR code
#!/usr/bin/env ruby
require 'chunky_png'
require 'rqrcode_png'
CHUNK_SIZE=4096/6 # 5462 bits, QRCode 29 = 5608 bits
IMG_SIZE = 800
IMG_PER_LINE = 2
def chunk(string, size)
string.scan /.{1,#{size}}/om
end
class QRCodeImage
def initialize(qr)
@qr = qr
end
def each_dark_pixel(&block)
@qr.modules.each_index do |row|
@qr.modules.each_index do |column|
if @qr.dark? row, column
yield column, row
end
end
end
end
def render(size, color = ::ChunkyPNG::Color::BLACK)
qr_size = @qr.module_count
qr = ::ChunkyPNG::Image.new qr_size, qr_size, ::ChunkyPNG::Color::WHITE
self.each_dark_pixel do |row, column|
qr[row, column] = color
end
border = (4*size/(qr_size+8).to_f).ceil # QRCode expect a 4 modules wide quiet zone
qr_size = size-2*border
qr = qr.resize qr_size, qr_size
img = ::ChunkyPNG::Image.new size, size, ::ChunkyPNG::Color::WHITE
img.replace qr, border, border
end
end
file = ARGV[0]
data = File.read file
chunks = chunk data, CHUNK_SIZE
cols = chunks.length < IMG_PER_LINE ? chunks.length : IMG_PER_LINE
rows = (chunks.length / IMG_PER_LINE.to_f).ceil
png = ChunkyPNG::Image.new cols*IMG_SIZE, rows*IMG_SIZE, ChunkyPNG::Color::WHITE
n = 0
chunks.each do |chunk|
qr = RQRCode::QRCode.new chunk, size: 29, level: :h
img = QRCodeImage.new(qr).render IMG_SIZE
offset = n.divmod cols
png.replace! img, IMG_SIZE*offset[1], IMG_SIZE*offset[0]
n += 1
end
png.save "#{file}.png"
#!/usr/bin/env ruby
require 'qrio'
ARGV.each do |file|
qr = Qrio::Qr.load file
p qr.text
qr.save_image(
"#{file}-annotated",
:crop => true,
:annotate => [
:finder_patterns,
:angles
]
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment