mirror of
https://github.com/zxing/zxing.git
synced 2025-02-02 05:41:08 -08:00
Closes # 263 retire jruby wrapper
This commit is contained in:
parent
a95a8fee84
commit
0859154bc8
|
@ -1,65 +0,0 @@
|
|||
h2. Decode QR Codes
|
||||
|
||||
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 library with JRuby. ZXing conveniently decodes a plethora of barcodes. Their site has a complete list.
|
||||
|
||||
h2. Requirements
|
||||
|
||||
* JRuby (tested with 1.4.0)
|
||||
* shoulda (for testing)
|
||||
|
||||
h2. Using the ZXing module/singleton.
|
||||
|
||||
<pre>
|
||||
<code>
|
||||
require 'zxing'
|
||||
|
||||
# You can decode a URL...
|
||||
ZXing.decode 'http://2d-code.co.uk/images/bbc-logo-in-qr-code.gif'
|
||||
|
||||
# ... or a file path...
|
||||
ZXing.decode '/Users/ecin/qrcode.png'
|
||||
|
||||
# ... or a File object...
|
||||
ZXing.decode File.open('qrcode.png')
|
||||
|
||||
# ... or anything that returns a URL or file path when #path or #to_s
|
||||
# is called on it.
|
||||
class Image
|
||||
attr_reader :path
|
||||
def initialize(path); @path = path end
|
||||
end
|
||||
|
||||
image = Image.new('qrcode.png')
|
||||
ZXing.decode image
|
||||
|
||||
# #decode returns nil if it can't decode the image.
|
||||
ZXing.decode 'image_without_a_code.png'
|
||||
# => nil
|
||||
|
||||
# #decode! will raise an error if it can't decode the image.
|
||||
ZXing.decode! 'image_without_a_code.png'
|
||||
# => NativeException
|
||||
|
||||
# Feel free to include ZXing to shorten the call.
|
||||
include ZXing
|
||||
|
||||
decode 'qrcode.png'
|
||||
</code>
|
||||
</pre>
|
||||
|
||||
h2. Including the Decodable module.
|
||||
|
||||
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.
|
||||
|
||||
<pre>
|
||||
<code>
|
||||
require 'zxing/decodable'
|
||||
|
||||
class File
|
||||
include Decodable
|
||||
end
|
||||
|
||||
file = File.open('qrcode.png')
|
||||
file.decode
|
||||
</code>
|
||||
</pre>
|
|
@ -1,70 +0,0 @@
|
|||
# Copyright 2013 ZXing authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
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
|
||||
|
||||
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)
|
||||
end
|
||||
end
|
|
@ -1,25 +0,0 @@
|
|||
# Copyright 2013 ZXing authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
require File.expand_path( File.dirname(__FILE__) + '/../zxing')
|
||||
|
||||
module Decodable
|
||||
def decode
|
||||
ZXing.decode(self)
|
||||
end
|
||||
|
||||
def decode!
|
||||
ZXing.decode!(self)
|
||||
end
|
||||
end
|
Binary file not shown.
Before Width: | Height: | Size: 123 KiB |
|
@ -1,18 +0,0 @@
|
|||
# Copyright 2013 ZXing authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
$LOAD_PATH.unshift File.expand_path( File.dirname(__FILE__) + '/../lib')
|
||||
$LOAD_PATH.unshift File.expand_path( File.dirname(__FILE__) )
|
||||
|
||||
require 'shoulda'
|
|
@ -1,54 +0,0 @@
|
|||
#!/usr/bin/env jruby --headless -rubygems
|
||||
#
|
||||
# Copyright 2013 ZXing authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
require File.expand_path( File.dirname(__FILE__) + '/../test_helper')
|
||||
require 'zxing/decodable'
|
||||
|
||||
class DecodableTest < Test::Unit::TestCase
|
||||
|
||||
class Object::File
|
||||
include Decodable
|
||||
end
|
||||
|
||||
class URL
|
||||
include Decodable
|
||||
def initialize(path)
|
||||
@path = path
|
||||
end
|
||||
def path; @path end
|
||||
end
|
||||
|
||||
context "A Decodable module" do
|
||||
setup do
|
||||
@file = File.open( File.expand_path( File.dirname(__FILE__) + '/../qrcode.png' ))
|
||||
@uri = URL.new "http://2d-code.co.uk/images/bbc-logo-in-qr-code.gif"
|
||||
@bad_uri = URL.new "http://google.com"
|
||||
end
|
||||
|
||||
should "provide #decode to decode the return value of #path" do
|
||||
assert_equal @file.decode, ZXing.decode(@file.path)
|
||||
assert_equal @uri.decode, ZXing.decode(@uri.path)
|
||||
assert_nil @bad_uri.decode
|
||||
end
|
||||
|
||||
should "provide #decode! as well" do
|
||||
assert_equal @file.decode!, ZXing.decode(@file.path)
|
||||
assert_equal @uri.decode!, ZXing.decode(@uri.path)
|
||||
assert_raise(NativeException) { @bad_uri.decode! }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -1,73 +0,0 @@
|
|||
#!/usr/bin/env jruby --headless -rubygems
|
||||
|
||||
# Copyright 2013 ZXing authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
require File.expand_path( File.dirname(__FILE__) + '/test_helper')
|
||||
require 'zxing'
|
||||
|
||||
class ZXingTest < Test::Unit::TestCase
|
||||
context "A QR decoder singleton" do
|
||||
|
||||
class Foo < Struct.new(:v); def to_s; self.v; end; end
|
||||
|
||||
setup do
|
||||
@decoder = ZXing
|
||||
@uri = "http://2d-code.co.uk/images/bbc-logo-in-qr-code.gif"
|
||||
@path = File.expand_path( File.dirname(__FILE__) + '/qrcode.png')
|
||||
@file = File.new(@path)
|
||||
@google_logo = "http://www.google.com/logos/grandparentsday10.gif"
|
||||
@uri_result = "http://bbc.co.uk/programmes"
|
||||
@path_result = "http://rubyflow.com"
|
||||
end
|
||||
|
||||
should "decode a URL" do
|
||||
assert_equal @decoder.decode(@uri), @uri_result
|
||||
end
|
||||
|
||||
should "decode a file path" do
|
||||
assert_equal @decoder.decode(@path), @path_result
|
||||
end
|
||||
|
||||
should "return nil if #decode fails" do
|
||||
assert_nil @decoder.decode(@google_logo)
|
||||
end
|
||||
|
||||
should "raise an exception if #decode! fails" do
|
||||
assert_raise(NativeException) { @decoder.decode!(@google_logo) }
|
||||
end
|
||||
|
||||
should "decode objects that respond to #path" do
|
||||
assert_equal @decoder.decode(@file), @path_result
|
||||
end
|
||||
|
||||
should "call #to_s to argument passed in as a last resort" do
|
||||
assert_equal @decoder.decode(Foo.new(@path)), @path_result
|
||||
end
|
||||
end
|
||||
|
||||
context "A QR decoder module" do
|
||||
|
||||
setup do
|
||||
class SpyRing; include ZXing end
|
||||
@ring = SpyRing.new
|
||||
end
|
||||
|
||||
should "include #decode and #decode! into classes" do
|
||||
assert_equal defined?(@ring.decode), "method"
|
||||
assert_equal defined?(@ring.decode!), "method"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue