Method: ZXing::RMagick::Image.read

Defined in:
lib/zxing/rmagick/image.rb

.read(argument) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zxing/rmagick/image.rb', line 15

def self.read argument
  img = nil

  if argument.is_a? String
    if argument.encoding.name == Encoding.aliases['BINARY']
      begin
        img = Magick::Image.from_blob(argument)[0]
      rescue Exception => e
        # Because 'BINARY' is just an alias for ASCII-8BIT, if treating the
        # argument as image blob failed, we should continue on and try treat
        # the argument like a regular string.
      end
    end
  end

  if img.nil?
    require 'uri'
    require 'pathname'

    uri = URI.parse(argument.respond_to?(:path) ? argument.path : argument.to_s)

    if uri.scheme.nil?
      uri.scheme = "file"
      uri.path = Pathname.new(uri.path).realpath.to_s
    end

    begin
      img = case uri.scheme
              when "file"; Magick::Image.read(uri.path)[0]
              else; Magick::Image.from_blob(fetch(uri).body)[0]
            end
    rescue Exception => e
      raise ZXing::BadImageException.new e.message
    end
  end

  # p Magick::Magick_version
  # p img.colorspace
  img.colorspace = Magick::RGBColorspace
  # p img.colorspace

  if false
    (8..8).each do |j|
      (8..8).each do |i|
        p i,j
        pixel = img.pixel_color(i,j)
        r = pixel.red&0xff
        g = pixel.green&0xff
        b = pixel.blue&0xff
        p ['%x'%r, '%x'%g, '%x'%b]
        p [r, g, b]
        p ((306 * (r) + 601 * (g) + 117 * (b) + (1 << 9)) >> 10)
        p img.export_pixels_to_str(i, j, 1, 1, "I").ord
        p "x"
      end
    end
  end

  self.new img
end