Proposed fix for Issue #836 - height and width parameters ignored when barcode_format is DATA_MATRIX (#933)

* Proposed fix for Issue #836 - height and width parameters ignored when barcode_format is DATA_MATRIX

* Adding requested changes for PR #933

* Wording change in Javadoc to make the description more sensible
This commit is contained in:
Lakshmikant Avasarala 2017-12-28 07:42:17 -08:00 committed by Sean Owen
parent 6e04f9b3bd
commit 1861cf4f01

View file

@ -55,7 +55,7 @@ public final class DataMatrixWriter implements Writer {
} }
if (width < 0 || height < 0) { if (width < 0 || height < 0) {
throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height); throw new IllegalArgumentException("Requested dimensions can't be negative: " + width + 'x' + height);
} }
// Try to get force shape & min / max size // Try to get force shape & min / max size
@ -89,12 +89,11 @@ public final class DataMatrixWriter implements Writer {
String codewords = ErrorCorrection.encodeECC200(encoded, symbolInfo); String codewords = ErrorCorrection.encodeECC200(encoded, symbolInfo);
//3. step: Module placement in Matrix //3. step: Module placement in Matrix
DefaultPlacement placement = DefaultPlacement placement = new DefaultPlacement(codewords, symbolInfo.getSymbolDataWidth(), symbolInfo.getSymbolDataHeight());
new DefaultPlacement(codewords, symbolInfo.getSymbolDataWidth(), symbolInfo.getSymbolDataHeight());
placement.place(); placement.place();
//4. step: low-level encoding //4. step: low-level encoding
return encodeLowLevel(placement, symbolInfo); return encodeLowLevel(placement, symbolInfo, width, height);
} }
/** /**
@ -104,7 +103,7 @@ public final class DataMatrixWriter implements Writer {
* @param symbolInfo The symbol info to encode. * @param symbolInfo The symbol info to encode.
* @return The bit matrix generated. * @return The bit matrix generated.
*/ */
private static BitMatrix encodeLowLevel(DefaultPlacement placement, SymbolInfo symbolInfo) { private static BitMatrix encodeLowLevel(DefaultPlacement placement, SymbolInfo symbolInfo, int width, int height) {
int symbolWidth = symbolInfo.getSymbolDataWidth(); int symbolWidth = symbolInfo.getSymbolDataWidth();
int symbolHeight = symbolInfo.getSymbolDataHeight(); int symbolHeight = symbolInfo.getSymbolDataHeight();
@ -150,26 +149,45 @@ public final class DataMatrixWriter implements Writer {
} }
} }
return convertByteMatrixToBitMatrix(matrix); return convertByteMatrixToBitMatrix(matrix, width, height);
} }
/** /**
* Convert the ByteMatrix to BitMatrix. * Convert the ByteMatrix to BitMatrix.
* *
* @param reqHeight The requested height of the image (in pixels) with the Datamatrix code
* @param reqWidth The requested width of the image (in pixels) with the Datamatrix code
* @param matrix The input matrix. * @param matrix The input matrix.
* @return The output matrix. * @return The output matrix.
*/ */
private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix) { private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix, int reqWidth, int reqHeight) {
int matrixWidgth = matrix.getWidth(); int matrixWidth = matrix.getWidth();
int matrixHeight = matrix.getHeight(); int matrixHeight = matrix.getHeight();
int outputWidth = Math.max(reqWidth, matrixWidth);
int outputHeight = Math.max(reqHeight, matrixHeight);
int multiple = Math.min(outputWidth / matrixWidth, outputHeight / matrixHeight);
int leftPadding = (outputWidth - (matrixWidth * multiple)) / 2 ;
int topPadding = (outputHeight - (matrixHeight * multiple)) / 2 ;
BitMatrix output;
// remove padding if requested width and height are too small
if (reqHeight < matrixHeight || reqWidth < matrixWidth) {
leftPadding = 0;
topPadding = 0;
output = new BitMatrix(matrixWidth, matrixHeight);
} else {
output = new BitMatrix(reqWidth, reqHeight);
}
BitMatrix output = new BitMatrix(matrixWidgth, matrixHeight);
output.clear(); output.clear();
for (int i = 0; i < matrixWidgth; i++) { for (int inputY = 0, outputY = topPadding; inputY < matrixHeight; inputY++, outputY += multiple) {
for (int j = 0; j < matrixHeight; j++) { // Write the contents of this row of the bytematrix
// Zero is white in the bytematrix for (int inputX = 0, outputX = leftPadding; inputX < matrixWidth; inputX++, outputX += multiple) {
if (matrix.get(i, j) == 1) { if (matrix.get(inputX, inputY) == 1) {
output.set(i, j); output.setRegion(outputX, outputY, multiple, multiple);
} }
} }
} }