mirror of
https://github.com/zxing/zxing.git
synced 2024-11-14 06:54:07 -08:00
65 lines
1.7 KiB
Plaintext
65 lines
1.7 KiB
Plaintext
|
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":http://code.google.com/p/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>
|