#!/usr/bin/env ruby $:.push File.expand_path(File.join(File.dirname(__FILE__),"../lib")) require 'zxing' include ZXing require 'optparse' options = { :rotation => 0 } OptionParser.new do |o| o.on('-r', '--rotate rotation') do |r| options[:rotation] = r end o.on('-l', '--luminance luminance_file') do |l| options[:luminance] = l end o.on('-b', '--binary binary_file') do |b| options[:binary] = b end o.on('-a', '--ascii ascii_file') do |a| options[:ascii] = a end o.on('-t', '--tryharder') do |th| options[:th] = th end o.on('-q', '--qr') do options[:qr] = true end end.parse! options.has_key? :preview or options[:preview] = options[:capture] reader = nil if !options[:qr] reader = MultiFormatReader.new end html = false ARGV.each_with_index do |uri,i| begin open(uri) do |file| if file.content_type == "text/html" html = true require 'hpricot' doc = Hpricot.parse file (doc/"img[@src]").each do |img| ARGV.insert(i+1, (URI.parse(uri)+URI.parse(img[:src])).to_s) end uri = nil end end next if uri.nil? rescue; end image = Image.read uri rotated = image.rotate options[:rotation].to_f source = LuminanceSource.new rotated if options[:luminance] source.image.write options[:luminance]+".png" end binarizer = Common::HybridBinarizer.new source if options[:binary] binarizer.image.write options[:binary]+".png" end bitmap = BinaryBitmap.new binarizer result = nil if options[:qr] detector = QRCode::Detector.new bitmap.black_matrix result = detector.detect result = QRCode::Decoder.new.decode result else begin result = reader.decode bitmap, options[:th] ? { :try_harder => true } : nil puts result.text rescue ZXing::ReaderException => re raise re if !html rescue ZXing::NotFoundException => nfe raise nfe if !html end end end