Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new method qrcode_decode #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ decodes a plethora of barcodes. Their site has a complete list.
# ZXing#decode! will raise an error if it can't decode the image.
ZXing.decode! 'image_without_a_code.png' #=> raises ZXing::UndecodableError

# You can call ZXing#qrcode_decode if you only want to decode QR Code
ZXing.qrcode_decode '/Users/ecin/qrcode.png' #=> "QRcode string"

== Decodable module

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.
Expand Down
4 changes: 4 additions & 0 deletions lib/zxing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def self.decode_all!(file)
Decoder.decode_all! normalize(file)
end

def self.qrcode_decode(file)
Decoder.qrcode_decode normalize(file)
end

private
def self.normalize(file)
file.respond_to?(:path) ? file.path : file
Expand Down
Binary file removed lib/zxing/core-3.1.1-SNAPSHOT.jar
Binary file not shown.
Binary file added lib/zxing/core-3.3.0.jar
Binary file not shown.
32 changes: 30 additions & 2 deletions lib/zxing/decoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ module ZXing
Decoder = Client.new
else
require 'java'
require 'zxing/core-3.1.1-SNAPSHOT.jar'
require 'zxing/javase-3.1.1-SNAPSHOT.jar'
require 'zxing/core-3.3.0.jar'
require 'zxing/javase-3.3.0.jar'

java_import com.google.zxing.MultiFormatReader
java_import com.google.zxing.qrcode.QRCodeReader
java_import com.google.zxing.BinaryBitmap
java_import com.google.zxing.Binarizer
java_import com.google.zxing.common.GlobalHistogramBinarizer
java_import com.google.zxing.common.HybridBinarizer
java_import com.google.zxing.LuminanceSource
java_import com.google.zxing.client.j2se.BufferedImageLuminanceSource
java_import com.google.zxing.multi.GenericMultipleBarcodeReader
Expand Down Expand Up @@ -47,6 +49,10 @@ def self.decode_all(file)
[]
end

def self.qrcode_decode(file)
new(file).qrcode_decode
end

def initialize(file)
self.file = file
end
Expand All @@ -59,6 +65,10 @@ def decode
reader.decode(bitmap).to_s
end

def qrcode_decode
qr_decode(bitmap) || qr_decode(hybrid_bitmap)
end

def decode_all
multi_barcode_reader = GenericMultipleBarcodeReader.new(reader)

Expand Down Expand Up @@ -93,6 +103,24 @@ def luminance
def binarizer
GlobalHistogramBinarizer.new(luminance)
end

def hybrid_binarizer
HybridBinarizer.new(luminance)
end

def hybrid_bitmap
BinaryBitmap.new(hybrid_binarizer)
end

def qr_decode(bitmap)
qrcode_reader.decode(bitmap).to_s
rescue NativeException
nil
end

def qrcode_reader
@qrcode_reader ||= QRCodeReader.new
end
end
end
end
Binary file removed lib/zxing/javase-3.1.1-SNAPSHOT.jar
Binary file not shown.
Binary file added lib/zxing/javase-3.3.0.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/zxing/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ZXing
VERSION = "0.4.0"
VERSION = "0.4.1"
end
Binary file added spec/fixtures/img-0001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/fixtures/img-0002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/fixtures/img-0003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/fixtures/img-0004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/fixtures/img-0005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions spec/zxing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,32 @@ def path
end
end

describe ".qrcode_decode" do
subject { ZXing.qrcode_decode(file) }

context "with image1" do
let(:file) { File.new(fixture_image("img-0001")) }
it { should == "SDQI:{\"name\":\"Miss Punniya teston120416\",\"ess_id\":\"punniya prabhu\",\"tag\":\"General\"}" }
end

context "with image2" do
let(:file) { File.new(fixture_image("img-0002")) }
it { should == "SDQI:{\"job seeker name\":\"Tom Gouke\",\"id\":20012,\"tag\":\"abcdefgababcdefgababcdefgababcdefgab1234abcdefgababcdefgababcdefgababcdefgab1234abcdefgababcdefgababcdefgababcdefgab1234abcdefgababcdefgababcdefgababcdefgab1234\"}" }
end

context "with image3" do
let(:file) { File.new(fixture_image("img-0003")) }
it { should == "SDQI:{\"name\":\"Miss Punniya teston120416\",\"ess_id\":\"punniya prabhu\",\"tag\":\"General\"}" }
end

context "with image4" do
let(:file) { File.new(fixture_image("img-0004")) }
it { should == "SDQI:{\"name\":\"Miss test name long long\",\"ess_id\":101016,\"tag\":\"test tags is very long very long very long very long\"}" }
end

context "with image5" do
let(:file) { File.new(fixture_image("img-0005")) }
it { should == "SDQI:{\"name\":\"Able Seaman pei han\",\"ess_id\":\"111\",\"tag\":\"resume1\"}" }
end
end
end