Skip to content

Commit

Permalink
Merge pull request #8 from chifung7/fixed_aspect_ratio
Browse files Browse the repository at this point in the history
Fixed aspect ratio
  • Loading branch information
jamesprior authored Jul 30, 2017
2 parents b9ed667 + 8550fd3 commit 8572fc8
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 12 deletions.
49 changes: 42 additions & 7 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,52 @@ There are a few ways to use the library, at its simplest:

If you want to get the raw barcode data from the PDF417 library:

barcode = PDF417.new("readable barcode data")
barcode.to_blob # oops, wrong text
barcode.text = "ACTUAL barcode data"
barcode.to_blob
barcode = PDF417.new("readable barcode data")
barcode.to_blob # oops, wrong text
barcode.text = "ACTUAL barcode data"
barcode.to_blob

If you want to get it as an array of strings (each array element representing a line)

barcode.encoding

If you want to use fixed rows or cols

barcode = PDF417.new(text: "barcode data" * 20, cols: 3) # fixed cols
barcode.to_blob
[barcode.cols, barcode.rows] # => [3, 42]
barcode = PDF417.new(text: "barcode data" * 20, rows: 2) # fixed rows
barcode.to_blob
[barcode.cols, barcode.rows] # => [30, 5]

If you want to use aspect ratio to determine rows and cols

barcode = PDF417.new(text: "barcode data" * 20, aspect_ratio: 0.618)
barcode.to_blob
[barcode.cols, barcode.rows] # => [4, 31]

If you want to change error level (maximum 8)

barcode = PDF417.new(text: "barcode data" * 20, error_level: 6)
barcode.to_blob
[barcode.cols, barcode.rows] # => [8, 32]

If you have chunky_png installed and you'd rather have a PNG
barcode.to_png


barcode.to_png # 1x1 scale
barcode.to_png(margin: 0, x_scale: 1, y_scale: 3) # stretch 3 times in the vertical direction

If you want to know the barcode image size in pixel

barcode = PDF417.new(text: "barcode data" * 20, aspect_ratio: 0.618, error_level: 6)
cimg = barcode.to_chunky_png(margin: 0, x_scale: 2, y_scale: 6)
[cimg.width, cimg.height] # => [376, 216]

If you want to use in Prawn

pdf.image(StringIO.new(barcode.to_png(margin: 0, x_scale: 2, y_scale: 6)), at: pos)
pdf.image(StringIO.new(barcode.to_chunky_png(margin: 0, x_scale: 2, y_scale: 6).to_blob), at: pos)

See the RDocs for more information.

== Note on Patches/Pull Requests
Expand Down
6 changes: 4 additions & 2 deletions lib/pdf417.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def initialize(*attrs)
self.text = ""
@y_height = 3
@aspect_ratio = 0.5
@rows = 1
@cols = 0
@rows = nil
@cols = nil

if attrs.first.is_a?(String)
self.text = attrs.first
Expand Down Expand Up @@ -72,6 +72,8 @@ def aspect_ratio
end
def aspect_ratio=(val)
@blob = nil
@rows = nil
@cols = nil
@aspect_ratio = val
end

Expand Down
64 changes: 61 additions & 3 deletions test/pdf417_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,60 @@ class Pdf417Test < Minitest::Test
b = PDF417.new("fred")
assert_equal "fred", b.text
end


should "defaults rows and cols to nil" do
b = PDF417.new("fred")
assert_nil b.rows
assert_nil b.cols
end

should "allows fixed cols" do
b = PDF417.new(text: "01234"*60, error_level: 6, cols: 5)
b.to_blob
assert_equal [5, 47], [b.cols, b.rows]

# if it's too small, auto determine the best value
b = PDF417.new(text: "01234"*60, error_level: 6, cols: 1)
b.to_blob
assert_equal [3, 90], [b.cols, b.rows]

# if it's too big, auto determine the best value
b = PDF417.new(text: "01234"*60, error_level: 6, cols: 100)
b.to_blob
assert_equal [30, 8], [b.cols, b.rows]
end

should "allows fixed rows" do
b = PDF417.new(text: "01234"*60, error_level: 6, rows: 10)
b.to_blob
assert_equal [24, 10], [b.cols, b.rows]

# if it's too small, auto determine the best value
b = PDF417.new(text: "01234"*60, error_level: 6, rows: 6)
b.to_blob
assert_equal [30, 8], [b.cols, b.rows]

# if it's too big, auto determine the best value
b = PDF417.new(text: "01234"*60, error_level: 6, rows: 100)
b.to_blob
assert_equal [3, 90], [b.cols, b.rows]
end

should "let default aspect_ratio to define rows and cols" do
b = PDF417.new(text: "01234"*60, error_level: 6)
b.to_blob
assert_equal 7, b.cols
assert_equal 34, b.rows
end

should "let custom aspect_ratio to define rows and cols" do
b = PDF417.new(text: "01234"*60, aspect_ratio: 0.618,
error_level: 6)
b.to_blob
assert_equal 6, b.cols
assert_equal 39, b.rows
end

should "know the right codewords for fred" do
assert_equal [4, 815, 514, 119], PDF417.encode_text("fred")
assert_equal [4, 815, 514, 119], PDF417.new(:text => "fred").codewords
Expand Down Expand Up @@ -143,11 +196,16 @@ class Pdf417Test < Minitest::Test
# reference and make sure there is enough data to have rows and cols
@barcode.text = "SOME REALLY LONG TEXT HERE! Gonna need some rows...." * 10
@barcode.rows = nil
@barcode.cols = 2
@barcode.cols = 10 # fixed cols input
@barcode.error_level = 3
@blob = @barcode.to_blob
assert_equal [10, 30], [@barcode.cols, @barcode.rows] # cols is fixed at 10

@barcode.aspect_ratio = 1000
refute_equal @blob, @barcode.to_blob
new_blob = @barcode.to_blob
assert_equal [4, 90], [@barcode.cols, @barcode.rows] # new cols and rows are defined

refute_equal @blob, new_blob
assert_barcode_start_sequence @barcode
assert_barcode_end_sequence @barcode
end
Expand Down

0 comments on commit 8572fc8

Please sign in to comment.