encode method

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

@param contents The contents to encode in the barcode @param format The barcode format to generate @param width The preferred width in pixels @param height The preferred height in pixels @param hints Additional parameters to supply to the encoder @return BitMatrix representing encoded barcode image @throws WriterException if contents cannot be encoded legally in a format

Implementation

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

  if (format != BarcodeFormat.qrCode) {
    throw ArgumentError('Can only encode QR_CODE, but got $format');
  }

  if (width < 0 || height < 0) {
    throw ArgumentError(
      'Requested dimensions are too small: $width x $height',
    );
  }

  ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
  int quietZone = _quietZoneSize;
  if (hints != null) {
    if (hints.errorCorrectionLevel != null) {
      errorCorrectionLevel = hints.errorCorrectionLevel!;
    } else if (hints.errorCorrection != null) {
      errorCorrectionLevel =
          ErrorCorrectionLevel.values[hints.errorCorrection!];
    }
    if (hints.margin != null) {
      quietZone = hints.margin!;
    }
  }

  final code = Encoder.encode(contents, errorCorrectionLevel, hints);
  return _renderResult(code, width, height, quietZone);
}