class EXIFR::JPEG
JPEG
decoder¶ ↑
Examples¶ ↑
EXIFR::JPEG.new('IMG_3422.JPG').width # -> 2272 EXIFR::JPEG.new('IMG_3422.JPG').exif.model # -> "Canon PowerShot G3"
Attributes
raw APP1 frames
comment; a string if one comment found, an array if more, otherwise nil
EXIF data if available
image height
image width
Public Class Methods
Source
# File lib/exifr/jpeg.rb, line 32 def initialize(file, load_thumbnails: true) if file.kind_of? String File.open(file, 'rb') { |io| examine(io, load_thumbnails: load_thumbnails) } else examine(file.dup, load_thumbnails: load_thumbnails) end end
file
is a filename or an IO object. Hint: use StringIO when working with slurped data like blobs.
Public Instance Methods
Source
# File lib/exifr/jpeg.rb, line 41 def exif? !exif.nil? end
Returns true
when EXIF data is available.
Source
# File lib/exifr/jpeg.rb, line 59 def method_missing(method, *args) super unless args.empty? super unless methods.include?(method) @exif.send method if defined?(@exif) && @exif end
Dispatch to EXIF. When no EXIF data is available but the method
does exist for EXIF data nil
will be returned.
Calls superclass method
Source
# File lib/exifr/jpeg.rb, line 46 def thumbnail defined?(@exif) && @exif && @exif.jpeg_thumbnails && @exif.jpeg_thumbnails.first end
Return thumbnail data when available.
Source
# File lib/exifr/jpeg.rb, line 51 def to_hash h = {:width => width, :height => height, :bits => bits, :comment => comment} h.merge!(exif) if exif? h end
Get a hash presentation of the image.
Private Instance Methods
Source
# File lib/exifr/jpeg.rb, line 98 def examine(io, load_thumbnails: true) io = Reader.new(io) unless io.getbyte == 0xFF && io.getbyte == 0xD8 # SOI raise MalformedJPEG, "no start of image marker found" end @app1s = [] while marker = io.next case marker when 0xC0..0xC3, 0xC5..0xC7, 0xC9..0xCB, 0xCD..0xCF # SOF markers length, @bits, @height, @width, components = io.readsof unless length == 8 + components * 3 raise MalformedJPEG, "frame length does not match number of components" end when 0xD9, 0xDA; break # EOI, SOS when 0xFE; (@comment ||= []) << io.readframe # COM when 0xE1; @app1s << io.readframe # APP1, may contain EXIF tag else io.readframe # ignore frame end end @comment = @comment.first if defined?(@comment) && @comment && @comment.size == 1 if app1 = @app1s.find { |d| d[0..5] == "Exif\0\0" } @exif_data = app1[6..-1] @exif = TIFF.new(StringIO.new(@exif_data), load_thumbnails: load_thumbnails) end end