Class: ZBar::Image
- Inherits:
-
Object
- Object
- ZBar::Image
- Defined in:
- lib/zbar/image.rb
Overview
Encapsulates a ZBar Image data structure.
Class Method Summary collapse
-
.from_jpeg(io_or_string) ⇒ Object
Instantiates an Image given JPEG data.
-
.from_pgm(io_or_string) ⇒ Object
Instantiates an Image given raw PGM data.
Instance Method Summary collapse
-
#convert(format) ⇒ Object
Ask ZBar to convert this image to a new format, returning a new Image.
-
#height ⇒ Object
Retrieve the image’s height in pixels.
-
#initialize(pointer = nil) ⇒ Image
constructor
Instantiates a new Image object, either by creating an empty one, or wrapping the supplied pointer.
-
#process(processor_or_config = nil) ⇒ Object
Attempt to recognize barcodes in this image, using the supplied processor or processor configuration (if any), falling back to defaults.
-
#set_data(format, data, width = nil, height = nil) ⇒ Object
Load arbitrary data from a string into the Image object.
-
#width ⇒ Object
Retrieve the image’s width in pixels.
Constructor Details
Class Method Details
.from_jpeg(io_or_string) ⇒ Object
Instantiates an Image given JPEG data.
This function uses the internal ZBar conversion function to decode the JPEG and convert it into a greyscale image suitable for further processing. This conversion may fail if ZBar was not built with --with-jpeg
.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/zbar/image.rb', line 19 def self.from_jpeg(io_or_string) raise "JPEG support is not available in your copy of libzbar" unless ZBar::JPEG.available? ZBar::JPEG.warn_once_if_bugged if io_or_string.respond_to?(:read) io_or_string = io_or_string.read end jpeg_image = new() jpeg_image.set_data(ZBar::Format::JPEG, io_or_string) return jpeg_image.convert(ZBar::Format::Y800) end |
.from_pgm(io_or_string) ⇒ Object
Instantiates an Image given raw PGM data.
PGM is a NetPBM format, encoding width, height, and greyscale data, one byte per pixel. It is therefore ideally suited for loading into ZBar, which operates natively on Y800 pixel data–identical to the data section of a PGM file.
The data is described in greater detail at netpbm.sourceforge.net/doc/pgm.html.
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 |
# File 'lib/zbar/image.rb', line 41 def self.from_pgm(io_or_string) if io_or_string.respond_to?(:read) string = io_or_string.read else string = io_or_string end # Ensure we're in binary mode if string.respond_to? :force_encoding string.force_encoding 'binary' end image_data = string.gsub(/^(P5)\s([0-9]+)\s([0-9]+)\s([0-9]+)\s/, '') if $1 != 'P5' raise ArgumentError, "input must be a PGM file" end width, height, max_val = $2.to_i, $3.to_i, $4.to_i if max_val != 255 raise ArgumentError, "maximum value must be 255" end image = new() image.set_data(ZBar::Format::Y800, image_data, width, height) image end |
Instance Method Details
#convert(format) ⇒ Object
Ask ZBar to convert this image to a new format, returning a new Image.
Not all conversions are possible: for example, if ZBar was built without JPEG support, it cannot convert JPEGs into anything else.
93 94 95 96 97 98 99 100 |
# File 'lib/zbar/image.rb', line 93 def convert(format) ptr = ZBar.(@img, format) if ptr.null? raise ArgumentError, "conversion failed" else Image.new(ptr) end end |
#height ⇒ Object
Retrieve the image’s height in pixels
124 125 126 |
# File 'lib/zbar/image.rb', line 124 def height ZBar.(@img) end |
#process(processor_or_config = nil) ⇒ Object
Attempt to recognize barcodes in this image, using the supplied processor or processor configuration (if any), falling back to defaults.
Returns an array of ZBar::Symbol objects.
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/zbar/image.rb', line 106 def process(processor_or_config = nil) if processor_or_config.respond_to?(:process) processor = processor_or_config elsif processor_or_config.nil? processor = Processor.new else processor = Processor.new(processor_or_config) end processor.process(self) end |
#set_data(format, data, width = nil, height = nil) ⇒ Object
Load arbitrary data from a string into the Image object.
Format must be a ZBar::Format constant. See the ZBar documentation for what formats are supported, and how the data should be formatted.
Most everyone should use one of the Image.from_*
methods instead.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/zbar/image.rb', line 75 def set_data(format, data, width=nil, height=nil) ZBar.(@img, format) # Note the @buffer jog: it's to keep Ruby GC from losing the last # reference to the old @buffer before calling image_set_data. new_buffer = FFI::MemoryPointer.from_string(data) ZBar.(@img, new_buffer, data.size, nil) @buffer = new_buffer if width && height ZBar.(@img, width.to_i, height.to_i) end end |
#width ⇒ Object
Retrieve the image’s width in pixels
119 120 121 |
# File 'lib/zbar/image.rb', line 119 def width ZBar.(@img) end |