Class: Zint::Barcode

Inherits:
Object
  • Object
show all
Defined in:
lib/zint/barcode.rb

Overview

A base class to represent the barcode being encoded

Direct Known Subclasses

DataMatrix, Ean, QRCode, Upc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, bctype = Zint::BARCODE_CODE128, &options) ⇒ Barcode

Returns a new instance of Barcode.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/zint/barcode.rb', line 13

def initialize(value, bctype=Zint::BARCODE_CODE128, &options)
  options ||={}
  @zint_symbol = Zint::Wrapper.create(bctype)
  @bctype = bctype
  @value = value
  @encoded = false
  if options[:path] 
    @path = options[:path]
    @zint_symbol[:outfile]= @path
  else
    @path = File.join(Dir.pwd,  "out.png")
    @zint_symbol[:outfile]= @path
  end
end

Instance Attribute Details

#bctypeObject

The type of barcode generated (e.g. UPC, QRCode, Data Matrix)



7
8
9
# File 'lib/zint/barcode.rb', line 7

def bctype
  @bctype
end

#pathObject

The output file path



9
10
11
# File 'lib/zint/barcode.rb', line 9

def path
  @path
end

#valueObject

The encoded value of the barcode



5
6
7
# File 'lib/zint/barcode.rb', line 5

def value
  @value
end

#zint_symbolObject (readonly)

Access for the underlying FFI ManagedStruct of the Zint C struct



11
12
13
# File 'lib/zint/barcode.rb', line 11

def zint_symbol
  @zint_symbol
end

Instance Method Details

#buffer!Object



52
53
54
55
56
57
58
59
60
# File 'lib/zint/barcode.rb', line 52

def buffer!
  tmp = File.join(Dir.tmpdir, File.basename(@path))
  @zint_symbol[:outfile] = tmp
  print!
  @zint_symbol[:outfile] = @path
  buffer = File.read(tmp)
  File.unlink(tmp)
  return buffer
end

#encode!Object



35
36
37
38
39
40
41
42
# File 'lib/zint/barcode.rb', line 35

def encode!
  return if @encoded
  err = Zint::Wrapper.zint_encode(@zint_symbol, @value, 0)
  if Zint::ERR[:err]
    raise Zint::Exception.new(@zint_symbol[:errtxt])
  end
  @encoded = true
end

#print!Object



44
45
46
47
48
49
50
# File 'lib/zint/barcode.rb', line 44

def print!
  encode!
  err = Zint::Wrapper.zint_print(@zint_symbol, 0)
  if Zint::ERR[:err]
    raise Zint::Exception.new(@zint_symbol[:errtxt])
  end        
end