zbar 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +2 -0
- data/.travis.yml +15 -0
- data/Gemfile +11 -0
- data/README.rdoc +2 -2
- data/Rakefile +25 -36
- data/VERSION +1 -1
- data/lib/zbar.rb +1 -0
- data/lib/zbar/image.rb +32 -5
- data/lib/zbar/jpeg.rb +57 -0
- data/lib/zbar/lib.rb +10 -3
- data/lib/zbar/processor.rb +79 -3
- data/spec/spec_helper.rb +12 -0
- data/spec/support/file_helpers.rb +13 -0
- data/{test → spec/support}/test.jpg +0 -0
- data/{test → spec/support}/test.pgm +0 -0
- data/spec/zbar/image_spec.rb +60 -0
- data/spec/zbar/processor_spec.rb +113 -0
- data/spec/zbar_spec.rb +5 -0
- data/zbar.gemspec +50 -38
- metadata +133 -67
- data/.gitignore +0 -21
- data/test/helper.rb +0 -10
- data/test/test_zbar.rb +0 -42
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.rdoc
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
= zbar
|
2
2
|
by Will Glynn
|
3
3
|
|
4
|
-
http://github.com/
|
4
|
+
http://github.com/willglynn/ruby-zbar
|
5
5
|
|
6
6
|
== DESCRIPTION:
|
7
7
|
|
@@ -56,4 +56,4 @@ Doesn't expose all ZBar functionality, including:
|
|
56
56
|
|
57
57
|
== Copyright
|
58
58
|
|
59
|
-
Copyright (c) 2010 Will Glynn. See LICENSE for details.
|
59
|
+
Copyright (c) 2010-2013 Will Glynn. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -1,48 +1,37 @@
|
|
1
|
-
|
2
|
-
require 'rake'
|
1
|
+
# encoding: utf-8
|
3
2
|
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
4
5
|
begin
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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"
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
19
11
|
end
|
12
|
+
require 'rake'
|
20
13
|
|
21
|
-
require '
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
gem.name = "zbar"
|
17
|
+
gem.summary = "Ruby bindings for ZBar, a barcode recognition library"
|
18
|
+
gem.description ="Ruby bindings for ZBar, a barcode recognition library. Uses FFI to interact with the underlying C library, but has no other dependencies."
|
19
|
+
gem.email = "will@willglynn.com"
|
20
|
+
gem.homepage = "http://github.com/delta407/ruby-zbar"
|
21
|
+
gem.authors = ["Will Glynn"]
|
22
|
+
# dependencies defined in Gemfile
|
26
23
|
end
|
24
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
25
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
26
|
+
require 'rspec/core'
|
27
|
+
require 'rspec/core/rake_task'
|
28
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
29
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
39
30
|
end
|
40
31
|
|
41
|
-
task :
|
42
|
-
|
43
|
-
task :default => :test
|
32
|
+
task :default => :spec
|
44
33
|
|
45
|
-
require '
|
34
|
+
require 'rdoc/task'
|
46
35
|
Rake::RDocTask.new do |rdoc|
|
47
36
|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
37
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/zbar.rb
CHANGED
data/lib/zbar/image.rb
CHANGED
@@ -17,6 +17,9 @@ module ZBar
|
|
17
17
|
# and convert it into a greyscale image suitable for further processing.
|
18
18
|
# This conversion may fail if ZBar was not built with <tt>--with-jpeg</tt>.
|
19
19
|
def self.from_jpeg(io_or_string)
|
20
|
+
raise "JPEG support is not available in your copy of libzbar" unless ZBar::JPEG.available?
|
21
|
+
ZBar::JPEG.warn_once_if_bugged
|
22
|
+
|
20
23
|
if io_or_string.respond_to?(:read)
|
21
24
|
io_or_string = io_or_string.read
|
22
25
|
end
|
@@ -37,10 +40,17 @@ module ZBar
|
|
37
40
|
# http://netpbm.sourceforge.net/doc/pgm.html.
|
38
41
|
def self.from_pgm(io_or_string)
|
39
42
|
if io_or_string.respond_to?(:read)
|
40
|
-
|
43
|
+
string = io_or_string.read
|
44
|
+
else
|
45
|
+
string = io_or_string
|
41
46
|
end
|
42
47
|
|
43
|
-
|
48
|
+
# Ensure we're in binary mode
|
49
|
+
if string.respond_to? :force_encoding
|
50
|
+
string.force_encoding 'binary'
|
51
|
+
end
|
52
|
+
|
53
|
+
image_data = string.gsub(/^(P5)\s([0-9]+)\s([0-9]+)\s([0-9]+)\s/, '')
|
44
54
|
if $1 != 'P5'
|
45
55
|
raise ArgumentError, "input must be a PGM file"
|
46
56
|
end
|
@@ -90,13 +100,30 @@ module ZBar
|
|
90
100
|
end
|
91
101
|
|
92
102
|
# Attempt to recognize barcodes in this image, using the supplied processor
|
93
|
-
# (if any), falling back to defaults.
|
103
|
+
# or processor configuration (if any), falling back to defaults.
|
94
104
|
#
|
95
105
|
# Returns an array of ZBar::Symbol objects.
|
96
|
-
def process(
|
97
|
-
|
106
|
+
def process(processor_or_config = nil)
|
107
|
+
if processor_or_config.respond_to?(:process)
|
108
|
+
processor = processor_or_config
|
109
|
+
elsif processor_or_config.nil?
|
110
|
+
processor = Processor.new
|
111
|
+
else
|
112
|
+
processor = Processor.new(processor_or_config)
|
113
|
+
end
|
114
|
+
|
98
115
|
processor.process(self)
|
99
116
|
end
|
117
|
+
|
118
|
+
# Retrieve the image's width in pixels
|
119
|
+
def width
|
120
|
+
ZBar.zbar_image_get_width(@img)
|
121
|
+
end
|
122
|
+
|
123
|
+
# Retrieve the image's height in pixels
|
124
|
+
def height
|
125
|
+
ZBar.zbar_image_get_height(@img)
|
126
|
+
end
|
100
127
|
end
|
101
128
|
|
102
129
|
end
|
data/lib/zbar/jpeg.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# zbar-0.10 has a problem handling certain JPEGs:
|
2
|
+
# * http://sourceforge.net/p/zbar/discussion/664596/thread/58b8d79b
|
3
|
+
# * https://github.com/willglynn/ruby-zbar/issues/2
|
4
|
+
#
|
5
|
+
# This is a problem in the underlying C library. It is fixed in zbar's source
|
6
|
+
# control repository, but an updated package has not yet been released.
|
7
|
+
#
|
8
|
+
# If this affects you, ask whoever compiled your libzbar to apply this patch:
|
9
|
+
# https://gist.github.com/willglynn/5659946
|
10
|
+
|
11
|
+
module ZBar::JPEG
|
12
|
+
# Is JPEG support available?
|
13
|
+
def self.available?
|
14
|
+
if @available.nil?
|
15
|
+
@available = image_works?(WorkingBits)
|
16
|
+
end
|
17
|
+
|
18
|
+
@available
|
19
|
+
end
|
20
|
+
|
21
|
+
# Is JPEG support bugged?
|
22
|
+
# See https://github.com/willglynn/ruby-zbar/issues/2 for details.
|
23
|
+
def self.bugged?
|
24
|
+
if @bugged.nil?
|
25
|
+
@bugged = available? && !image_works?(BuggedBits)
|
26
|
+
end
|
27
|
+
|
28
|
+
@bugged
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.warn_once_if_bugged #:nodoc:
|
32
|
+
@warn_attempted ||= begin
|
33
|
+
if bugged?
|
34
|
+
STDERR.print "Your libzbar has a JPEG bug. Some images will fail to process correctly.\nSee: https://github.com/willglynn/ruby-zbar/blob/master/lib/zbar/jpeg.rb\n"
|
35
|
+
end
|
36
|
+
true
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
WorkingBits = "\377\330\377\340\000\020JFIF\000\001\001\000\000\001\000\001\000\000\377\333\000C\000\e\032\032)\035)A&&AB///BG?>>?GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG\377\333\000C\001\035))4&4?((?G?5?GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG\377\300\000\021\b\000\b\000\b\003\001\"\000\002\021\001\003\021\001\377\304\000\025\000\001\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\006\377\304\000\024\020\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\304\000\024\001\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\304\000\024\021\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\332\000\f\003\001\000\002\021\003\021\000?\000\246\000\037\377\331"
|
42
|
+
BuggedBits = "\377\330\377\340\000\020JFIF\000\001\002\000\000d\000d\000\000\377\354\000\021Ducky\000\001\000\004\000\000\000\000\000\000\377\356\000\016Adobe\000d\300\000\000\000\001\377\333\000\204\000\e\032\032)\035)A&&AB///BG?>>?GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG\001\035))4&4?((?G?5?GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG\377\300\000\021\b\000\b\000\b\003\001\"\000\002\021\001\003\021\001\377\304\000K\000\001\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\006\001\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\020\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\021\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\332\000\f\003\001\000\002\021\003\021\000?\000\246\000\037\377\331"
|
43
|
+
|
44
|
+
def self.image_works?(bits) #:nodoc:
|
45
|
+
ZBar::Image.new.tap { |img|
|
46
|
+
img.set_data(ZBar::Format::JPEG, bits)
|
47
|
+
img.convert(ZBar::Format::Y800)
|
48
|
+
} && true
|
49
|
+
rescue ArgumentError
|
50
|
+
if $!.to_s == "conversion failed"
|
51
|
+
false
|
52
|
+
else
|
53
|
+
raise
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/lib/zbar/lib.rb
CHANGED
@@ -9,9 +9,11 @@ module ZBar
|
|
9
9
|
begin
|
10
10
|
ffi_lib(*paths)
|
11
11
|
rescue LoadError => le
|
12
|
-
raise LoadError,
|
13
|
-
"
|
14
|
-
"Please install zbar (http://zbar.sourceforge.net/)"
|
12
|
+
raise LoadError, [
|
13
|
+
"Didn't find libzbar on your system",
|
14
|
+
"Please install zbar (http://zbar.sourceforge.net/) or ZBAR_LIB if it's in a weird place",
|
15
|
+
"FFI::Library::ffi_lib() failed with error: #{le}"
|
16
|
+
].join("\n")
|
15
17
|
end
|
16
18
|
|
17
19
|
attach_function :zbar_symbol_get_type, [:pointer], :int
|
@@ -28,15 +30,20 @@ module ZBar
|
|
28
30
|
attach_function :zbar_image_first_symbol, [:pointer], :pointer
|
29
31
|
attach_function :zbar_image_set_format, [:pointer, :ulong], :void
|
30
32
|
attach_function :zbar_image_convert, [:pointer, :ulong], :pointer
|
33
|
+
attach_function :zbar_image_get_width, [:pointer], :int
|
34
|
+
attach_function :zbar_image_get_height, [:pointer], :int
|
31
35
|
attach_function :zbar_image_set_size, [:pointer, :uint, :uint], :void
|
32
36
|
attach_function :zbar_image_set_data, [:pointer, :buffer_in, :uint, :pointer], :void
|
33
37
|
|
34
38
|
attach_function :zbar_processor_create, [:int], :pointer
|
35
39
|
attach_function :zbar_processor_destroy, [:pointer], :void
|
36
40
|
attach_function :zbar_processor_init, [:pointer, :string, :int], :int
|
41
|
+
attach_function :zbar_processor_set_config, [:pointer, :int, :int, :int], :int
|
37
42
|
|
38
43
|
attach_function :zbar_process_image, [:pointer, :pointer], :int
|
39
44
|
|
45
|
+
attach_function :zbar_parse_config, [:string, :pointer, :pointer, :pointer], :int
|
46
|
+
|
40
47
|
attach_function :zbar_set_verbosity, [:int], :void
|
41
48
|
attach_function :zbar_get_symbol_name, [:int], :string
|
42
49
|
attach_function :zbar_get_addon_name, [:int], :string
|
data/lib/zbar/processor.rb
CHANGED
@@ -1,9 +1,17 @@
|
|
1
1
|
module ZBar
|
2
2
|
class Processor
|
3
3
|
# Create a new processor.
|
4
|
-
|
4
|
+
# Accepts a hash of configurations
|
5
|
+
def initialize(config = nil)
|
6
|
+
# This function used to accept an integer refering to the number of threads
|
7
|
+
if config.kind_of?(Fixnum)
|
8
|
+
config = { :threads => config }
|
9
|
+
end
|
10
|
+
|
11
|
+
config ||= {}
|
12
|
+
|
5
13
|
@processor = FFI::AutoPointer.new(
|
6
|
-
ZBar.zbar_processor_create(threads),
|
14
|
+
ZBar.zbar_processor_create(config[:threads] || 0),
|
7
15
|
ZBar.method(:zbar_processor_destroy)
|
8
16
|
)
|
9
17
|
|
@@ -11,8 +19,76 @@ module ZBar
|
|
11
19
|
ZBar._zbar_error_spew(@processor, 0)
|
12
20
|
raise "error!"
|
13
21
|
end
|
22
|
+
|
23
|
+
config.each { |key,value|
|
24
|
+
if key == :threads
|
25
|
+
# do nothing; we handled this above
|
26
|
+
else
|
27
|
+
setter = "#{key}=".to_sym
|
28
|
+
if respond_to?(setter)
|
29
|
+
send(setter, value)
|
30
|
+
else
|
31
|
+
raise ArgumentError, "unsupported configuration option: #{key}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
# Indicates that this processor should only search for the indicated symbologies.
|
38
|
+
# Accepts an array of the following symbologies, specified as a string or symbol:
|
39
|
+
# - qrcode
|
40
|
+
# - upca
|
41
|
+
# - upce
|
42
|
+
# - ean13
|
43
|
+
# - ean8
|
44
|
+
# - i25
|
45
|
+
# - scanner
|
46
|
+
# - isbn13
|
47
|
+
# - isbn10
|
48
|
+
# - code39
|
49
|
+
# - pdf417
|
50
|
+
# - code128
|
51
|
+
def symbologies=(symbologies)
|
52
|
+
self.zbar_config = ["disable"] + symbologies.map { |sym| "#{sym}.enable" }
|
53
|
+
true
|
54
|
+
end
|
55
|
+
|
56
|
+
# Indicates that this processor should only search for the indicated symbology.
|
57
|
+
# See #symbologies= for details.
|
58
|
+
def symbology=(symbology)
|
59
|
+
self.symbologies = [symbology]
|
60
|
+
end
|
61
|
+
|
62
|
+
# Configures this processor using the specified configuration string. See
|
63
|
+
# zbar_parse_config (zbar/config.c) for supported values.
|
64
|
+
def zbar_config=(config_value)
|
65
|
+
case config_value
|
66
|
+
when String
|
67
|
+
symbology = FFI::MemoryPointer.new :int
|
68
|
+
config_t = FFI::MemoryPointer.new :int
|
69
|
+
value = FFI::MemoryPointer.new :int
|
70
|
+
|
71
|
+
if ZBar.zbar_parse_config(config_value, symbology, config_t, value) == 0
|
72
|
+
# parsed successfully
|
73
|
+
if ZBar.zbar_processor_set_config(@processor, symbology.read_int, config_t.read_int, value.read_int) == 0
|
74
|
+
true
|
75
|
+
else
|
76
|
+
raise ArgumentError, "config string #{config_value.inspect} parsed but could not be set"
|
77
|
+
end
|
78
|
+
else
|
79
|
+
raise ArgumentError, "config string #{config_value.inspect} was not recognized"
|
80
|
+
end
|
81
|
+
|
82
|
+
when Enumerable
|
83
|
+
config_value.each { |value|
|
84
|
+
self.zbar_config = value
|
85
|
+
}
|
86
|
+
|
87
|
+
else
|
88
|
+
raise ArgumentError, "unsupported config value"
|
89
|
+
end
|
14
90
|
end
|
15
|
-
|
91
|
+
|
16
92
|
# Attempt to recognize barcodes in this image. Raises an exception if ZBar
|
17
93
|
# signals an error, otherwise returns an array of ZBar::Symbol objects.
|
18
94
|
def process(image)
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'zbar'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.include FileHelpers
|
12
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module FileHelpers
|
2
|
+
def file_path(filename)
|
3
|
+
File.dirname(__FILE__) + "/" + filename
|
4
|
+
end
|
5
|
+
|
6
|
+
def open_file(filename, &block)
|
7
|
+
File.open(file_path(filename), 'rb', &block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def read_file(filename)
|
11
|
+
open_file(filename, &:read)
|
12
|
+
end
|
13
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe ZBar::Image do
|
4
|
+
|
5
|
+
if !ZBar::JPEG.available?
|
6
|
+
pending "JPEG support is not available in your copy of libzbar; skipping tests"
|
7
|
+
elsif ZBar::JPEG.bugged?
|
8
|
+
ZBar::JPEG.warn_once_if_bugged
|
9
|
+
pending "JPEG support is broken in your copy of libzbar; skipping tests"
|
10
|
+
else
|
11
|
+
describe ".from_jpeg" do
|
12
|
+
let(:jpeg_data) { read_file jpeg_file }
|
13
|
+
subject { described_class.from_jpeg(jpeg_data) }
|
14
|
+
context "given test.jpg" do
|
15
|
+
let(:jpeg_file) { "test.jpg" }
|
16
|
+
|
17
|
+
its(:width) { should == 480 }
|
18
|
+
its(:height) { should == 240 }
|
19
|
+
|
20
|
+
describe "process" do
|
21
|
+
it "delegates to the passed processor" do
|
22
|
+
processor = double("processor")
|
23
|
+
expected_result = Object.new
|
24
|
+
processor.should_receive(:process).with(subject).and_return(expected_result)
|
25
|
+
|
26
|
+
subject.process(processor).should == expected_result
|
27
|
+
end
|
28
|
+
|
29
|
+
it "instantiates a new processor with no arguments" do
|
30
|
+
processor = double("processor")
|
31
|
+
processor.should_receive(:process)
|
32
|
+
ZBar::Processor.should_receive(:new).with().and_return(processor)
|
33
|
+
subject.process
|
34
|
+
end
|
35
|
+
|
36
|
+
it "instantiates a new processor with configuration" do
|
37
|
+
config_hash = { :foo => :bar }
|
38
|
+
processor = double("processor")
|
39
|
+
processor.should_receive(:process)
|
40
|
+
ZBar::Processor.should_receive(:new).with(config_hash).and_return(processor)
|
41
|
+
subject.process(config_hash)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe ".from_pgm" do
|
49
|
+
let(:pgm_data) { read_file pgm_file }
|
50
|
+
let(:image) { described_class.from_pgm(pgm_data) }
|
51
|
+
subject { image }
|
52
|
+
|
53
|
+
context "given test.pgm" do
|
54
|
+
let(:pgm_file) { "test.pgm" }
|
55
|
+
|
56
|
+
its(:width) { should == 480 }
|
57
|
+
its(:height) { should == 240 }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe ZBar::Processor do
|
4
|
+
describe "new" do
|
5
|
+
it "calls ZBar.zbar_processor_create" do
|
6
|
+
ZBar.should_receive(:zbar_processor_create).and_call_original
|
7
|
+
described_class.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "calls ZBar.zbar_processor_init" do
|
11
|
+
ZBar.should_receive(:zbar_processor_init).and_call_original
|
12
|
+
described_class.new
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should instantiate with no parameters" do
|
16
|
+
described_class.new
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should instantiate with an integer parameter" do
|
20
|
+
described_class.new(0)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should instantiate with an hash parameter" do
|
24
|
+
described_class.new(:threads => 0)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#zbar_config=" do
|
29
|
+
it "calls ZBar.zbar_parse_config" do
|
30
|
+
ZBar.should_receive(:zbar_parse_config).with("disable", anything(), anything(), anything()).and_call_original
|
31
|
+
subject.zbar_config = "disable"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "calls ZBar.zbar_processor_set_config" do
|
35
|
+
ZBar.should_receive(:zbar_processor_set_config).and_call_original
|
36
|
+
subject.zbar_config = "disable"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "calls itself repeatedly when given an array" do
|
40
|
+
args = ["foo", :bar, 1]
|
41
|
+
subject.should_receive(:zbar_config=).with(args).and_call_original
|
42
|
+
args.each { |arg|
|
43
|
+
subject.should_receive(:zbar_config=).with(arg)
|
44
|
+
}
|
45
|
+
subject.zbar_config = args
|
46
|
+
end
|
47
|
+
|
48
|
+
it "succeeds for \"disable\"" do
|
49
|
+
subject.zbar_config = "disable"
|
50
|
+
end
|
51
|
+
it "succeeds for \"enable=0\"" do
|
52
|
+
subject.zbar_config = "enable=0"
|
53
|
+
end
|
54
|
+
it "succeeds for \"qrcode.enable=1\"" do
|
55
|
+
subject.zbar_config = "qrcode.enable=1"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "raises ArgumentError for \"foo bar baz\"" do
|
59
|
+
lambda {
|
60
|
+
subject.zbar_config = "foo bar baz"
|
61
|
+
}.should raise_error ArgumentError
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#symbology=" do
|
66
|
+
it "delegates to #symbologies=" do
|
67
|
+
subject.should_receive(:symbologies=).with(['foo'])
|
68
|
+
subject.symbology = 'foo'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "#symbologies=" do
|
73
|
+
it "enables the selected symbologies" do
|
74
|
+
subject.should_receive(:zbar_config=).with(["disable", "foo.enable", "bar.enable"])
|
75
|
+
subject.symbologies = ['foo', :bar]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "#process" do
|
80
|
+
context "when processing test.pgm" do
|
81
|
+
let(:pgm_data) { read_file "test.pgm" }
|
82
|
+
let(:image) { ZBar::Image.from_pgm(pgm_data) }
|
83
|
+
let(:config) { {} }
|
84
|
+
let(:processor) { described_class.new(config) }
|
85
|
+
subject { processor.process(image) }
|
86
|
+
|
87
|
+
it "finds the expected symbol" do
|
88
|
+
symbols = subject
|
89
|
+
symbols.size.should == 1
|
90
|
+
|
91
|
+
symbol = symbols[0]
|
92
|
+
symbol.should be_kind_of ZBar::Symbol
|
93
|
+
symbol.data.should == "9876543210128"
|
94
|
+
symbol.symbology.should == "EAN-13"
|
95
|
+
end
|
96
|
+
|
97
|
+
context "when all symbologies are disabled" do
|
98
|
+
let(:config) { { :symbologies => [] } }
|
99
|
+
it { should be_empty }
|
100
|
+
end
|
101
|
+
|
102
|
+
context "when only a wrong symbology is enabled" do
|
103
|
+
let(:config) { { :symbology => :qrcode } }
|
104
|
+
it { should be_empty }
|
105
|
+
end
|
106
|
+
|
107
|
+
context "when the correct symbology is enabled" do
|
108
|
+
let(:config) { { :symbologies => ["qrcode", :ean13] } }
|
109
|
+
it { should_not be_empty }
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
data/spec/zbar_spec.rb
ADDED
data/zbar.gemspec
CHANGED
@@ -1,63 +1,75 @@
|
|
1
1
|
# Generated by jeweler
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.
|
7
|
+
s.name = "zbar"
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Will Glynn"]
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
12
|
+
s.date = "2013-06-13"
|
13
|
+
s.description = "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 = "will@willglynn.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
|
-
|
17
|
+
"README.rdoc"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
21
|
+
".rspec",
|
22
|
+
".travis.yml",
|
23
|
+
"Gemfile",
|
24
|
+
"LICENSE",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/zbar.rb",
|
29
|
+
"lib/zbar/image.rb",
|
30
|
+
"lib/zbar/jpeg.rb",
|
31
|
+
"lib/zbar/lib.rb",
|
32
|
+
"lib/zbar/processor.rb",
|
33
|
+
"lib/zbar/symbol.rb",
|
34
|
+
"spec/spec_helper.rb",
|
35
|
+
"spec/support/file_helpers.rb",
|
36
|
+
"spec/support/test.jpg",
|
37
|
+
"spec/support/test.pgm",
|
38
|
+
"spec/zbar/image_spec.rb",
|
39
|
+
"spec/zbar/processor_spec.rb",
|
40
|
+
"spec/zbar_spec.rb",
|
41
|
+
"zbar.gemspec"
|
36
42
|
]
|
37
|
-
s.homepage =
|
38
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
43
|
+
s.homepage = "http://github.com/delta407/ruby-zbar"
|
39
44
|
s.require_paths = ["lib"]
|
40
|
-
s.rubygems_version =
|
41
|
-
s.summary =
|
42
|
-
s.test_files = [
|
43
|
-
"test/helper.rb",
|
44
|
-
"test/test_zbar.rb"
|
45
|
-
]
|
45
|
+
s.rubygems_version = "1.8.25"
|
46
|
+
s.summary = "Ruby bindings for ZBar, a barcode recognition library"
|
46
47
|
|
47
48
|
if s.respond_to? :specification_version then
|
48
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
49
49
|
s.specification_version = 3
|
50
50
|
|
51
|
-
if Gem::Version.new(Gem::
|
52
|
-
s.
|
53
|
-
s.
|
51
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_runtime_dependency(%q<ffi>, [">= 1.0.0"])
|
53
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.12.0"])
|
54
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.12.0"])
|
55
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
56
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
57
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
54
58
|
else
|
55
|
-
s.add_dependency(%q<
|
56
|
-
s.add_dependency(%q<
|
59
|
+
s.add_dependency(%q<ffi>, [">= 1.0.0"])
|
60
|
+
s.add_dependency(%q<rspec>, ["~> 2.12.0"])
|
61
|
+
s.add_dependency(%q<rspec>, ["~> 2.12.0"])
|
62
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
63
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
64
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
57
65
|
end
|
58
66
|
else
|
59
|
-
s.add_dependency(%q<
|
60
|
-
s.add_dependency(%q<
|
67
|
+
s.add_dependency(%q<ffi>, [">= 1.0.0"])
|
68
|
+
s.add_dependency(%q<rspec>, ["~> 2.12.0"])
|
69
|
+
s.add_dependency(%q<rspec>, ["~> 2.12.0"])
|
70
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
71
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
72
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
61
73
|
end
|
62
74
|
end
|
63
75
|
|
metadata
CHANGED
@@ -1,102 +1,168 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: zbar
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 1
|
8
|
-
- 1
|
9
|
-
version: 0.1.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Will Glynn
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
12
|
+
date: 2013-06-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: ffi
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.0
|
22
|
+
type: :runtime
|
22
23
|
prerelease: false
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.12.0
|
30
38
|
type: :development
|
31
|
-
version_requirements: *id001
|
32
|
-
- !ruby/object:Gem::Dependency
|
33
|
-
name: ffi
|
34
39
|
prerelease: false
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.12.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.12.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.12.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rdoc
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '3.12'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '3.12'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: bundler
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '1.0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '1.0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: jeweler
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.8.4
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 1.8.4
|
110
|
+
description: Ruby bindings for ZBar, a barcode recognition library. Uses FFI to interact
|
111
|
+
with the underlying C library, but has no other dependencies.
|
45
112
|
email: will@willglynn.com
|
46
113
|
executables: []
|
47
|
-
|
48
114
|
extensions: []
|
49
|
-
|
50
|
-
extra_rdoc_files:
|
115
|
+
extra_rdoc_files:
|
51
116
|
- LICENSE
|
52
117
|
- README.rdoc
|
53
|
-
files:
|
118
|
+
files:
|
54
119
|
- .document
|
55
|
-
- .
|
120
|
+
- .rspec
|
121
|
+
- .travis.yml
|
122
|
+
- Gemfile
|
56
123
|
- LICENSE
|
57
124
|
- README.rdoc
|
58
125
|
- Rakefile
|
59
126
|
- VERSION
|
60
127
|
- lib/zbar.rb
|
61
128
|
- lib/zbar/image.rb
|
129
|
+
- lib/zbar/jpeg.rb
|
62
130
|
- lib/zbar/lib.rb
|
63
131
|
- lib/zbar/processor.rb
|
64
132
|
- lib/zbar/symbol.rb
|
65
|
-
-
|
66
|
-
-
|
67
|
-
-
|
68
|
-
- test
|
133
|
+
- spec/spec_helper.rb
|
134
|
+
- spec/support/file_helpers.rb
|
135
|
+
- spec/support/test.jpg
|
136
|
+
- spec/support/test.pgm
|
137
|
+
- spec/zbar/image_spec.rb
|
138
|
+
- spec/zbar/processor_spec.rb
|
139
|
+
- spec/zbar_spec.rb
|
69
140
|
- zbar.gemspec
|
70
|
-
has_rdoc: true
|
71
141
|
homepage: http://github.com/delta407/ruby-zbar
|
72
142
|
licenses: []
|
73
|
-
|
74
143
|
post_install_message:
|
75
|
-
rdoc_options:
|
76
|
-
|
77
|
-
require_paths:
|
144
|
+
rdoc_options: []
|
145
|
+
require_paths:
|
78
146
|
- lib
|
79
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
-
requirements:
|
88
|
-
- - ">="
|
89
|
-
- !ruby/object:Gem::Version
|
90
|
-
segments:
|
147
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
148
|
+
none: false
|
149
|
+
requirements:
|
150
|
+
- - ! '>='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
segments:
|
91
154
|
- 0
|
92
|
-
|
155
|
+
hash: 4088186622936310540
|
156
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
157
|
+
none: false
|
158
|
+
requirements:
|
159
|
+
- - ! '>='
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
version: '0'
|
93
162
|
requirements: []
|
94
|
-
|
95
163
|
rubyforge_project:
|
96
|
-
rubygems_version: 1.
|
164
|
+
rubygems_version: 1.8.25
|
97
165
|
signing_key:
|
98
166
|
specification_version: 3
|
99
167
|
summary: Ruby bindings for ZBar, a barcode recognition library
|
100
|
-
test_files:
|
101
|
-
- test/helper.rb
|
102
|
-
- test/test_zbar.rb
|
168
|
+
test_files: []
|
data/.gitignore
DELETED
data/test/helper.rb
DELETED
data/test/test_zbar.rb
DELETED
@@ -1,42 +0,0 @@
|
|
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
|