zbar 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Will Glynn
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,59 @@
1
+ = zbar
2
+ by Will Glynn
3
+
4
+ http://github.com/delta407/ruby-zbar
5
+
6
+ == DESCRIPTION:
7
+
8
+ Ruby bindings for ZBar, a barcode recognition library.
9
+
10
+ == SYNOPSIS:
11
+
12
+ require 'zbar'
13
+
14
+ ZBar::Image.from_jpeg(File.read('test.jpg')).process
15
+ => [#<Zbar::Symbol:0x10147c668
16
+ @addon="",
17
+ @data="9876543210128",
18
+ @location= [...],
19
+ @quality=15,
20
+ @symbology="EAN-13">]
21
+
22
+ == REQUIREMENTS:
23
+
24
+ * FFI
25
+ * ZBar, probably 0.10 or more
26
+
27
+ ZBar can be obtained from http://zbar.sourceforge.net.
28
+
29
+ == DOWNLOAD/INSTALL:
30
+
31
+ From gemcutter:
32
+
33
+ [sudo] gem install zbar
34
+
35
+ From github:
36
+
37
+ git clone git://github.com/delta407/ruby-zbar.git
38
+ cd ruby-zbar
39
+ rake install
40
+
41
+ == LIMITATIONS:
42
+
43
+ Doesn't expose all ZBar functionality, including:
44
+ * No video functions
45
+ * No low-level interfaces (scanner, decoder)
46
+
47
+ == Note on Patches/Pull Requests
48
+
49
+ * Fork the project.
50
+ * Make your feature addition or bug fix.
51
+ * Add tests for it. This is important so I don't break it in a
52
+ future version unintentionally.
53
+ * Commit, do not mess with rakefile, version, or history.
54
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
55
+ * Send me a pull request. Bonus points for topic branches.
56
+
57
+ == Copyright
58
+
59
+ Copyright (c) 2010 Will Glynn. See LICENSE for details.
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "zbar"
8
+ gem.summary = "Ruby bindings for ZBar, a barcode recognition library"
9
+ gem.description ="Ruby bindings for ZBar, a barcode recognition library. Uses FFI to interact with the underlying C library, but has no other dependencies."
10
+ gem.email = "will@willglynn.com"
11
+ gem.homepage = "http://github.com/delta407/ruby-zbar"
12
+ gem.authors = ["Will Glynn"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ gem.add_dependency "ffi", ">= 0"
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "zbar #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,6 @@
1
+ require 'ffi'
2
+
3
+ require 'zbar/lib'
4
+ require 'zbar/symbol'
5
+ require 'zbar/processor'
6
+ require 'zbar/image'
@@ -0,0 +1,102 @@
1
+ module ZBar
2
+
3
+ # Encapsulates a ZBar Image data structure.
4
+ class Image
5
+ # Instantiates a new Image object, either by creating an empty one,
6
+ # or wrapping the supplied pointer.
7
+ def initialize(pointer=nil)
8
+ @img = FFI::AutoPointer.new(
9
+ pointer || ZBar.zbar_image_create,
10
+ ZBar.method(:zbar_image_destroy)
11
+ )
12
+ end
13
+
14
+ # Instantiates an Image given JPEG data.
15
+ #
16
+ # This function uses the internal ZBar conversion function to decode the JPEG
17
+ # and convert it into a greyscale image suitable for further processing.
18
+ # This conversion may fail if ZBar was not built with <tt>--with-jpeg</tt>.
19
+ def self.from_jpeg(io_or_string)
20
+ if io_or_string.respond_to?(:read)
21
+ io_or_string = io_or_string.read
22
+ end
23
+
24
+ jpeg_image = new()
25
+ jpeg_image.set_data(ZBar::Format::JPEG, io_or_string)
26
+ return jpeg_image.convert(ZBar::Format::Y800)
27
+ end
28
+
29
+ # Instantiates an Image given raw PGM data.
30
+ #
31
+ # PGM is a NetPBM format, encoding width, height, and greyscale data, one byte
32
+ # per pixel. It is therefore ideally suited for loading into ZBar, which
33
+ # operates natively on Y800 pixel data--identical to the data section of a PGM
34
+ # file.
35
+ #
36
+ # The data is described in greater detail at
37
+ # http://netpbm.sourceforge.net/doc/pgm.html.
38
+ def self.from_pgm(io_or_string)
39
+ if io_or_string.respond_to?(:read)
40
+ io_or_string = io_or_string.read
41
+ end
42
+
43
+ image_data = io_or_string.gsub(/^(P5)\s([0-9]+)\s([0-9]+)\s([0-9]+)\s/, '')
44
+ if $1 != 'P5'
45
+ raise ArgumentError, "input must be a PGM file"
46
+ end
47
+
48
+ width, height, max_val = $2.to_i, $3.to_i, $4.to_i
49
+
50
+ if max_val != 255
51
+ raise ArgumentError, "maximum value must be 255"
52
+ end
53
+
54
+ image = new()
55
+ image.set_data(ZBar::Format::Y800, image_data, width, height)
56
+ image
57
+ end
58
+
59
+ # Load arbitrary data from a string into the Image object.
60
+ #
61
+ # Format must be a ZBar::Format constant. See the ZBar documentation
62
+ # for what formats are supported, and how the data should be formatted.
63
+ #
64
+ # Most everyone should use one of the <tt>Image.from_*</tt> methods instead.
65
+ def set_data(format, data, width=nil, height=nil)
66
+ ZBar.zbar_image_set_format(@img, format)
67
+
68
+ # Note the @buffer jog: it's to keep Ruby GC from losing the last
69
+ # reference to the old @buffer before calling image_set_data.
70
+ new_buffer = FFI::MemoryPointer.from_string(data)
71
+ ZBar.zbar_image_set_data(@img, new_buffer, data.size, nil)
72
+ @buffer = new_buffer
73
+
74
+ if width && height
75
+ ZBar.zbar_image_set_size(@img, width.to_i, height.to_i)
76
+ end
77
+ end
78
+
79
+ # Ask ZBar to convert this image to a new format, returning a new Image.
80
+ #
81
+ # Not all conversions are possible: for example, if ZBar was built without
82
+ # JPEG support, it cannot convert JPEGs into anything else.
83
+ def convert(format)
84
+ ptr = ZBar.zbar_image_convert(@img, format)
85
+ if ptr.null?
86
+ raise ArgumentError, "conversion failed"
87
+ else
88
+ Image.new(ptr)
89
+ end
90
+ end
91
+
92
+ # Attempt to recognize barcodes in this image, using the supplied processor
93
+ # (if any), falling back to defaults.
94
+ #
95
+ # Returns an array of ZBar::Symbol objects.
96
+ def process(processor = nil)
97
+ processor ||= Processor.new
98
+ processor.process(self)
99
+ end
100
+ end
101
+
102
+ end
@@ -0,0 +1,44 @@
1
+ module ZBar
2
+ extend FFI::Library
3
+ ffi_lib 'zbar'
4
+
5
+ attach_function :zbar_symbol_get_type, [:pointer], :int
6
+ attach_function :zbar_symbol_get_data, [:pointer], :string
7
+ attach_function :zbar_symbol_get_type, [:pointer], :int
8
+ attach_function :zbar_symbol_get_quality, [:pointer], :int
9
+ attach_function :zbar_symbol_get_loc_size, [:pointer], :uint
10
+ attach_function :zbar_symbol_get_loc_x, [:pointer, :uint], :int
11
+ attach_function :zbar_symbol_get_loc_y, [:pointer, :uint], :int
12
+ attach_function :zbar_symbol_next, [:pointer], :pointer
13
+
14
+ attach_function :zbar_image_create, [], :pointer
15
+ attach_function :zbar_image_destroy, [:pointer], :void
16
+ attach_function :zbar_image_first_symbol, [:pointer], :pointer
17
+ attach_function :zbar_image_set_format, [:pointer, :ulong], :void
18
+ attach_function :zbar_image_convert, [:pointer, :ulong], :pointer
19
+ attach_function :zbar_image_set_size, [:pointer, :uint, :uint], :void
20
+ attach_function :zbar_image_set_data, [:pointer, :buffer_in, :uint, :pointer], :void
21
+
22
+ attach_function :zbar_processor_create, [:int], :pointer
23
+ attach_function :zbar_processor_destroy, [:pointer], :void
24
+ attach_function :zbar_processor_init, [:pointer, :string, :int], :int
25
+
26
+ attach_function :zbar_process_image, [:pointer, :pointer], :int
27
+
28
+ attach_function :zbar_set_verbosity, [:int], :void
29
+ attach_function :zbar_get_symbol_name, [:int], :string
30
+ attach_function :zbar_get_addon_name, [:int], :string
31
+ attach_function :_zbar_error_spew, [:pointer, :int], :int
32
+
33
+ module Format #:nodoc:
34
+ %w(JPEG Y800 GREY).each { |format|
35
+ const_set(format.to_sym, format.unpack('V')[0])
36
+ }
37
+ end
38
+
39
+ # Sets the verbosity of the underlying ZBar library, which writes
40
+ # directly to stderr.
41
+ def self.verbosity=(v)
42
+ zbar_set_verbosity(v.to_i)
43
+ end
44
+ end
@@ -0,0 +1,37 @@
1
+ module ZBar
2
+ class Processor
3
+ # Create a new processor.
4
+ def initialize(threads = 0)
5
+ @processor = FFI::AutoPointer.new(
6
+ ZBar.zbar_processor_create(threads),
7
+ ZBar.method(:zbar_processor_destroy)
8
+ )
9
+
10
+ if ZBar.zbar_processor_init(@processor, nil, 0) > 0
11
+ ZBar._zbar_error_spew(@processor, 0)
12
+ raise "error!"
13
+ end
14
+ end
15
+
16
+ # Attempt to recognize barcodes in this image. Raises an exception if ZBar
17
+ # signals an error, otherwise returns an array of ZBar::Symbol objects.
18
+ def process(image)
19
+ raise ArgumentError, "process() operates only on ZBar::Image objects" unless image.kind_of?(ZBar::Image)
20
+ image = image.instance_variable_get(:@img)
21
+
22
+ if ZBar.zbar_process_image(@processor, image) != 0
23
+ raise ArgumentError, "processing failed"
24
+ end
25
+
26
+ out = []
27
+
28
+ sym = ZBar.zbar_image_first_symbol(image)
29
+ until sym.null?
30
+ out << Symbol.new(sym)
31
+ sym = ZBar.zbar_symbol_next(sym)
32
+ end
33
+
34
+ out
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,37 @@
1
+ module ZBar
2
+
3
+ class Symbol
4
+ attr_reader :symbology, :data, :addon, :quality, :location
5
+
6
+ def initialize(symbol=nil)
7
+ if symbol
8
+ type = ZBar.zbar_symbol_get_type(symbol)
9
+ @symbology = ZBar.zbar_get_symbol_name(type)
10
+ @data = ZBar.zbar_symbol_get_data(symbol)
11
+ @addon = ZBar.zbar_get_addon_name(type)
12
+ @quality = ZBar.zbar_symbol_get_quality(symbol)
13
+
14
+ @location = []
15
+ point_count = ZBar.zbar_symbol_get_loc_size(symbol)
16
+ i = 0
17
+ while i < point_count
18
+ @location << [ZBar.zbar_symbol_get_loc_x(symbol, i), ZBar.zbar_symbol_get_loc_y(symbol, i)]
19
+ i += 1
20
+ end
21
+ end
22
+ end
23
+
24
+ def ==(symbol)
25
+ return false unless symbol.kind_of?(Symbol)
26
+
27
+ (
28
+ self.symbology == symbol.symbology &&
29
+ self.data == symbol.data &&
30
+ self.addon == symbol.addon &&
31
+ self.quality == symbol.quality &&
32
+ self.location == symbol.location
33
+ )
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'zbar'
8
+
9
+ class Test::Unit::TestCase
10
+ end
Binary file
Binary file
@@ -0,0 +1,42 @@
1
+ require 'helper'
2
+
3
+ Path = File.dirname(__FILE__)
4
+
5
+ class TestZBar < Test::Unit::TestCase
6
+ should "read the right barcode from a PGM blob" do
7
+ result = ZBar::Image.from_pgm(File.read("#{Path}/test.pgm")).process
8
+ assert_equal(result.size, 1)
9
+ assert_equal(result[0].data, '9876543210128')
10
+ assert_equal(result[0].symbology, 'EAN-13')
11
+ end
12
+
13
+ should "read a barcode from a PGM file" do
14
+ File.open("#{Path}/test.pgm") { |f|
15
+ result = ZBar::Image.from_pgm(f).process
16
+ assert_equal(result.size, 1)
17
+ }
18
+ end
19
+
20
+ should "be able to re-use a processor" do
21
+ processor = ZBar::Processor.new
22
+ pgm = File.read("#{Path}/test.pgm")
23
+
24
+ result1 = processor.process ZBar::Image.from_pgm(pgm)
25
+ result2 = processor.process ZBar::Image.from_pgm(pgm)
26
+ assert_equal(result1.size, 1)
27
+ assert_equal(result2.size, 1)
28
+ assert_equal(result1, result2)
29
+ end
30
+
31
+ should "read a barcode from a JPEG blob" do
32
+ result = ZBar::Image.from_jpeg(File.read("#{Path}/test.jpg")).process
33
+ assert_equal(result.size, 1)
34
+ end
35
+
36
+ should "read a barcode from a JPEG file" do
37
+ File.open("#{Path}/test.jpg") { |f|
38
+ result = ZBar::Image.from_jpeg(f).process
39
+ assert_equal(result.size, 1)
40
+ }
41
+ end
42
+ end
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{zbar}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Will Glynn"]
12
+ s.date = %q{2010-08-29}
13
+ s.description = %q{Ruby bindings for ZBar, a barcode recognition library. Uses FFI to interact with the underlying C library, but has no other dependencies.}
14
+ s.email = %q{will@willglynn.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/zbar.rb",
27
+ "lib/zbar/image.rb",
28
+ "lib/zbar/lib.rb",
29
+ "lib/zbar/processor.rb",
30
+ "lib/zbar/symbol.rb",
31
+ "test/helper.rb",
32
+ "test/test.jpg",
33
+ "test/test.pgm",
34
+ "test/test_zbar.rb",
35
+ "zbar.gemspec"
36
+ ]
37
+ s.homepage = %q{http://github.com/delta407/ruby-zbar}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.6}
41
+ s.summary = %q{Ruby bindings for ZBar, a barcode recognition library}
42
+ s.test_files = [
43
+ "test/helper.rb",
44
+ "test/test_zbar.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
53
+ s.add_runtime_dependency(%q<ffi>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
56
+ s.add_dependency(%q<ffi>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
60
+ s.add_dependency(%q<ffi>, [">= 0"])
61
+ end
62
+ end
63
+
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zbar
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Will Glynn
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-29 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: thoughtbot-shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: ffi
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
44
+ description: Ruby bindings for ZBar, a barcode recognition library. Uses FFI to interact with the underlying C library, but has no other dependencies.
45
+ email: will@willglynn.com
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ extra_rdoc_files:
51
+ - LICENSE
52
+ - README.rdoc
53
+ files:
54
+ - .document
55
+ - .gitignore
56
+ - LICENSE
57
+ - README.rdoc
58
+ - Rakefile
59
+ - VERSION
60
+ - lib/zbar.rb
61
+ - lib/zbar/image.rb
62
+ - lib/zbar/lib.rb
63
+ - lib/zbar/processor.rb
64
+ - lib/zbar/symbol.rb
65
+ - test/helper.rb
66
+ - test/test.jpg
67
+ - test/test.pgm
68
+ - test/test_zbar.rb
69
+ - zbar.gemspec
70
+ has_rdoc: true
71
+ homepage: http://github.com/delta407/ruby-zbar
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options:
76
+ - --charset=UTF-8
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.3.6
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Ruby bindings for ZBar, a barcode recognition library
100
+ test_files:
101
+ - test/helper.rb
102
+ - test/test_zbar.rb