Skip to content

Commit

Permalink
Refactor to use rspec, bundler, proper namespace
Browse files Browse the repository at this point in the history
Also, remove decodable module
  • Loading branch information
jgdavey committed Feb 20, 2011
1 parent cfddf43 commit efdea01
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 164 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.rvmrc
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--colour
5 changes: 5 additions & 0 deletions Gemfile
@@ -0,0 +1,5 @@
source "http://rubygems.org"

group :test do
gem 'rspec', '~> 2.5.0'
end
19 changes: 19 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,19 @@
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.2)
rspec (2.5.0)
rspec-core (~> 2.5.0)
rspec-expectations (~> 2.5.0)
rspec-mocks (~> 2.5.0)
rspec-core (2.5.1)
rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)

PLATFORMS
java
ruby

DEPENDENCIES
rspec (~> 2.5.0)
6 changes: 6 additions & 0 deletions Rakefile
@@ -0,0 +1,6 @@
require 'rspec/core/rake_task'

desc "Run specs"
RSpec::Core::RakeTask.new(:spec)

task :default => :spec
Binary file removed lib/core.jar
Binary file not shown.
Binary file removed lib/javase.jar
Binary file not shown.
53 changes: 3 additions & 50 deletions lib/zxing.rb
@@ -1,56 +1,9 @@
raise "ZXing requires JRuby" unless defined?(JRuby)

require File.expand_path( File.dirname(__FILE__) + '/core.jar' ) # ZXing core classes
require File.expand_path( File.dirname(__FILE__) + '/javase.jar' ) # ZXing JavaSE classes

require 'uri'

# Google ZXing classes
java_import com.google.zxing.MultiFormatReader
java_import com.google.zxing.BinaryBitmap
java_import com.google.zxing.Binarizer
java_import com.google.zxing.common.GlobalHistogramBinarizer
java_import com.google.zxing.LuminanceSource
java_import com.google.zxing.client.j2se.BufferedImageLuminanceSource

# Standard Java classes
java_import javax.imageio.ImageIO
java_import java.net.URL
require 'zxing/decoder'

module ZXing

@@decoder = MultiFormatReader.new

# Transform the module into a singleton!
extend self

def decode(descriptor)
begin
decode!(descriptor)
rescue NativeException
return nil
end
end

def decode!(descriptor)
descriptor = descriptor.path if descriptor.respond_to? :path
descriptor = descriptor.to_s
descriptor = case descriptor
when URI.regexp(['http', 'https'])
URL.new(descriptor)
else
Java::JavaIO::File.new(descriptor)
end
image = ImageIO.read(descriptor)
bitmap = to_bitmap(image)
@@decoder.decode(bitmap).to_s
end

private

def to_bitmap(image)
luminance = BufferedImageLuminanceSource.new(image)
binarizer = GlobalHistogramBinarizer.new(luminance)
BinaryBitmap.new(binarizer)
def decode(file)
Decoder.new(file).decode
end
end
Binary file added lib/zxing/core.jar
Binary file not shown.
11 changes: 0 additions & 11 deletions lib/zxing/decodable.rb

This file was deleted.

44 changes: 44 additions & 0 deletions lib/zxing/decoder.rb
@@ -0,0 +1,44 @@
module ZXing
unless RUBY_PLATFORM != 'java'
require 'java'
require 'zxing/core.jar'
require 'zxing/javase.jar'

java_import com.google.zxing.MultiFormatReader
java_import com.google.zxing.BinaryBitmap
java_import com.google.zxing.Binarizer
java_import com.google.zxing.common.GlobalHistogramBinarizer
java_import com.google.zxing.LuminanceSource
java_import com.google.zxing.client.j2se.BufferedImageLuminanceSource

java_import javax.imageio.ImageIO

class Decoder
attr_accessor :file

def initialize(file)
self.file = file
end

def decode
MultiFormatReader.new.decode(bitmap).to_s
end

def bitmap
BinaryBitmap.new(binarizer)
end

def image
ImageIO.read(Java::JavaIO::File.new(file))
end

def luminance
BufferedImageLuminanceSource.new(image)
end

def binarizer
GlobalHistogramBinarizer.new(luminance)
end
end
end
end
Binary file added lib/zxing/javase.jar
Binary file not shown.
Binary file added spec/fixtures/example.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,4 @@
lib = File.expand_path('../lib', __FILE__)
$:.unshift lib unless $:.include?(lib)

require 'zxing'
10 changes: 10 additions & 0 deletions spec/zxing_spec.rb
@@ -0,0 +1,10 @@
require 'spec_helper'

describe ZXing do
describe ".decode" do
let(:file) { File.expand_path('../fixtures/example.png', __FILE__) }
it "decodes png files" do
ZXing.decode(file).should == "example"
end
end
end
Binary file removed test/qrcode.png
Binary file not shown.
4 changes: 0 additions & 4 deletions test/test_helper.rb

This file was deleted.

40 changes: 0 additions & 40 deletions test/zxing/decodable_test.rb

This file was deleted.

59 changes: 0 additions & 59 deletions test/zxing_test.rb

This file was deleted.

0 comments on commit efdea01

Please sign in to comment.