qrio 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,107 @@
1
+ require_relative '../lib/qrio'
2
+ require 'test/unit'
3
+
4
+ class TestQr < Test::Unit::TestCase
5
+ def setup
6
+ @qr = Qrio::Qr.new
7
+ end
8
+
9
+ def test_grouping
10
+ [
11
+ [26,4,81],
12
+ [27,4,81],
13
+ [28,4,81],
14
+ [29,4,81],
15
+ [30,3,81],
16
+ [31,4,81],
17
+ [32,4,81],
18
+ [33,4,81],
19
+ [34,3,80],
20
+ [35,3,81],
21
+ [36,3,81],
22
+ [37,3,81],
23
+ [38,4,81],
24
+ [39,4,82],
25
+ [40,4,80],
26
+ [42,3,81],
27
+ [43,3,80],
28
+ [44,4,81],
29
+ [45,4,81],
30
+ [46,3,81],
31
+ [47,3,81],
32
+ [48,4,81],
33
+ [49,4,80],
34
+ [50,3,81],
35
+ [51,4,81],
36
+ [54,4,81],
37
+ [55,4,81],
38
+ [56,4,81],
39
+ [57,3,81],
40
+ [58,4,81]
41
+ ].each do |(offset, origin, terminus)|
42
+ slice = Qrio::HorizontalMatch.build(offset, origin, terminus)
43
+ @qr.add_candidate(slice, :horizontal)
44
+ end
45
+
46
+ assert_equal 1, @qr.candidates[:horizontal].length
47
+ end
48
+
49
+ def test_rle
50
+ input = [1, 1, 1, 1, 0, 0, 1, 1, 1]
51
+ assert_equal [4, 2, 3], @qr.rle(input)
52
+
53
+ input.map!{|i| i == 1 }
54
+ assert_equal [4, 2, 3], @qr.rle(input), "RLE should work with booleans"
55
+ end
56
+
57
+ def test_finder_pattern_detection
58
+ assert_contains_finder_pattern(
59
+ "finder_pattern1.png",
60
+ [10, 27, 52, 19],
61
+ [27, 10, 18, 52]
62
+ )
63
+
64
+ assert_contains_finder_pattern(
65
+ "finder_pattern3.png",
66
+ [5, 21, 52, 16],
67
+ [22, 3, 17, 51]
68
+ )
69
+
70
+ assert_contains_finder_pattern(
71
+ "finder_pattern4.png",
72
+ [3, 26, 79, 34],
73
+ [27, 3, 31, 80]
74
+ )
75
+ end
76
+
77
+ private
78
+
79
+ def assert_contains_finder_pattern(img, h, v)
80
+ @qr.load_image fixture_img_path(img)
81
+ @qr.scan(:horizontal)
82
+ @qr.scan(:vertical)
83
+ @qr.filter_candidates
84
+
85
+ assert_equal 1, @qr.matches[:horizontal].length
86
+ hmatch = @qr.matches[:horizontal].first
87
+ assert_dimensions(hmatch, *h)
88
+
89
+ assert_equal 1, @qr.matches[:vertical].length
90
+ vmatch = @qr.matches[:vertical].first
91
+ assert_dimensions(vmatch, *v)
92
+
93
+ @qr.find_intersections
94
+ assert_equal 1, @qr.finder_patterns.length
95
+ end
96
+
97
+ def assert_dimensions(region, left, top, width, height)
98
+ assert_equal left, region.left, "L #{ region } != #{ left }"
99
+ assert_equal top, region.top, "T #{ region } != #{ top }"
100
+ assert_equal width, region.width, "W #{ region } != #{ width }"
101
+ assert_equal height, region.height, "H #{ region } != #{ height }"
102
+ end
103
+
104
+ def fixture_img_path(filename)
105
+ File.expand_path("../fixtures/#{ filename }", __FILE__)
106
+ end
107
+ end
@@ -0,0 +1,87 @@
1
+ require_relative '../lib/qrio'
2
+ require 'test/unit'
3
+
4
+ class TestRegion < Test::Unit::TestCase
5
+ def test_region_basics
6
+ region = Qrio::Region.new(1, 1, 4, 4)
7
+
8
+ assert_equal [1, 1], region.top_left
9
+ assert_equal [4, 4], region.bottom_right
10
+
11
+ assert_equal 4, region.width
12
+ assert_equal 4, region.height
13
+ end
14
+
15
+ def test_orientation_detection
16
+ region = Qrio::Region.new(0, 2, 8, 2)
17
+ assert region.horizontal?
18
+ assert ! region.vertical?
19
+
20
+ region = Qrio::Region.new(2, 0, 2, 8)
21
+ assert ! region.horizontal?
22
+ assert region.vertical?
23
+
24
+ region = Qrio::Region.new(0, 0, 8, 8)
25
+ assert ! region.horizontal?
26
+ assert ! region.vertical?
27
+ end
28
+
29
+ def test_union
30
+ slice1 = Qrio::Region.new(0, 2, 8, 2)
31
+ slice2 = Qrio::Region.new(0, 3, 8, 3)
32
+
33
+ slice2 = slice1.union slice2
34
+
35
+ assert_equal 2, slice2.height
36
+ assert_equal 0, slice2.left
37
+ assert_equal 8, slice2.right
38
+ assert_equal 2, slice2.top
39
+ assert_equal 3, slice2.bottom
40
+
41
+ slice3 = slice2.union Qrio::Region.new(0, 4, 8, 4)
42
+
43
+ assert_equal 3, slice3.height
44
+ assert_equal 0, slice3.left
45
+ assert_equal 8, slice3.right
46
+ assert_equal 2, slice3.top
47
+ assert_equal 4, slice3.bottom
48
+
49
+ slice4 = slice3.union Qrio::Region.new(0, 6, 8, 6)
50
+
51
+ assert_equal 5, slice4.height
52
+ assert_equal 0, slice4.left
53
+ assert_equal 8, slice4.right
54
+ assert_equal 2, slice4.top
55
+ assert_equal 6, slice4.bottom
56
+ end
57
+
58
+ def test_equality_detection
59
+ slice1 = Qrio::Region.new(0, 2, 8, 2)
60
+ slice2 = Qrio::Region.new(0, 2, 8, 2)
61
+
62
+ assert_equal slice1, slice2
63
+ assert slice1.eql?(slice2)
64
+ end
65
+
66
+ def test_translation
67
+ region = Qrio::Region.new(7, 7, 10, 10)
68
+ translated = region.translate(7, 7)
69
+ assert_equal "R[0,0,3,3]", translated.to_s
70
+ end
71
+
72
+ def test_rotation
73
+ region = Qrio::Region.new(0,0,6,6)
74
+
75
+ rotated = region.rotate(21, 21)
76
+ assert_equal "R[14,0,20,6]", rotated.to_s
77
+
78
+ rotated = rotated.rotate(21, 21)
79
+ assert_equal "R[14,14,20,20]", rotated.to_s
80
+
81
+ rotated = rotated.rotate(21, 21)
82
+ assert_equal "R[0,14,6,20]", rotated.to_s
83
+
84
+ rotated = rotated.rotate(21, 21)
85
+ assert_equal "R[0,0,6,6]", rotated.to_s
86
+ end
87
+ end
@@ -0,0 +1,78 @@
1
+ require_relative '../lib/qrio'
2
+ require 'test/unit'
3
+
4
+ class TestSamplingGrid < Test::Unit::TestCase
5
+ def setup
6
+ @matrix = Qrio::Matrix.new(Array.new(441, false), 21, 21)
7
+ end
8
+
9
+ def test_orientation_detection
10
+ finder_patterns = build_finder_patterns([
11
+ [ 0, 0, 6, 6],
12
+ [14, 0,20, 6],
13
+ [ 0,14, 6,20]
14
+ ])
15
+
16
+ grid = Qrio::SamplingGrid.new(@matrix, finder_patterns)
17
+ assert_equal "F[0,0,6,6]", grid.origin_corner.to_s
18
+ assert_equal 0, grid.orientation
19
+ assert_equal 1, grid.provisional_version
20
+
21
+ finder_patterns = build_finder_patterns([
22
+ [ 0, 0, 6, 6],
23
+ [14, 0,20, 6],
24
+ [14,14,20,20]
25
+ ])
26
+
27
+ grid = Qrio::SamplingGrid.new(@matrix, finder_patterns)
28
+ assert_equal "F[14,0,20,6]", grid.origin_corner.to_s
29
+ assert_equal 1, grid.orientation
30
+ assert_equal 1, grid.provisional_version
31
+
32
+ finder_patterns = build_finder_patterns([
33
+ [14, 0,20, 6],
34
+ [14,14,20,20],
35
+ [ 0,14, 6,20]
36
+ ])
37
+
38
+ grid = Qrio::SamplingGrid.new(@matrix, finder_patterns)
39
+ assert_equal "F[14,14,20,20]", grid.origin_corner.to_s
40
+ assert_equal 2, grid.orientation
41
+ assert_equal 1, grid.provisional_version
42
+
43
+ finder_patterns = build_finder_patterns([
44
+ [14,14,20,20],
45
+ [ 0,14, 6,20],
46
+ [ 0, 0, 6, 6],
47
+ ])
48
+
49
+ grid = Qrio::SamplingGrid.new(@matrix, finder_patterns)
50
+ assert_equal "F[0,14,6,20]", grid.origin_corner.to_s
51
+ assert_equal 3, grid.orientation
52
+ assert_equal 1, grid.provisional_version
53
+ end
54
+
55
+ def test_slight_rotation
56
+ finder_patterns = build_finder_patterns([
57
+ [105,217,155,266],
58
+ [290,216,341,266],
59
+ [100,401,151,452]
60
+ ])
61
+
62
+ grid = Qrio::SamplingGrid.new(@matrix, finder_patterns)
63
+ assert_equal "F[105,217,155,266]", grid.origin_corner.to_s
64
+ assert_equal 0, grid.orientation
65
+ assert_equal 3, grid.provisional_version
66
+ assert_equal '7.38', ('%.2f' % grid.block_width)
67
+ assert_equal '7.29', ('%.2f' % grid.block_height)
68
+ end
69
+
70
+
71
+ private
72
+
73
+ def build_finder_patterns(arrays)
74
+ arrays.map do |coordinates|
75
+ Qrio::FinderPatternSlice.new(*coordinates)
76
+ end
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qrio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Solomon White
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chunky_png
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: QR code decoder in pure Ruby
28
+ email:
29
+ - rubysolo@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gemtest
35
+ - .gitignore
36
+ - .rvmrc.example
37
+ - Gemfile
38
+ - README.md
39
+ - Rakefile
40
+ - examples/extract_qr
41
+ - lib/qrio.rb
42
+ - lib/qrio/finder_pattern_slice.rb
43
+ - lib/qrio/horizontal_match.rb
44
+ - lib/qrio/image_dumper.rb
45
+ - lib/qrio/image_loader/png_image_loader.rb
46
+ - lib/qrio/matrix.rb
47
+ - lib/qrio/neighbor.rb
48
+ - lib/qrio/qr.rb
49
+ - lib/qrio/qr_matrix.rb
50
+ - lib/qrio/region.rb
51
+ - lib/qrio/sampling_grid.rb
52
+ - lib/qrio/version.rb
53
+ - lib/qrio/vertical_match.rb
54
+ - qrio.gemspec
55
+ - test/finder_pattern_slice_test.rb
56
+ - test/fixtures/block_test.qr
57
+ - test/fixtures/finder_pattern1.png
58
+ - test/fixtures/finder_pattern2.png
59
+ - test/fixtures/finder_pattern3.png
60
+ - test/fixtures/finder_pattern4.png
61
+ - test/fixtures/masked0.qr
62
+ - test/fixtures/no_finder_pattern1.png
63
+ - test/fixtures/qrio-codewords.txt
64
+ - test/fixtures/qrio.qr
65
+ - test/horizontal_match_test.rb
66
+ - test/matrix_test.rb
67
+ - test/qr_matrix_test.rb
68
+ - test/qr_test.rb
69
+ - test/region_test.rb
70
+ - test/sampling_grid_test.rb
71
+ homepage: http://github.com/rubysolo/qrio
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.0.3
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: QR code decoder
95
+ test_files:
96
+ - test/finder_pattern_slice_test.rb
97
+ - test/fixtures/block_test.qr
98
+ - test/fixtures/finder_pattern1.png
99
+ - test/fixtures/finder_pattern2.png
100
+ - test/fixtures/finder_pattern3.png
101
+ - test/fixtures/finder_pattern4.png
102
+ - test/fixtures/masked0.qr
103
+ - test/fixtures/no_finder_pattern1.png
104
+ - test/fixtures/qrio-codewords.txt
105
+ - test/fixtures/qrio.qr
106
+ - test/horizontal_match_test.rb
107
+ - test/matrix_test.rb
108
+ - test/qr_matrix_test.rb
109
+ - test/qr_test.rb
110
+ - test/region_test.rb
111
+ - test/sampling_grid_test.rb