Class: Zint::Barcode

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

Overview

Base class to represent the barcode

Examples:

Create new barcode

barcode = Zint::Barcode.new(value: "Test", symbology: Zint::BARCODE_QRCODE, option_1: 1)
barcode.to_file(path: "qr.png")

Defined Under Namespace

Classes: AlreadyGenerated

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value: nil, input_file: nil, segments: nil, symbology: Zint::BARCODE_CODE128, **kwargs) ⇒ Barcode

Returns a new instance of Barcode.

Parameters:

  • value (String, NilClass) (defaults to: nil)

    Content of the barcode

  • input_file (String, NilClass) (defaults to: nil)

    Path to input file with content of the barcode

  • segments (Array<Hash>, NilClass) (defaults to: nil)

    Array of ECI segments to encode

  • symbology (Integer) (defaults to: Zint::BARCODE_CODE128)

    Type of barcode

  • kwargs (Hash)

    Specific options for zint symbol (height, scale, …)

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/zint/barcode.rb', line 22

def initialize(value: nil, input_file: nil, segments: nil, symbology: Zint::BARCODE_CODE128, **kwargs)
  raise ArgumentError, "value, input_file or segments must be given!" if value&.empty? && input_file&.empty? && segments.nil?
  raise ArgumentError, "input_file not found!" if input_file && !File.exist?(input_file)

  @zint_symbol = Native.ZBarcode_Create
  self.symbology = symbology
  kwargs.each do |k, v|
    send(:"#{k}=", v)
  end

  @value = value
  @input_file = input_file
  encode_segments(segments)
end

Instance Attribute Details

#input_fileString, NilClass

Returns Path to input file with content of the barcode.

Returns:

  • (String, NilClass)

    Path to input file with content of the barcode



15
16
17
# File 'lib/zint/barcode.rb', line 15

def input_file
  @input_file
end

#valueString, NilClass

Returns Content of the barcode.

Returns:

  • (String, NilClass)

    Content of the barcode



13
14
15
# File 'lib/zint/barcode.rb', line 13

def value
  @value
end

Instance Method Details

#bgcolourObject

Gets background colour of barcode



254
255
256
# File 'lib/zint/barcode.rb', line 254

def bgcolour
  @zint_symbol[:bgcolour].to_s
end

#bgcolour=(bgcolour) ⇒ Object

Sets background as RGB/RGBA hexadecimal string, 6 or 8 characters, NUL-terminated

Parameters:

  • bgcolour (String)

    Background color of barcode



261
262
263
# File 'lib/zint/barcode.rb', line 261

def bgcolour=(bgcolour)
  @zint_symbol[:bgcolour] = bgcolour
end

#bitmap_heightObject

Gets bitmap height of barcode



470
471
472
# File 'lib/zint/barcode.rb', line 470

def bitmap_height
  @zint_symbol[:bitmap_height]
end

#bitmap_widthObject

Gets bitmap width of barcode



465
466
467
# File 'lib/zint/barcode.rb', line 465

def bitmap_width
  @zint_symbol[:bitmap_width]
end

#border_widthObject

Gets size of border in X-dimensions



218
219
220
# File 'lib/zint/barcode.rb', line 218

def border_width
  @zint_symbol[:border_width]
end

#border_width=(border_width) ⇒ Object

Sets size of border in X-dimensions

Parameters:

  • border_width (Integer)

    Border width of barcode



225
226
227
# File 'lib/zint/barcode.rb', line 225

def border_width=(border_width)
  @zint_symbol[:border_width] = border_width
end

#debugObject

Gets debugging flags



487
488
489
# File 'lib/zint/barcode.rb', line 487

def debug
  @zint_symbol[:debug]
end

#debug=(debug) ⇒ Object

Sets debugging flags

Parameters:

  • debug (Integer)

    Debug level of barcode



494
495
496
# File 'lib/zint/barcode.rb', line 494

def debug=(debug)
  @zint_symbol[:debug] = debug
end

#dot_sizeObject

Gets dot size of barcode



475
476
477
# File 'lib/zint/barcode.rb', line 475

def dot_size
  @zint_symbol[:dot_size]
end

#dot_size=(dot_size) ⇒ Object

Sets size of dots used in BARCODE_DOTTY_MODE

Parameters:

  • dot_size (Float)

    Dot size of barcode



482
483
484
# File 'lib/zint/barcode.rb', line 482

def dot_size=(dot_size)
  @zint_symbol[:dot_size] = dot_size
end

#dpmmObject

Gets resolution of output in dots per mm (BMP/EMF/PCX/PNG/TIF only). Default 0 (none)



346
347
348
# File 'lib/zint/barcode.rb', line 346

def dpmm
  @zint_symbol[:dpmm]
end

#dpmm=(dpmm) ⇒ Object

Sets resolution of output in dots per mm (BMP/EMF/PCX/PNG/TIF only). Default 0 (none)

Parameters:

  • dpmm (Float)

    Resolution of output in dots per mm



353
354
355
# File 'lib/zint/barcode.rb', line 353

def dpmm=(dpmm)
  @zint_symbol[:dpmm] = dpmm
end

#eciObject

Gets ECI of barcode



334
335
336
# File 'lib/zint/barcode.rb', line 334

def eci
  @zint_symbol[:eci]
end

#eci=(eci) ⇒ Object

Sets extended Channel Interpretation. Default 0 (none)

Parameters:

  • eci (Integer)

    ECI of barcode



341
342
343
# File 'lib/zint/barcode.rb', line 341

def eci=(eci)
  @zint_symbol[:eci] = eci
end

#encodeObject

Encodes the symbology without exporting to a bitmap or vector data

This method fills the output accessors of the Zint::Barcode object.

Returns:

  • self



136
137
138
139
140
141
142
143
144
145
# File 'lib/zint/barcode.rb', line 136

def encode
  if input_file
    call_function(:ZBarcode_Encode_File, @zint_symbol, input_file)
  elsif @p_segments
    call_seg_function(:ZBarcode_Encode_Segs)
  else
    call_function(:ZBarcode_Encode, @zint_symbol, value, value.bytesize)
  end
  self
end

#encoded_data_as_array_of_stringsArray<String>

Return the raw encoded data as array of strings.

Each row of the symbology is represented by one item of the array. The columns are represented by “0” and “1” characters of the stings.

Examples:

Zint::DataMatrix.new(value: "12345").encode.encoded_data_as_array_of_strings
# => ["1010101010",
#     "1101100111",
#     "1100010110",
#     "1100110101",
#     "1100111000",
#     "1000011111",
#     "1101011110",
#     "1110000111",
#     "1101100100",
#     "1111111111"]

Returns:

  • (Array<String>)

    encoded data



434
435
436
437
438
439
# File 'lib/zint/barcode.rb', line 434

def encoded_data_as_array_of_strings
  rows.times.map do |row|
    binstr = @zint_symbol[:encoded_data].to_ptr.get_bytes(144 * row, (width + 7) / 8)
    binstr.unpack1("b*")[0, width]
  end
end

#encoded_data_raw_ffiObject

Gets encoded data of barcode as internal FFI::StructLayout::CharArray object

Don’t use this method, it might be changed in future releases.



411
412
413
# File 'lib/zint/barcode.rb', line 411

def encoded_data_raw_ffi
  @zint_symbol[:encoded_data]
end

#errtxtObject

Gets error message in the event that an error occurred



460
461
462
# File 'lib/zint/barcode.rb', line 460

def errtxt
  @zint_symbol[:errtxt].to_s.force_encoding(Encoding::UTF_8)
end

#fgcolourObject

Gets foreground colour of barcode



242
243
244
# File 'lib/zint/barcode.rb', line 242

def fgcolour
  @zint_symbol[:fgcolour].to_s
end

#fgcolour=(fgcolour) ⇒ Object

Sets foreground as RGB/RGBA hexadecimal string, 6 or 8 characters, NUL-terminated

Parameters:

  • fgcolour (String)

    Foreground colour of barcode



249
250
251
# File 'lib/zint/barcode.rb', line 249

def fgcolour=(fgcolour)
  @zint_symbol[:fgcolour] = fgcolour
end

#freeObject

Free barcode and all memory associated with it.

Note: This method is dangerous insofar, that previously exported vectors (by #to_vector ) are no longer usable and any access to them will result in a segfault. It is better to not call this method and leave cleaning up to the garbage collector.



151
152
153
# File 'lib/zint/barcode.rb', line 151

def free
  @zint_symbol.pointer.free
end

#guard_descentObject

Gets height in X-dimensions that EAN/UPC guard bars descend. Default 5



358
359
360
# File 'lib/zint/barcode.rb', line 358

def guard_descent
  @zint_symbol[:guard_descent]
end

#guard_descent=(guard_descent) ⇒ Object

Sets height in X-dimensions that EAN/UPC guard bars descend. Default 5

Parameters:

  • guard_descent (Float)

    Height in X-dimensions that EAN/UPC guard bars descend



365
366
367
# File 'lib/zint/barcode.rb', line 365

def guard_descent=(guard_descent)
  @zint_symbol[:guard_descent] = guard_descent
end

#heightObject

Gets height of barcode



170
171
172
# File 'lib/zint/barcode.rb', line 170

def height
  @zint_symbol[:height]
end

#height=(height) ⇒ Object

Sets height in X-dimensions (ignored for fixed-width barcodes)

Parameters:

  • height (Float)

    Height of barcode



177
178
179
# File 'lib/zint/barcode.rb', line 177

def height=(height)
  @zint_symbol[:height] = height
end

#input_modeObject

Gets input mode of barcode



322
323
324
# File 'lib/zint/barcode.rb', line 322

def input_mode
  @zint_symbol[:input_mode]
end

#input_mode=(input_mode) ⇒ Object

Sets encoding of input data (see DATA_MODE etc below). Default DATA_MODE

Parameters:

  • input_mode (Integer)

    Input mode of barcode



329
330
331
# File 'lib/zint/barcode.rb', line 329

def input_mode=(input_mode)
  @zint_symbol[:input_mode] = input_mode
end

#option_1Object

Gets option 1 of barcode



273
274
275
# File 'lib/zint/barcode.rb', line 273

def option_1
  @zint_symbol[:option_1]
end

#option_1=(option_1) ⇒ Object

Sets option 1 (symbol-specific options (see “../docs/manual.txt”))

Parameters:

  • option_1 (Integer)

    Option 1 of barcode



280
281
282
# File 'lib/zint/barcode.rb', line 280

def option_1=(option_1)
  @zint_symbol[:option_1] = option_1
end

#option_2Object

Gets option 2 of barcode



285
286
287
# File 'lib/zint/barcode.rb', line 285

def option_2
  @zint_symbol[:option_2]
end

#option_2=(option_2) ⇒ Object

Sets option 2 (symbol-specific options) of barcode

Parameters:

  • option_2 (Integer)

    Option 2 of barcode



292
293
294
# File 'lib/zint/barcode.rb', line 292

def option_2=(option_2)
  @zint_symbol[:option_2] = option_2
end

#option_3Object

Gets option 3 of barcode



297
298
299
# File 'lib/zint/barcode.rb', line 297

def option_3
  @zint_symbol[:option_3]
end

#option_3=(option_3) ⇒ Object

Sets option 3 (symbol-specific options) of barcode

Parameters:

  • option_3 (Integer)

    Option 3 of barcode



304
305
306
# File 'lib/zint/barcode.rb', line 304

def option_3=(option_3)
  @zint_symbol[:option_3] = option_3
end

#outfileObject

Contains the name of the file to output a resulting barcode symbol to.

Must end in .png, .gif, .bmp, .emf, .eps, .pcx, .svg, .tif or .txt



268
269
270
# File 'lib/zint/barcode.rb', line 268

def outfile
  @zint_symbol[:outfile].to_s
end

#output_optionsObject

Gets output options of barcode



230
231
232
# File 'lib/zint/barcode.rb', line 230

def output_options
  @zint_symbol[:output_options]
end

#output_options=(output_options) ⇒ Object

Set various output parameters (bind, box etc, see below) of barcode

Parameters:

  • output_options (Integer)

    Output options of barcode



237
238
239
# File 'lib/zint/barcode.rb', line 237

def output_options=(output_options)
  @zint_symbol[:output_options] = output_options
end

#primaryObject

Gets primary message data for more complex symbols



397
398
399
# File 'lib/zint/barcode.rb', line 397

def primary
  @zint_symbol[:primary].to_s
end

#primary=(primary) ⇒ Object

Sets primary message data (MaxiCode, Composite), NUL-terminated of barcode

Parameters:

  • primary (String)

    Primary of barcode



404
405
406
# File 'lib/zint/barcode.rb', line 404

def primary=(primary)
  @zint_symbol[:primary] = primary
end

#row_height_raw_ffiObject

Gets row heights of barcode as internal FFI::Struct::InlineArray object

Don’t use this method, it might be changed in future releases.



444
445
446
# File 'lib/zint/barcode.rb', line 444

def row_height_raw_ffi
  @zint_symbol[:row_height]
end

#row_heightsArray<Float>

Gets heights of all barcode rows

Examples:

Zint::Kix.new(value: "130203").encode.row_heights
# => [3.0, 2.0, 3.0]

Returns:

  • (Array<Float>)

    row heights



455
456
457
# File 'lib/zint/barcode.rb', line 455

def row_heights
  @zint_symbol[:row_height].to_a[0, rows]
end

#rowsObject

Gets rows of barcode



387
388
389
# File 'lib/zint/barcode.rb', line 387

def rows
  @zint_symbol[:rows]
end

#scaleObject

Gets scale factor of barcode



182
183
184
# File 'lib/zint/barcode.rb', line 182

def scale
  @zint_symbol[:scale]
end

#scale=(scale) ⇒ Object

Sets scale factor when printing barcode

Parameters:

  • scale (Float)

    Scale of barcode



189
190
191
# File 'lib/zint/barcode.rb', line 189

def scale=(scale)
  @zint_symbol[:scale] = scale
end

#show_hrtObject

Gets show_hrt of barcode



309
310
311
# File 'lib/zint/barcode.rb', line 309

def show_hrt
  @zint_symbol[:show_hrt]
end

#show_hrt=(show_hrt) ⇒ Object

Sets show_hrt of barcode show = 1 or hide = 0 Human Readable Text. Default is show (1)

Parameters:

  • show_hrt (Integer)

    show_hrt of barcode



317
318
319
# File 'lib/zint/barcode.rb', line 317

def show_hrt=(show_hrt)
  @zint_symbol[:show_hrt] = show_hrt
end

#structappObject

Gets structured append info. Default structapp.count 0 (none)



370
371
372
# File 'lib/zint/barcode.rb', line 370

def structapp
  @zint_symbol[:structapp]
end

#structapp=(structapp) ⇒ Object

Sets structured append info. Default structapp.count 0 (none)

Parameters:



377
378
379
# File 'lib/zint/barcode.rb', line 377

def structapp=(structapp)
  @zint_symbol[:structapp] = structapp
end

#symbologyObject

Gets type of barcode



158
159
160
# File 'lib/zint/barcode.rb', line 158

def symbology
  @zint_symbol[:symbology]
end

#symbology=(type) ⇒ Object

Sets symbol to use (see BARCODE_XXX below)

Parameters:

  • type (Integer)

    Type of barcode



165
166
167
# File 'lib/zint/barcode.rb', line 165

def symbology=(type)
  @zint_symbol[:symbology] = type
end

#textObject

Human Readable Text, which usually consists of input data plus one more check digit. Uses UTF-8 formatting.



382
383
384
# File 'lib/zint/barcode.rb', line 382

def text
  @zint_symbol[:text].to_ptr.read_bytes(@zint_symbol[:text_length]).force_encoding(Encoding::UTF_8)
end

#to_bitmap(rotate_angle: 0) ⇒ Zint::Bitmap

Exports barcode to buffer

Parameters:

  • rotate_angle (Integer) (defaults to: 0)

    Rotate angle in degrees (0, 90, 180, 270)

Returns:



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/zint/barcode.rb', line 96

def to_bitmap(rotate_angle: 0)
  zint_bitmap = to_buffer(rotate_angle: rotate_angle)

  pixels = []
  @zint_symbol[:bitmap_height].times do |row|
    @zint_symbol[:bitmap_width].times do |column|
      pixels << BitmapPixel.new(column, row, zint_bitmap[pixels.size])
    end
  end

  Bitmap.new(@zint_symbol[:bitmap_width], @zint_symbol[:bitmap_height], pixels)
end

#to_buffer(rotate_angle: 0) ⇒ String

Exports barcode to buffer

Parameters:

  • rotate_angle (Integer) (defaults to: 0)

    Rotate angle in degrees (0, 90, 180, 270)

Returns:

  • (String)

    Exported barcode buffer



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/zint/barcode.rb', line 75

def to_buffer(rotate_angle: 0)
  unless @zint_symbol[:bitmap].null?
    raise AlreadyGenerated, "to_vector was already executed"
  end
  @zint_symbol[:output_options] = Zint::OUT_BUFFER_INTERMEDIATE

  if input_file
    call_function(:ZBarcode_Encode_File_and_Buffer, @zint_symbol, input_file, rotate_angle)
  elsif @p_segments
    call_seg_function(:ZBarcode_Encode_Segs_and_Buffer, rotate_angle)
  else
    call_function(:ZBarcode_Encode_and_Buffer, @zint_symbol, value, value.bytesize, rotate_angle)
  end

  @zint_symbol[:bitmap].read_bytes(@zint_symbol[:bitmap_width] * @zint_symbol[:bitmap_height])
end

#to_file(path:, rotate_angle: 0) ⇒ Object

Exports barcode to file

Parameters:

  • path (String)

    Path to export file. Contains the name of the file to output a resulting barcode symbol to. Must end in .png, .gif, .bmp, .emf, .eps, .pcx, .svg, .tif or .txt

  • rotate_angle (Integer) (defaults to: 0)

    Rotate angle in degrees (0, 90, 180, 270)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/zint/barcode.rb', line 43

def to_file(path:, rotate_angle: 0)
  unless outfile == "out.png"
    raise AlreadyGenerated, "to_file was already executed"
  end
  @zint_symbol[:outfile] = path

  if input_file
    call_function(:ZBarcode_Encode_File_and_Print, @zint_symbol, input_file, rotate_angle)
  elsif @p_segments
    call_seg_function(:ZBarcode_Encode_Segs_and_Print, rotate_angle)
  else
    call_function(:ZBarcode_Encode_and_Print, @zint_symbol, value, value.bytesize, rotate_angle)
  end
end

#to_memory_file(extension: ".png", rotate_angle: 0) ⇒ String

Exports barcode to memory file

Parameters:

  • extension (String) (defaults to: ".png")

    Extension exported memory file

  • rotate_angle (Integer) (defaults to: 0)

    Rotate angle in degrees (0, 90, 180, 270)

Returns:

  • (String)

    Exported memory file



63
64
65
66
67
68
69
# File 'lib/zint/barcode.rb', line 63

def to_memory_file(extension: ".png", rotate_angle: 0)
  @zint_symbol[:output_options] = Zint::BARCODE_MEMORY_FILE

  to_file(path: extension, rotate_angle: rotate_angle)

  @zint_symbol[:memfile].read_bytes(@zint_symbol[:memfile_size])
end

#to_vector(rotate_angle: 0) ⇒ Zint::Structs::Vector

Exports barcode as Zint vector

Parameters:

  • rotate_angle (Integer) (defaults to: 0)

    Rotate angle in degrees (0, 90, 180, 270)

Returns:



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/zint/barcode.rb', line 113

def to_vector(rotate_angle: 0)
  unless @zint_symbol[:vector].pointer.null?
    raise AlreadyGenerated, "to_vector was already executed"
  end
  if input_file
    call_function(:ZBarcode_Encode_File_and_Buffer_Vector, @zint_symbol, input_file, rotate_angle)
  elsif @p_segments
    call_seg_function(:ZBarcode_Encode_Segs_and_Buffer_Vector, rotate_angle)
  else
    call_function(:ZBarcode_Encode_and_Buffer_Vector, @zint_symbol, value, value.bytesize, rotate_angle)
  end

  v = @zint_symbol[:vector]
  # Avoid garbage collection of Symbol before Vector, since the Vector is also freed by ZBarcode_Delete()
  v.instance_variable_set(:@symbol, @zint_symbol)
  v
end

#warn_levelObject

Gets warn level of barcode



499
500
501
# File 'lib/zint/barcode.rb', line 499

def warn_level
  @zint_symbol[:warn_level]
end

#warn_level=(warn_level) ⇒ Object

Sets warn level (affects error/warning value returned by Zint API (see WARN_XXX below)) of barcode

Parameters:

  • warn_level (Integer)

    Warn level of barcode



506
507
508
# File 'lib/zint/barcode.rb', line 506

def warn_level=(warn_level)
  @zint_symbol[:warn_level] = warn_level
end

#whitespace_heightObject

Gets height in X-dimensions of whitespace above & below the barcode



206
207
208
# File 'lib/zint/barcode.rb', line 206

def whitespace_height
  @zint_symbol[:whitespace_height]
end

#whitespace_height=(whitespace_height) ⇒ Object

Sets height in X-dimensions of whitespace above & below the barcode

Parameters:

  • whitespace_height (Integer)

    Whitespace height of barcode



213
214
215
# File 'lib/zint/barcode.rb', line 213

def whitespace_height=(whitespace_height)
  @zint_symbol[:whitespace_height] = whitespace_height
end

#whitespace_widthObject

Gets width in X-dimensions of whitespace to left & right of barcode



194
195
196
# File 'lib/zint/barcode.rb', line 194

def whitespace_width
  @zint_symbol[:whitespace_width]
end

#whitespace_width=(whitespace_width) ⇒ Object

Sets width in X-dimensions of whitespace to left & right of barcode

Parameters:

  • whitespace_width (Integer)

    Whitespace width of barcode



201
202
203
# File 'lib/zint/barcode.rb', line 201

def whitespace_width=(whitespace_width)
  @zint_symbol[:whitespace_width] = whitespace_width
end

#widthObject

Gets width of barcode



392
393
394
# File 'lib/zint/barcode.rb', line 392

def width
  @zint_symbol[:width]
end