Permalink
Browse files

Proposed fix for Issue #836 - height and width parameters ignored whe…

…n 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
  • Loading branch information...
kent92 authored and srowen committed Dec 28, 2017
1 parent 6e04f9b commit 1861cf4f013d7b2ffede653211e3aadacd8234d5
Showing with 32 additions and 14 deletions.
  1. +32 −14 core/src/main/java/com/google/zxing/datamatrix/DataMatrixWriter.java
@@ -55,7 +55,7 @@ public BitMatrix encode(String contents, BarcodeFormat format, int width, int he
}
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 BitMatrix encode(String contents, BarcodeFormat format, int width, int he
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 BitMatrix encode(String contents, BarcodeFormat format, int width, int he
* @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 @@ private static BitMatrix encodeLowLevel(DefaultPlacement placement, SymbolInfo s
}
}
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);
}
}
}

0 comments on commit 1861cf4

Please sign in to comment.