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) {
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
@ -89,12 +89,11 @@ public final class DataMatrixWriter implements Writer {
String codewords = ErrorCorrection.encodeECC200(encoded, symbolInfo);
//3. step: Module placement in Matrix
DefaultPlacement placement =
new DefaultPlacement(codewords, symbolInfo.getSymbolDataWidth(), symbolInfo.getSymbolDataHeight());
DefaultPlacement placement = new DefaultPlacement(codewords, symbolInfo.getSymbolDataWidth(), symbolInfo.getSymbolDataHeight());
placement.place();
//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.
* @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 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.
*
* @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.
* @return The output matrix.
*/
private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix) {
int matrixWidgth = matrix.getWidth();
private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix, int reqWidth, int reqHeight) {
int matrixWidth = matrix.getWidth();
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();
for (int i = 0; i < matrixWidgth; i++) {
for (int j = 0; j < matrixHeight; j++) {
// Zero is white in the bytematrix
if (matrix.get(i, j) == 1) {
output.set(i, j);
for (int inputY = 0, outputY = topPadding; inputY < matrixHeight; inputY++, outputY += multiple) {
// Write the contents of this row of the bytematrix
for (int inputX = 0, outputX = leftPadding; inputX < matrixWidth; inputX++, outputX += multiple) {
if (matrix.get(inputX, inputY) == 1) {
output.setRegion(outputX, outputY, multiple, multiple);
}
}
}