zxing 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile ADDED
@@ -0,0 +1,65 @@
1
+ h2. Decode QR Codes
2
+
3
+ p. "QR code":http://en.wikipedia.org/wiki/QR_Code generation is well served in the Ruby community, but decoding seems to be stuck in the Java world. This is an attempt to bridge the gap by wrapping the "ZXing":http://code.google.com/p/zxing/ library with JRuby. ZXing conveniently decodes a plethora of barcodes. Their site has a complete list.
4
+
5
+ h2. Requirements
6
+
7
+ * JRuby (tested with 1.4.0)
8
+ * shoulda (for testing)
9
+
10
+ h2. Using the ZXing module/singleton.
11
+
12
+ <pre>
13
+ <code>
14
+ require 'zxing'
15
+
16
+ # You can decode a URL...
17
+ ZXing.decode 'http://2d-code.co.uk/images/bbc-logo-in-qr-code.gif'
18
+
19
+ # ... or a file path...
20
+ ZXing.decode '/Users/ecin/qrcode.png'
21
+
22
+ # ... or a File object...
23
+ ZXing.decode File.open('qrcode.png')
24
+
25
+ # ... or anything that returns a URL or file path when #path or #to_s
26
+ # is called on it.
27
+ class Image
28
+ attr_reader :path
29
+ def initialize(path); @path = path end
30
+ end
31
+
32
+ image = Image.new('qrcode.png')
33
+ ZXing.decode image
34
+
35
+ # #decode returns nil if it can't decode the image.
36
+ ZXing.decode 'image_without_a_code.png'
37
+ # => nil
38
+
39
+ # #decode! will raise an error if it can't decode the image.
40
+ ZXing.decode! 'image_without_a_code.png'
41
+ # => NativeException
42
+
43
+ # Feel free to include ZXing to shorten the call.
44
+ include ZXing
45
+
46
+ decode 'qrcode.png'
47
+ </code>
48
+ </pre>
49
+
50
+ h2. Including the Decodable module.
51
+
52
+ p. A Decodable module is included (pun intended) to ease using the library with objects that return the URL or file path to decode when #path or #to_s is called.
53
+
54
+ <pre>
55
+ <code>
56
+ require 'zxing/decodable'
57
+
58
+ class File
59
+ include Decodable
60
+ end
61
+
62
+ file = File.open('qrcode.png')
63
+ file.decode
64
+ </code>
65
+ </pre>
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/lib/core.jar ADDED
Binary file
data/lib/javase.jar ADDED
Binary file
data/lib/zxing.rb ADDED
@@ -0,0 +1,56 @@
1
+ raise "ZXing requires JRuby" unless defined?(JRuby)
2
+
3
+ require File.expand_path( File.dirname(__FILE__) + '/core.jar' ) # ZXing core classes
4
+ require File.expand_path( File.dirname(__FILE__) + '/javase.jar' ) # ZXing JavaSE classes
5
+
6
+ require 'uri'
7
+
8
+ # Google ZXing classes
9
+ java_import com.google.zxing.MultiFormatReader
10
+ java_import com.google.zxing.BinaryBitmap
11
+ java_import com.google.zxing.Binarizer
12
+ java_import com.google.zxing.common.GlobalHistogramBinarizer
13
+ java_import com.google.zxing.LuminanceSource
14
+ java_import com.google.zxing.client.j2se.BufferedImageLuminanceSource
15
+
16
+ # Standard Java classes
17
+ java_import javax.imageio.ImageIO
18
+ java_import java.net.URL
19
+
20
+ module ZXing
21
+
22
+ @@decoder = MultiFormatReader.new
23
+
24
+ # Transform the module into a singleton!
25
+ extend self
26
+
27
+ def decode(descriptor)
28
+ begin
29
+ decode!(descriptor)
30
+ rescue NativeException
31
+ return nil
32
+ end
33
+ end
34
+
35
+ def decode!(descriptor)
36
+ descriptor = descriptor.path if descriptor.respond_to? :path
37
+ descriptor = descriptor.to_s
38
+ descriptor = case descriptor
39
+ when URI.regexp(['http', 'https'])
40
+ URL.new(descriptor)
41
+ else
42
+ Java::JavaIO::File.new(descriptor)
43
+ end
44
+ image = ImageIO.read(descriptor)
45
+ bitmap = to_bitmap(image)
46
+ @@decoder.decode(bitmap).to_s
47
+ end
48
+
49
+ private
50
+
51
+ def to_bitmap(image)
52
+ luminance = BufferedImageLuminanceSource.new(image)
53
+ binarizer = GlobalHistogramBinarizer.new(luminance)
54
+ BinaryBitmap.new(binarizer)
55
+ end
56
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path( File.dirname(__FILE__) + '/../zxing')
2
+
3
+ module Decodable
4
+ def decode
5
+ ZXing.decode(self)
6
+ end
7
+
8
+ def decode!
9
+ ZXing.decode!(self)
10
+ end
11
+ end
data/test/qrcode.png ADDED
Binary file
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path( File.dirname(__FILE__) + '/../lib')
2
+ $LOAD_PATH.unshift File.expand_path( File.dirname(__FILE__) )
3
+
4
+ require 'shoulda'
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env jruby --headless -rubygems
2
+
3
+ require File.expand_path( File.dirname(__FILE__) + '/../test_helper')
4
+ require 'zxing/decodable'
5
+
6
+ class DecodableTest < Test::Unit::TestCase
7
+
8
+ class Object::File
9
+ include Decodable
10
+ end
11
+
12
+ class URL
13
+ include Decodable
14
+ def initialize(path)
15
+ @path = path
16
+ end
17
+ def path; @path end
18
+ end
19
+
20
+ context "A Decodable module" do
21
+ setup do
22
+ @file = File.open( File.expand_path( File.dirname(__FILE__) + '/../qrcode.png' ))
23
+ @uri = URL.new "http://2d-code.co.uk/images/bbc-logo-in-qr-code.gif"
24
+ @bad_uri = URL.new "http://google.com"
25
+ end
26
+
27
+ should "provide #decode to decode the return value of #path" do
28
+ assert_equal @file.decode, ZXing.decode(@file.path)
29
+ assert_equal @uri.decode, ZXing.decode(@uri.path)
30
+ assert_nil @bad_uri.decode
31
+ end
32
+
33
+ should "provide #decode! as well" do
34
+ assert_equal @file.decode!, ZXing.decode(@file.path)
35
+ assert_equal @uri.decode!, ZXing.decode(@uri.path)
36
+ assert_raise(NativeException) { @bad_uri.decode! }
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env jruby --headless -rubygems
2
+
3
+ require File.expand_path( File.dirname(__FILE__) + '/test_helper')
4
+ require 'zxing'
5
+
6
+ class ZXingTest < Test::Unit::TestCase
7
+ context "A QR decoder singleton" do
8
+
9
+ class Foo < Struct.new(:v); def to_s; self.v; end; end
10
+
11
+ setup do
12
+ @decoder = ZXing
13
+ @uri = "http://2d-code.co.uk/images/bbc-logo-in-qr-code.gif"
14
+ @path = File.expand_path( File.dirname(__FILE__) + '/qrcode.png')
15
+ @file = File.new(@path)
16
+ @google_logo = "http://www.google.com/logos/grandparentsday10.gif"
17
+ @uri_result = "http://bbc.co.uk/programmes"
18
+ @path_result = "http://rubyflow.com"
19
+ end
20
+
21
+ should "decode a URL" do
22
+ assert_equal @decoder.decode(@uri), @uri_result
23
+ end
24
+
25
+ should "decode a file path" do
26
+ assert_equal @decoder.decode(@path), @path_result
27
+ end
28
+
29
+ should "return nil if #decode fails" do
30
+ assert_nil @decoder.decode(@google_logo)
31
+ end
32
+
33
+ should "raise an exception if #decode! fails" do
34
+ assert_raise(NativeException) { @decoder.decode!(@google_logo) }
35
+ end
36
+
37
+ should "decode objects that respond to #path" do
38
+ assert_equal @decoder.decode(@file), @path_result
39
+ end
40
+
41
+ should "call #to_s to argument passed in as a last resort" do
42
+ assert_equal @decoder.decode(Foo.new(@path)), @path_result
43
+ end
44
+ end
45
+
46
+ context "A QR decoder module" do
47
+
48
+ setup do
49
+ class SpyRing; include ZXing end
50
+ @ring = SpyRing.new
51
+ end
52
+
53
+ should "include #decode and #decode! into classes" do
54
+ assert_equal defined?(@ring.decode), "method"
55
+ assert_equal defined?(@ring.decode!), "method"
56
+ end
57
+
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zxing
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - ecin
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-05 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 10
30
+ - 3
31
+ version: 2.10.3
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Need to decode 1D/2D bars and don't mind using JRuby to help with the task? ZXing is the wrapper for you!
35
+ email: ecin@copypastel.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - README.textile
42
+ files:
43
+ - README.textile
44
+ - VERSION
45
+ - lib/core.jar
46
+ - lib/javase.jar
47
+ - lib/zxing.rb
48
+ - lib/zxing/decodable.rb
49
+ - test/qrcode.png
50
+ - test/test_helper.rb
51
+ - test/zxing/decodable_test.rb
52
+ - test/zxing_test.rb
53
+ has_rdoc: true
54
+ homepage: http://github.com/ecin/zxing.rb
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --charset=UTF-8
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.6
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: JRuby wrapper for ZXing 1D/2D barcode image processing library.
83
+ test_files:
84
+ - test/test_helper.rb
85
+ - test/zxing/decodable_test.rb
86
+ - test/zxing_test.rb