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.DATA_MATRIX) {
throw ArgumentError('Can only encode DATA_MATRIX, but got $format');
}
if (width < 0 || height < 0) {
throw ArgumentError(
"Requested dimensions can't be negative: $width" 'x$height',
);
}
// Try to get force shape & min / max size
SymbolShapeHint shape = SymbolShapeHint.FORCE_NONE;
Dimension? minSize;
Dimension? maxSize;
if (hints != null) {
final requestedShape =
hints[EncodeHintType.DATA_MATRIX_SHAPE] as SymbolShapeHint?;
if (requestedShape != null) {
shape = requestedShape;
}
// ignore: deprecated_member_use_from_same_package
final requestedMinSize = hints[EncodeHintType.MIN_SIZE] as Dimension?;
if (requestedMinSize != null) {
minSize = requestedMinSize;
}
// ignore: deprecated_member_use_from_same_package
final requestedMaxSize = hints[EncodeHintType.MAX_SIZE] as Dimension?;
if (requestedMaxSize != null) {
maxSize = requestedMaxSize;
}
}
//1. step: Data encodation
String encoded;
final hasCompactionHint = hints != null &&
hints.containsKey(EncodeHintType.DATA_MATRIX_COMPACT) &&
(hints[EncodeHintType.DATA_MATRIX_COMPACT] as bool);
if (hasCompactionHint) {
final hasGS1FormatHint = hints.containsKey(EncodeHintType.GS1_FORMAT) &&
(hints[EncodeHintType.GS1_FORMAT] as bool);
Encoding? charset;
final hasEncodingHint = hints.containsKey(EncodeHintType.CHARACTER_SET);
if (hasEncodingHint) {
charset = (hints[EncodeHintType.CHARACTER_SET] as Encoding?);
}
encoded = MinimalEncoder.encodeHighLevel(
contents,
charset,
hasGS1FormatHint ? 0x1D : -1,
shape,
);
} else {
encoded =
HighLevelEncoder.encodeHighLevel(contents, shape, minSize, maxSize);
}
final symbolInfo =
SymbolInfo.lookup(encoded.length, shape, minSize, maxSize, true);
//2. step: ECC generation
final codewords = ErrorCorrection.encodeECC200(encoded, symbolInfo!);
//3. step: Module placement in Matrix
final placement = DefaultPlacement(
codewords,
symbolInfo.symbolDataWidth,
symbolInfo.symbolDataHeight,
);
placement.place();
//4. step: low-level encoding
return _encodeLowLevel(placement, symbolInfo, width, height);
}