Wrote a Matrix class and fixed all uses of it, as well as other small fixes like StringBuffer. MaskUtil and QRCode now compile with no errors.

git-svn-id: https://zxing.googlecode.com/svn/trunk@697 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
dswitkin 2008-11-13 21:18:56 +00:00
parent 8666ac2f8f
commit 6f4898c68f
5 changed files with 229 additions and 174 deletions

View file

@ -325,8 +325,7 @@ public final class Encoder {
&final_bits);
// Step 7: Choose the mask pattern and set to "qr_code".
QRCodeMatrix* matrix = new QRCodeMatrix(qr_code.matrix_width(),
qr_code.matrix_width());
Matrix matrix = new Matrix(qr_code.matrix_width(), qr_code.matrix_width());
qr_code.set_mask_pattern(ChooseMaskPattern(final_bits,
qr_code.ec_level(),
qr_code.version(),
@ -398,22 +397,22 @@ public final class Encoder {
}
private static int ChooseMaskPattern(final BitVector &bits, int ec_level, int version,
QRCodeMatrix *matrix) {
Matrix matrix) {
if (!QRCode.IsValidMatrixWidth(matrix.width())) {
Debug.LOG_ERROR("Invalid matrix width: " + matrix.width());
return -1;
}
Debug.LOG_ERROR("Invalid matrix width: " + matrix.width());
return -1;
}
int min_penalty = INT_MAX; // Lower penalty is better.
int min_penalty = Integer.MAX_VALUE; // Lower penalty is better.
int best_mask_pattern = -1;
// We try all mask patterns to choose the best one.
for (int i = 0; i < kNumMaskPatterns; ++i) {
final int mask_pattern = i;
if (!MatrixUtil.BuildMatrix(bits, ec_level, version,
mask_pattern, matrix)) {
return -1;
}
final int penalty = MaskUtil.CalculateMaskPenalty(*matrix);
mask_pattern, matrix)) {
return -1;
}
final int penalty = MaskUtil.CalculateMaskPenalty(matrix);
Debug.LOG_INFO("mask_pattern: " + mask_pattern + ", " + "penalty: " + penalty);
if (penalty < min_penalty) {
min_penalty = penalty;

View file

@ -16,18 +16,16 @@
package com.google.zxing.qrcode.encoder;
// #include "util/array/array2d-inl.h"
/**
* @author satorux@google.com (Satoru Takabayashi) - creator
* @author dswitkin@google.com (Daniel Switkin) - ported from C++
*/
public final class MaskUtil {
// The mask penalty calculation is complicated. See Table 21 of
// JISX0510:2004 (p.45) for details. Basically it applies four
// rules and summate all penalties.
public static int CalculateMaskPenalty(final QRCodeMatrix &matrix) {
public static int CalculateMaskPenalty(final Matrix matrix) {
int penalty = 0;
penalty += ApplyMaskPenaltyRule1(matrix);
penalty += ApplyMaskPenaltyRule2(matrix);
@ -39,7 +37,7 @@ public final class MaskUtil {
// Apply mask penalty rule 1 and return the penalty.
// Find repetitive cells with the same color and give penalty to
// them. Example: 00000 or 11111.
public static int ApplyMaskPenaltyRule1(final QRCodeMatrix &matrix) {
public static int ApplyMaskPenaltyRule1(final Matrix matrix) {
final int penalty = (ApplyMaskPenaltyRule1Internal(matrix, true) +
ApplyMaskPenaltyRule1Internal(matrix, false));
Debug.LOG_INFO("\tApplyMaskPenaltyRule1: " + penalty);
@ -48,13 +46,16 @@ public final class MaskUtil {
// Apply mask penalty rule 2 and return the penalty.
// Find 2x2 blocks with the same color and give penalty to them.
public static int ApplyMaskPenaltyRule2(final QRCodeMatrix &matrix) {
//
// JAVAPORT: Consider using Matrix.getArray() instead.
public static int ApplyMaskPenaltyRule2(final Matrix matrix) {
int penalty = 0;
for (int y = 0; y < matrix.height() - 1; ++y) {
for (int x = 0; x < matrix.width() - 1; ++x) {
if (matrix(y, x) == matrix(y + 0, x + 1) &&
matrix(y, x) == matrix(y + 1, x + 0) &&
matrix(y, x) == matrix(y + 1, x + 1)) {
int value = matrix.get(y, x);
if (value == matrix.get(y + 0, x + 1) &&
value == matrix.get(y + 1, x + 0) &&
value == matrix.get(y + 1, x + 1)) {
penalty += 3;
}
}
@ -67,49 +68,52 @@ public final class MaskUtil {
// Find consecutive cells of 00001011101 or 10111010000, and give
// penalty to them. If we find patterns like 000010111010000, we give
// penalties twice (i.e. 40 * 2).
public static int ApplyMaskPenaltyRule3(final QRCodeMatrix &matrix) {
//
// JAVAPORT: This many calls to Matrix.get() looks expensive. We should profile and consider
// adding a byte[][] Matrix.getArray() method, then using that array locally.
public static int ApplyMaskPenaltyRule3(final Matrix matrix) {
int penalty = 0;
for (int y = 0; y < matrix.height(); ++y) {
for (int x = 0; x < matrix.width(); ++x) {
// Tried to simplify following conditions but failed.
if (x + 6 < matrix.width() &&
matrix(y, x + 0) == 1 &&
matrix(y, x + 1) == 0 &&
matrix(y, x + 2) == 1 &&
matrix(y, x + 3) == 1 &&
matrix(y, x + 4) == 1 &&
matrix(y, x + 5) == 0 &&
matrix(y, x + 6) == 1 &&
matrix.get(y, x + 0) == 1 &&
matrix.get(y, x + 1) == 0 &&
matrix.get(y, x + 2) == 1 &&
matrix.get(y, x + 3) == 1 &&
matrix.get(y, x + 4) == 1 &&
matrix.get(y, x + 5) == 0 &&
matrix.get(y, x + 6) == 1 &&
((x + 10 < matrix.width() &&
matrix(y, x + 7) == 0 &&
matrix(y, x + 8) == 0 &&
matrix(y, x + 9) == 0 &&
matrix(y, x + 10) == 0) ||
matrix.get(y, x + 7) == 0 &&
matrix.get(y, x + 8) == 0 &&
matrix.get(y, x + 9) == 0 &&
matrix.get(y, x + 10) == 0) ||
(x - 4 >= 0 &&
matrix(y, x - 1) == 0 &&
matrix(y, x - 2) == 0 &&
matrix(y, x - 3) == 0 &&
matrix(y, x - 4) == 0))) {
matrix.get(y, x - 1) == 0 &&
matrix.get(y, x - 2) == 0 &&
matrix.get(y, x - 3) == 0 &&
matrix.get(y, x - 4) == 0))) {
penalty += 40;
}
if (y + 6 < matrix.height() &&
matrix(y + 0, x) == 1 &&
matrix(y + 1, x) == 0 &&
matrix(y + 2, x) == 1 &&
matrix(y + 3, x) == 1 &&
matrix(y + 4, x) == 1 &&
matrix(y + 5, x) == 0 &&
matrix(y + 6, x) == 1 &&
matrix.get(y + 0, x) == 1 &&
matrix.get(y + 1, x) == 0 &&
matrix.get(y + 2, x) == 1 &&
matrix.get(y + 3, x) == 1 &&
matrix.get(y + 4, x) == 1 &&
matrix.get(y + 5, x) == 0 &&
matrix.get(y + 6, x) == 1 &&
((y + 10 < matrix.height() &&
matrix(y + 7, x) == 0 &&
matrix(y + 8, x) == 0 &&
matrix(y + 9, x) == 0 &&
matrix(y + 10, x) == 0) ||
matrix.get(y + 7, x) == 0 &&
matrix.get(y + 8, x) == 0 &&
matrix.get(y + 9, x) == 0 &&
matrix.get(y + 10, x) == 0) ||
(y - 4 >= 0 &&
matrix(y - 1, x) == 0 &&
matrix(y - 2, x) == 0 &&
matrix(y - 3, x) == 0 &&
matrix(y - 4, x) == 0))) {
matrix.get(y - 1, x) == 0 &&
matrix.get(y - 2, x) == 0 &&
matrix.get(y - 3, x) == 0 &&
matrix.get(y - 4, x) == 0))) {
penalty += 40;
}
}
@ -129,44 +133,43 @@ public final class MaskUtil {
// - 55% => 10
// - 55% => 20
// - 100% => 100
public static int ApplyMaskPenaltyRule4(final QRCodeMatrix &matrix) {
public static int ApplyMaskPenaltyRule4(final Matrix matrix) {
int num_dark_cells = 0;
for (int y = 0; y < matrix.height(); ++y) {
for (int x = 0; x < matrix.width(); ++x) {
if (matrix(y, x) == 1) {
if (matrix.get(y, x) == 1) {
num_dark_cells += 1;
}
}
}
final int num_total_cells = matrix.height() * matrix.width();
double dark_ratio = static_cast<double>(num_dark_cells) / num_total_cells;
final int penalty = abs(static_cast<int>(dark_ratio * 100 - 50)) / 5 * 10;
double dark_ratio = (double) num_dark_cells / num_total_cells;
final int penalty = Math.abs((int) (dark_ratio * 100 - 50)) / 5 * 10;
Debug.LOG_INFO("\tApplyMaskPenaltyRule4: " + penalty);
return penalty;
}
// Return the mask bit for "mask_pattern" at "x" and "y".
// See 8.8 of JISX0510:2004 for mask pattern conditions.
public static int GetDataMaskBit(final int mask_pattern,
final int x, final int y) {
public static int GetDataMaskBit(final int mask_pattern, final int x, final int y) {
Debug.DCHECK(QRCode.IsValidMaskPattern(mask_pattern));
switch (mask_pattern) {
case 0:
return (y + x) % 2 == 0;
return ((y + x) % 2 == 0) ? 1 : 0;
case 1:
return y % 2 == 0;
return (y % 2 == 0) ? 1 : 0;
case 2:
return x % 3 == 0;
return (x % 3 == 0) ? 1 : 0;
case 3:
return (y + x) % 3 == 0;
return ((y + x) % 3 == 0) ? 1 : 0;
case 4:
return ((y / 2) + (x / 3)) % 2 == 0;
return (((y / 2) + (x / 3)) % 2 == 0) ? 1 : 0;
case 5:
return ((y * x) % 2) + ((y * x) % 3) == 0;
return (((y * x) % 2) + ((y * x) % 3) == 0) ? 1 : 0;
case 6:
return (((y * x) % 2) + ((y * x) % 3)) % 2 == 0;
return ((((y * x) % 2) + ((y * x) % 3)) % 2 == 0) ? 1 : 0;
case 7:
return (((y * x) % 3) + ((y + x) % 2)) % 2 == 0;
return ((((y * x) % 3) + ((y + x) % 2)) % 2 == 0) ? 1 : 0;
default:
;
}
@ -177,24 +180,23 @@ public final class MaskUtil {
// Helper function for ApplyMaskPenaltyRule1. We need this for doing
// this calculation in both vertical and horizontal orders
// respectively.
private static int ApplyMaskPenaltyRule1Internal(final QRCodeMatrix &matrix,
boolean is_horizontal) {
private static int ApplyMaskPenaltyRule1Internal(final Matrix matrix, boolean is_horizontal) {
int penalty = 0;
int num_same_bit_cells = 0;
int prev_bit = -1;
// Horizontal mode:
// for (int i = 0; i < matrix.height(); ++i) {
// for (int j = 0; j < matrix.width(); ++j) {
// int bit = matrix(i, j);
// int bit = matrix.get(i, j);
// Vertical mode:
// for (int i = 0; i < matrix.width(); ++i) {
// for (int j = 0; j < matrix.height(); ++j) {
// int bit = matrix(j, i);
// int bit = matrix.get(j, i);
final int i_limit = is_horizontal ? matrix.height() : matrix.width();
final int j_limit = is_horizontal ? matrix.width() : matrix.height();
for (int i = 0; i < i_limit; ++i) {
for (int j = 0; j < j_limit; ++j) {
final int bit = is_horizontal ? matrix(i, j) : matrix(j, i);
final int bit = is_horizontal ? matrix.get(i, j) : matrix.get(j, i);
if (bit == prev_bit) {
num_same_bit_cells += 1;
// Found five repetitive cells with the same color (bit).

View file

@ -0,0 +1,61 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.qrcode.encoder;
/**
* A class which wraps a 2D array.
*
* JAVAPORT: I'm not happy about the argument ordering throughout the file, as I always like to have
* the horizontal component first, but this is for compatibility with the C++ code. The original
* code was a 2D array of ints, but since it only ever gets assigned zeros and ones, I'm going to
* use less memory and go with bytes.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
public final class Matrix {
private final byte[][] bytes;
private final int height;
private final int width;
public Matrix(int height, int width) {
bytes = new byte[height][width];
this.height = height;
this.width = width;
}
public final int height() {
return height;
}
public final int width() {
return width;
}
public final byte get(int y, int x) {
return bytes[y][x];
}
public final void set(int y, int x, byte value) {
bytes[y][x] = value;
}
public final void set(int y, int x, int value) {
bytes[y][x] = (byte) value;
}
}

View file

@ -123,20 +123,20 @@ public final class MatrixUtil {
// Set all cells to -1. -1 means that the cell is empty (not set
// yet).
public static void ClearMatrix(QRCodeMatrix *matrix) {
public static void ClearMatrix(Matrix matrix) {
for (int y = 0; y < matrix.height(); ++y) {
for (int x = 0; x < matrix.width(); ++x) {
(*matrix)(y, x) = -1;
matrix.set(y, x, -1);
}
}
}
// Convert "matrix" to ASCII String for debugging.
public static String ToASCII(final QRCodeMatrix &matrix) {
String result;
public static String ToASCII(final Matrix matrix) {
StringBuffer result = new StringBuffer();
for (int y = 0; y < matrix.height(); ++y) {
for (int x = 0; x < matrix.width(); ++x) {
switch (matrix(y, x)) {
switch (matrix.get(y, x)) {
case 0:
result.append(" 0");
break;
@ -150,7 +150,7 @@ public final class MatrixUtil {
}
result.append("\n");
}
return result;
return result.toString();
}
// Build 2D matrix of QR Code from "data_bits" with "ec_level",
@ -160,7 +160,7 @@ public final class MatrixUtil {
int ec_level,
int version,
int mask_pattern,
QRCodeMatrix *matrix) {
Matrix matrix) {
MatrixUtil.ClearMatrix(matrix);
if (!EmbedBasicPatterns(version, matrix)) {
return false;
@ -183,8 +183,7 @@ public final class MatrixUtil {
// - Timing patterns
// - Dark dot at the left bottom corner
// - Position adjustment patterns, if need be
public static boolean EmbedBasicPatterns(int version,
QRCodeMatrix *matrix) {
public static boolean EmbedBasicPatterns(int version, Matrix matrix) {
// Let's get started with embedding big squares at corners.
EmbedPositionDetectionPatternsAndSeparators(matrix);
// Then, embed the dark dot at the left bottom corner.
@ -199,7 +198,7 @@ public final class MatrixUtil {
// Embed type information. On success, modify the matrix and return
// true. On error, return false.
public static boolean EmbedTypeInfo(int ec_level, int mask_pattern, QRCodeMatrix *matrix) {
public static boolean EmbedTypeInfo(int ec_level, int mask_pattern, Matrix matrix) {
BitVector type_info_bits;
if (!MakeTypeInfoBits(ec_level, mask_pattern, &type_info_bits)) {
return false;
@ -215,18 +214,18 @@ public final class MatrixUtil {
// See 8.9 of JISX0510:2004 (p.46).
final int x1 = kTypeInfoCoordinates[i][0];
final int y1 = kTypeInfoCoordinates[i][1];
(*matrix)(y1, x1) = bit;
matrix.set(y1, x1, bit);
if (i < 8) {
// Right top corner.
final int x2 = matrix.width() - i - 1;
final int y2 = 8;
(*matrix)(y2, x2) = bit;
matrix.set(y2, x2, bit);
} else {
// Left bottom corner.
final int x2 = 8;
final int y2 = matrix.height() - 7 + (i - 8);
(*matrix)(y2, x2) = bit;
matrix.set(y2, x2, bit);
}
}
return true;
@ -236,8 +235,7 @@ public final class MatrixUtil {
// matrix and return true. On error, return false.
// See 8.10 of JISX0510:2004 (p.47) for how to embed version
// information. Return true on success. Return false otherwise.
public static boolean MaybeEmbedVersionInfo(int version,
QRCodeMatrix *matrix) {
public static boolean MaybeEmbedVersionInfo(int version, Matrix matrix) {
if (version < 7) { // Version info is necessary if version >= 7.
return true; // Don't need version info.
}
@ -253,9 +251,9 @@ public final class MatrixUtil {
// Place bits in LSB (least significant bit) to MSB order.
final int bit = version_info_bits.at(bit_index--);
// Left bottom corner.
(*matrix)(matrix.height() - 11 + j, i) = bit;
matrix.set(matrix.height() - 11 + j, i, bit);
// Right bottom corner.
(*matrix)(i, matrix.height() - 11 + j) = bit;
matrix.set(i, matrix.height() - 11 + j, bit);
}
}
return true;
@ -265,9 +263,7 @@ public final class MatrixUtil {
// matrix and return true. On error, return false. For debugging
// purpose, it skips masking process if "mask_pattern" is -1.
// See 8.7 of JISX0510:2004 (p.38) for how to embed data bits.
public static boolean EmbedDataBits(final BitVector &data_bits,
int mask_pattern,
QRCodeMatrix *matrix) {
public static boolean EmbedDataBits(final BitVector &data_bits, int mask_pattern, Matrix matrix) {
int bit_index = 0;
int direction = -1;
// Start from the right bottom cell.
@ -282,7 +278,7 @@ public final class MatrixUtil {
for (int i = 0; i < 2; ++i) {
final int xx = x - i;
// Skip the cell if it's not empty.
if (!IsEmpty((*matrix)(y, xx))) {
if (!IsEmpty(matrix.get(y, xx))) {
continue;
}
int bit = -1;
@ -303,7 +299,7 @@ public final class MatrixUtil {
Debug.DCHECK(mask == 0 || mask == 1);
bit ^= mask;
}
(*matrix)(y, xx) = bit;
matrix.set(y, xx, bit);
}
y += direction;
}
@ -434,94 +430,86 @@ public final class MatrixUtil {
value == 1); // Dark (black).
}
private static void EmbedTimingPatterns(QRCodeMatrix *matrix) {
private static void EmbedTimingPatterns(Matrix matrix) {
// -8 is for skipping position detection patterns (size 7), and
// two horizontal/vertical separation patterns (size 1).
// Thus, 8 = 7 + 1.
for (int i = 8; i < matrix.width() - 8; ++i) {
final int bit = (i + 1) % 2;
// Horizontal line.
Debug.DCHECK(IsValidValue((*matrix)(6, i)));
if (IsEmpty((*matrix)(6, i))) {
(*matrix)(6, i) = bit;
}
Debug.DCHECK(IsValidValue(matrix.get(6, i)));
if (IsEmpty(matrix.get(6, i))) {
matrix.set(6, i, bit);
}
// Vertical line.
Debug.DCHECK(IsValidValue((*matrix)(i, 6)));
if (IsEmpty((*matrix)(i, 6))) {
(*matrix)(i, 6) = bit;
}
Debug.DCHECK(IsValidValue(matrix.get(i, 6)));
if (IsEmpty(matrix.get(i, 6))) {
matrix.set(i, 6, bit);
}
}
}
// Embed the lonely dark dot at left bottom corner.
// JISX0510:2004 (p.46)
private static void EmbedDarkDotAtLeftBottomCorner(
QRCodeMatrix *matrix) {
Debug.DCHECK((*matrix)(matrix.height() - 8, 8));
(*matrix)(matrix.height() - 8, 8) = 1;
private static void EmbedDarkDotAtLeftBottomCorner(Matrix matrix) {
Debug.DCHECK(matrix.get(matrix.height() - 8, 8) != 0);
matrix.set(matrix.height() - 8, 8, 1);
}
private static void EmbedHorizontalSeparationPattern(final int x_start,
final int y_start,
QRCodeMatrix *matrix) {
private static void EmbedHorizontalSeparationPattern(final int x_start, final int y_start,
Matrix matrix) {
// We know the width and height.
Debug.DCHECK_EQ(8, arraysize(kHorizontalSeparationPattern[0]));
Debug.DCHECK_EQ(1, arraysize(kHorizontalSeparationPattern));
for (int x = 0; x < 8; ++x) {
Debug.DCHECK(IsEmpty((*matrix)(y_start, x_start + x)));
(*matrix)(y_start, x_start + x) = kHorizontalSeparationPattern[0][x];
Debug.DCHECK(IsEmpty(matrix.get(y_start, x_start + x)));
matrix.set(y_start, x_start + x, kHorizontalSeparationPattern[0][x]);
}
}
private static void EmbedVerticalSeparationPattern(final int x_start,
final int y_start,
QRCodeMatrix *matrix) {
private static void EmbedVerticalSeparationPattern(final int x_start, final int y_start,
Matrix matrix) {
// We know the width and height.
Debug.DCHECK_EQ(1, arraysize(kVerticalSeparationPattern[0]));
Debug.DCHECK_EQ(7, arraysize(kVerticalSeparationPattern));
for (int y = 0; y < 7; ++y) {
Debug.DCHECK(IsEmpty((*matrix)(y_start + y, x_start)));
(*matrix)(y_start + y, x_start) = kVerticalSeparationPattern[y][0];
Debug.DCHECK(IsEmpty(matrix.get(y_start + y, x_start)));
matrix.set(y_start + y, x_start, kVerticalSeparationPattern[y][0]);
}
}
// Note that we cannot unify the function with
// EmbedPositionDetectionPattern() despite they are almost
// identical, since we cannot write a function that takes 2D arrays
// in different sizes in C/C++. We should live with the fact.
private static void EmbedPositionAdjustmentPattern(final int x_start,
final int y_start,
QRCodeMatrix *matrix) {
// Note that we cannot unify the function with EmbedPositionDetectionPattern() despite they are
// almost identical, since we cannot write a function that takes 2D arrays in different sizes in
// C/C++. We should live with the fact.
private static void EmbedPositionAdjustmentPattern(final int x_start, final int y_start,
Matrix matrix) {
// We know the width and height.
Debug.DCHECK_EQ(5, arraysize(kPositionAdjustmentPattern[0]));
Debug.DCHECK_EQ(5, arraysize(kPositionAdjustmentPattern));
for (int y = 0; y < 5; ++y) {
for (int x = 0; x < 5; ++x) {
Debug.DCHECK(IsEmpty((*matrix)(y_start + y, x_start + x)));
(*matrix)(y_start + y, x_start + x) =
kPositionAdjustmentPattern[y][x];
Debug.DCHECK(IsEmpty(matrix.get(y_start + y, x_start + x)));
matrix.set(y_start + y, x_start + x, kPositionAdjustmentPattern[y][x]);
}
}
}
private static void EmbedPositionDetectionPattern(final int x_start,
final int y_start,
QRCodeMatrix *matrix) {
private static void EmbedPositionDetectionPattern(final int x_start, final int y_start,
Matrix matrix) {
// We know the width and height.
Debug.DCHECK_EQ(7, arraysize(kPositionDetectionPattern[0]));
Debug.DCHECK_EQ(7, arraysize(kPositionDetectionPattern));
for (int y = 0; y < 7; ++y) {
for (int x = 0; x < 7; ++x) {
Debug.DCHECK(IsEmpty((*matrix)(y_start + y, x_start + x)));
(*matrix)(y_start + y, x_start + x) =
kPositionDetectionPattern[y][x];
Debug.DCHECK(IsEmpty(matrix.get(y_start + y, x_start + x)));
matrix.set(y_start + y, x_start + x, kPositionDetectionPattern[y][x]);
}
}
}
// Embed position detection patterns and surrounding
// vertical/horizontal separators.
private static void EmbedPositionDetectionPatternsAndSeparators(QRCodeMatrix *matrix) {
private static void EmbedPositionDetectionPatternsAndSeparators(Matrix matrix) {
// Embed three big squares at corners.
final int pdp_width = arraysize(kPositionDetectionPattern[0]);
// Left top corner.
@ -553,8 +541,7 @@ public final class MatrixUtil {
}
// Embed position adjustment patterns if need be.
private static void MaybeEmbedPositionAdjustmentPatterns(final int version,
QRCodeMatrix *matrix) {
private static void MaybeEmbedPositionAdjustmentPatterns(final int version, Matrix matrix) {
if (version < 2) { // The patterns appear if version >= 2
return;
}
@ -572,11 +559,11 @@ public final class MatrixUtil {
}
// If the cell is unset, we embed the position adjustment
// pattern here.
if (IsEmpty((*matrix)(y, x))) {
// -2 is necessary since the x/y coordinates point to the
// center of the pattern, not the left top corner.
EmbedPositionAdjustmentPattern(x - 2, y - 2, matrix);
}
if (IsEmpty(matrix.get(y, x))) {
// -2 is necessary since the x/y coordinates point to the
// center of the pattern, not the left top corner.
EmbedPositionAdjustmentPattern(x - 2, y - 2, matrix);
}
}
}
}

View file

@ -16,12 +16,6 @@
package com.google.zxing.qrcode.encoder;
// JAVAPORT: QRCodeMatrix needs to be renamed Matrix and built as a new class.
//
// template <typename T> class Array2D;
// typedef Array2D<int> QRCodeMatrix;
// #include "util/array/array2d-inl.h"
/**
* @author satorux@google.com (Satoru Takabayashi) - creator
* @author dswitkin@google.com (Daniel Switkin) - ported from C++
@ -55,7 +49,7 @@ public final class QRCode {
private int num_data_bytes_;
private int num_ec_bytes_;
private int num_rs_blocks_;
private QRCodeMatrix *matrix_;
private Matrix matrix_;
// They call encoding "mode". The modes are defined in 8.3 of JISX0510:2004 (p.14). It's unlikely
@ -125,7 +119,7 @@ public final class QRCode {
// Number of Reedsolomon blocks in the QR Code.
public int num_rs_blocks() { return num_rs_blocks_; }
// Matrix data of the QR Code.
public final QRCodeMatrix* matrix() { return matrix_; }
public final Matrix matrix() { return matrix_; }
// Return the value of the module (cell) pointed by "x" and "y" in
// the matrix of the QR Code. They call cells in the matrix
@ -143,12 +137,14 @@ public final class QRCode {
//
public int at(int x, int y) {
// The value must be zero or one.
Debug.DCHECK((*matrix_)(y, x) == 0 || (*matrix_)(y, x) == 1);
return (*matrix_)(y, x);
int value = matrix_.get(y, x);
Debug.DCHECK(value == 0 || value == 1);
return value;
}
// Checks all the member variables are set properly. Returns true
// on success. Otherwise, returns false.
// Checks all the member variables are set properly. Returns true on success. Otherwise, returns
// false.
// JAVAPORT: Do not call EverythingIsBinary(matrix_) here as it is very expensive.
public boolean IsValid() {
return (
// First check if all version are not uninitialized.
@ -173,31 +169,39 @@ public final class QRCode {
matrix_width_ == matrix_.width() &&
// See 7.3.1 of JISX0510:2004 (p.5).
matrix_width_ == kMinMatrixWidth + (version_ - 1) * 4 &&
matrix_.width() == matrix_.height() && // Must be square.
EverythingIsBinary(*matrix_));
matrix_.width() == matrix_.height()); // Must be square.
}
// Return debug String.
public String DebugString() {
String result;
StringAppendF(&result, "<<QRCode\n");
StringAppendF(&result, " mode: %s\n", ModeToString(mode_));
StringAppendF(&result, " ec_level: %s\n", ECLevelToString(ec_level_));
StringAppendF(&result, " version: %d\n", version_);
StringAppendF(&result, " matrix_width: %d\n", matrix_width_);
StringAppendF(&result, " mask_pattern: %d\n", mask_pattern_);
StringAppendF(&result, " num_total_bytes_: %d\n", num_total_bytes_);
StringAppendF(&result, " num_data_bytes: %d\n", num_data_bytes_);
StringAppendF(&result, " num_ec_bytes: %d\n", num_ec_bytes_);
StringAppendF(&result, " num_rs_blocks: %d\n", num_rs_blocks_);
StringBuffer result = new StringBuffer();
result.append("<<QRCode\n");
result.append(" mode: ");
result.append(ModeToString(mode_));
result.append("\n ec_level: ");
result.append(ECLevelToString(ec_level_));
result.append("\n version: ");
result.append(version_);
result.append("\n matrix_width: ");
result.append(matrix_width_);
result.append("\n mask_pattern: ");
result.append(mask_pattern_);
result.append("\n num_total_bytes_: ");
result.append(num_total_bytes_);
result.append("\n num_data_bytes: ");
result.append(num_data_bytes_);
result.append("\n num_ec_bytes: ");
result.append(num_ec_bytes_);
result.append("\n num_rs_blocks: ");
result.append(num_rs_blocks_);
if (matrix_ == null) {
StringAppendF(&result, " matrix: null\n");
result.append("\n matrix: null");
} else {
StringAppendF(&result, " matrix:\n%s",
MatrixUtil.ToASCII(*matrix_).c_str());
result.append("\n matrix:");
result.append(MatrixUtil.ToASCII(matrix_));
}
StringAppendF(&result, ">>\n");
return result;
result.append("\n>>\n");
return result.toString();
}
public void set_mode(int value) { mode_ = value; }
@ -211,7 +215,7 @@ public final class QRCode {
public void set_num_rs_blocks(int value) { num_rs_blocks_ = value; }
// This takes ownership of the 2D array. The 2D array will be
// deleted in the destructor of the class.
public void set_matrix(QRCodeMatrix *value) { matrix_ = value; }
public void set_matrix(Matrix value) { matrix_ = value; }
// Check if "version" is valid.
@ -334,12 +338,14 @@ public final class QRCode {
return -1;
}
// Return true if the all values in the matrix are binary numbers.
// Otherwise, return false.
private static boolean EverythingIsBinary(final Array2D<int> &matrix) {
// Return true if the all values in the matrix are binary numbers. Otherwise, return false.
// JAVAPORT: This is going to be super expensive and unnecessary, we should not call this in
// production. I'm leaving it because it may be useful for testing.
private static boolean EverythingIsBinary(final Matrix matrix) {
for (int y = 0; y < matrix.height(); ++y) {
for (int x = 0; x < matrix.width(); ++x) {
if (!(matrix(y, x) == 0 || matrix(y, x) == 1)) {
int value = matrix.get(y, x);
if (!(value == 0 || value == 1)) {
// Found non zero/one value.
return false;
}