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.
-
#initialize(pointer = nil) ⇒ Image
constructor
Instantiates a new Image object, either by creating an empty one, or wrapping the supplied pointer.
-
#process(processor = nil) ⇒ Object
Attempt to recognize barcodes in this image, using the supplied processor (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.
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 |
# File 'lib/zbar/image.rb', line 19 def self.from_jpeg(io_or_string) 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.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/zbar/image.rb', line 38 def self.from_pgm(io_or_string) if io_or_string.respond_to?(:read) io_or_string = io_or_string.read end image_data = io_or_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.
83 84 85 86 87 88 89 90 |
# File 'lib/zbar/image.rb', line 83 def convert(format) ptr = ZBar.(@img, format) if ptr.null? raise ArgumentError, "conversion failed" else Image.new(ptr) end end |
#process(processor = nil) ⇒ Object
Attempt to recognize barcodes in this image, using the supplied processor (if any), falling back to defaults.
Returns an array of ZBar::Symbol objects.
96 97 98 99 |
# File 'lib/zbar/image.rb', line 96 def process(processor = nil) processor ||= Processor.new 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.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/zbar/image.rb', line 65 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 |