encode method

  1. @override
BitMatrix encode(
  1. String contents,
  2. BarcodeFormat format,
  3. int width,
  4. int height, [
  5. EncodeHint? hints,
])
override

Encode the contents following specified format. width and height are required size. This method may return bigger size BitMatrix when specified size is too small. The user can set both width and height to zero to get minimum size barcode. If negative value is set to width or height, IllegalArgumentException is thrown.

Implementation

@override
BitMatrix encode(
  String contents,
  BarcodeFormat format,
  int width,
  int height, [
  EncodeHint? hints,
]) {
  if (contents.isEmpty) {
    throw ArgumentError('Found empty contents');
  }

  if (width < 0 || height < 0) {
    throw ArgumentError(
      'Negative size is not allowed. Input: $width' 'x$height',
    );
  }
  final supportedFormats = supportedWriteFormats;
  if (supportedFormats != null && !supportedFormats.contains(format)) {
    throw ArgumentError('Can only encode $supportedFormats, but got $format');
  }

  int sidesMargin = defaultMargin;
  if (hints?.margin != null) {
    sidesMargin = hints!.margin!;
  }

  final code = encodeContent(contents, hints);
  return _renderResult(code, width, height, sidesMargin);
}