Skip to content

Instantly share code, notes, and snippets.

@charlescui
Created October 7, 2013 03:02
Show Gist options
  • Save charlescui/6861984 to your computer and use it in GitHub Desktop.
Save charlescui/6861984 to your computer and use it in GitHub Desktop.
This is a generate qrcode tool, resolve between pc and smart phone or pad translate tiny information.
#! /usr/bin/env ruby
require "rqrcode"
require "uuid"
require "erb"
@qr = RQRCode::QRCode.new(ARGV[0])
file = File.expand_path($0)
begin
io = ::IO.respond_to?(:binread) ? ::IO.binread(file) : ::IO.read(file)
app, data = io.gsub("\r\n", "\n").split(/^__END__$/, 2)
rescue Errno::ENOENT
app, data = nil
end
if !data
puts "No template found."
exit -1
end
TEMPLATE = ERB.new data
File.open("/tmp/qrcode-#{UUID.generate}.html", 'w+') do |output|
content = TEMPLATE.result(binding)
output.write content
puts "Output path:\n#{output.path}"
system("open -a safari #{output.path}")
end
__END__
<style type="text/css">
table {
border-width: 0;
border-style: none;
border-color: #0000ff;
border-collapse: collapse;
}
td {
border-width: 0;
border-style: none;
border-color: #0000ff;
border-collapse: collapse;
padding: 0;
margin: 0;
width: 10px;
height: 10px;
}
td.black { background-color: #000; }
td.white { background-color: #fff; }
</style>
<table>
<% @qr.modules.each_index do |x|%>
<tr>
<% @qr.modules.each_index do |y|%>
<% if @qr.dark?(x,y)%>
<td class="black"/>
<% else%>
<td class="white"/>
<% end%>
<% end%>
</tr>
<% end%>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment