encode method
BitMatrix
encode(
- String contents,
- BarcodeFormat format,
- int width,
- int height, [
- Map<
EncodeHintType, Object> ? 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, [
Map<EncodeHintType, Object>? hints,
]) {
if (contents.isEmpty) {
throw ArgumentError('Found empty contents');
}
if (format != BarcodeFormat.QR_CODE) {
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 = _QUIET_ZONE_SIZE;
if (hints != null) {
if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
errorCorrectionLevel = ErrorCorrectionLevel
.values[hints[EncodeHintType.ERROR_CORRECTION] as int];
}
if (hints.containsKey(EncodeHintType.MARGIN)) {
quietZone = int.parse(hints[EncodeHintType.MARGIN].toString());
}
}
final code = Encoder.encode(contents, errorCorrectionLevel, hints);
return _renderResult(code, width, height, quietZone);
}