Sha256: 351f94133d2a86ebe165c10dc1f2db0c6354726a33a7dcc6ccc292a0c81f5700

Contents?: true

Size: 1.91 KB

Versions: 5

Compression:

Stored size: 1.91 KB

Contents

#!/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

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
zxing_cpp_mac_big_sur-0.1.1 bin/zxd
zxing_cpp_no_cmake-0.1.2 bin/zxd
zxing_cpp_no_cmake-0.1.1 bin/zxd
zxing_cpp-0.1.1 bin/zxd
zxing_cpp-0.1.0 bin/zxd