Class: QRTools::QRCode
- Inherits:
-
Object
- Object
- QRTools::QRCode
- Defined in:
- lib/qrtools/qrcode.rb,
ext/qrtools/qrtools_qrcode.c
Class Method Summary collapse
Instance Method Summary collapse
- #data ⇒ Object
- #to_matrix ⇒ Object
- #to_png(scale = 4, border = 10) ⇒ Object
- #to_s ⇒ Object
- #version ⇒ Object
- #width ⇒ Object
Class Method Details
Instance Method Details
#data ⇒ Object
17 18 19 20 21 22 23 |
# File 'ext/qrtools/qrtools_qrcode.c', line 17
static VALUE data(VALUE self)
{
QRcode * code;
Data_Get_Struct(self, QRcode, code);
return rb_str_new(code->data, code->width * code->width);
}
|
#to_matrix ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/qrtools/qrcode.rb', line 16 def to_matrix codes = [] data.unpack("C#{width ** 2}").each_slice(width) { |row| codes << row.map { |x| x & 0x1 == 1 } } codes end |
#to_png(scale = 4, border = 10) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/qrtools/qrcode.rb', line 30 def to_png scale = 4, border = 10 # Add 2 extra rows / columns for white borders size = (width + border) * scale canvas = PNG::Canvas.new(size, size, PNG::Color::White) scaled_matrix = [] to_matrix.each do |row| scaled_row = row.map { |bit| [bit] * scale }.flatten scaled_matrix += ([scaled_row] * scale) end factor = border / 2 * scale scaled_matrix.each_with_index { |row, i| x = i + factor row.each_with_index { |bit, j| y = j + factor canvas[x, y] = bit ? PNG::Color::Black : PNG::Color::White } } PNG.new(canvas).to_blob end |
#to_s ⇒ Object
24 25 26 27 28 |
# File 'lib/qrtools/qrcode.rb', line 24 def to_s to_matrix.map { |row| row.map { |bit| bit ? '#' : ' ' }.join }.join("\n") end |
#version ⇒ Object
10 11 12 13 14 15 |
# File 'ext/qrtools/qrtools_qrcode.c', line 10
static VALUE version(VALUE self)
{
QRcode * code;
Data_Get_Struct(self, QRcode, code);
return INT2NUM(code->version);
}
|
#width ⇒ Object
3 4 5 6 7 8 |
# File 'ext/qrtools/qrtools_qrcode.c', line 3
static VALUE width(VALUE self)
{
QRcode * code;
Data_Get_Struct(self, QRcode, code);
return INT2NUM(code->width);
}
|