mirror of
https://github.com/zxing/zxing.git
synced 2025-02-21 02:55:27 -08:00
Issue 1325 Encoder clarifications
git-svn-id: https://zxing.googlecode.com/svn/trunk@2374 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
17882315dd
commit
5384255cae
|
@ -17,49 +17,63 @@
|
|||
package com.google.zxing.qrcode.encoder;
|
||||
|
||||
/**
|
||||
* @author satorux@google.com (Satoru Takabayashi) - creator
|
||||
* @author dswitkin@google.com (Daniel Switkin) - ported from C++
|
||||
* @author Satoru Takabayashi
|
||||
* @author Daniel Switkin
|
||||
* @author Sean Owen
|
||||
*/
|
||||
final class MaskUtil {
|
||||
|
||||
// Penalty weights from section 6.8.2.1
|
||||
private static final int N1 = 3;
|
||||
private static final int N2 = 3;
|
||||
private static final int N3 = 40;
|
||||
private static final int N4 = 10;
|
||||
|
||||
private MaskUtil() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
// 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.
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
static int applyMaskPenaltyRule1(ByteMatrix matrix) {
|
||||
return applyMaskPenaltyRule1Internal(matrix, true) + applyMaskPenaltyRule1Internal(matrix, false);
|
||||
}
|
||||
|
||||
// Apply mask penalty rule 2 and return the penalty. Find 2x2 blocks with the same color and give
|
||||
// penalty to them.
|
||||
/**
|
||||
* Apply mask penalty rule 2 and return the penalty. Find 2x2 blocks with the same color and give
|
||||
* penalty to them. This is actually equivalent to the spec's rule, which is to find MxN blocks and give a
|
||||
* penalty proportional to (M-1)x(N-1), because this is the number of 2x2 blocks inside such a block.
|
||||
*/
|
||||
static int applyMaskPenaltyRule2(ByteMatrix matrix) {
|
||||
int penalty = 0;
|
||||
byte[][] array = matrix.getArray();
|
||||
int width = matrix.getWidth();
|
||||
int height = matrix.getHeight();
|
||||
for (int y = 0; y < height - 1; ++y) {
|
||||
for (int x = 0; x < width - 1; ++x) {
|
||||
for (int y = 0; y < height - 1; y++) {
|
||||
for (int x = 0; x < width - 1; x++) {
|
||||
int value = array[y][x];
|
||||
if (value == array[y][x + 1] && value == array[y + 1][x] && value == array[y + 1][x + 1]) {
|
||||
penalty += 3;
|
||||
penalty++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return penalty;
|
||||
return N2 * penalty;
|
||||
}
|
||||
|
||||
// Apply mask penalty rule 3 and return the penalty. 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).
|
||||
/**
|
||||
* Apply mask penalty rule 3 and return the penalty. 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).
|
||||
*/
|
||||
static int applyMaskPenaltyRule3(ByteMatrix matrix) {
|
||||
int penalty = 0;
|
||||
byte[][] array = matrix.getArray();
|
||||
int width = matrix.getWidth();
|
||||
int height = matrix.getHeight();
|
||||
for (int y = 0; y < height; ++y) {
|
||||
for (int x = 0; x < width; ++x) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
// Tried to simplify following conditions but failed.
|
||||
if (x + 6 < width &&
|
||||
array[y][x] == 1 &&
|
||||
|
@ -79,7 +93,7 @@ final class MaskUtil {
|
|||
array[y][x - 2] == 0 &&
|
||||
array[y][x - 3] == 0 &&
|
||||
array[y][x - 4] == 0))) {
|
||||
penalty += 40;
|
||||
penalty += N3;
|
||||
}
|
||||
if (y + 6 < height &&
|
||||
array[y][x] == 1 &&
|
||||
|
@ -99,45 +113,41 @@ final class MaskUtil {
|
|||
array[y - 2][x] == 0 &&
|
||||
array[y - 3][x] == 0 &&
|
||||
array[y - 4][x] == 0))) {
|
||||
penalty += 40;
|
||||
penalty += N3;
|
||||
}
|
||||
}
|
||||
}
|
||||
return penalty;
|
||||
}
|
||||
|
||||
// Apply mask penalty rule 4 and return the penalty. Calculate the ratio of dark cells and give
|
||||
// penalty if the ratio is far from 50%. It gives 10 penalty for 5% distance. Examples:
|
||||
// - 0% => 100
|
||||
// - 40% => 20
|
||||
// - 45% => 10
|
||||
// - 50% => 0
|
||||
// - 55% => 10
|
||||
// - 55% => 20
|
||||
// - 100% => 100
|
||||
/**
|
||||
* Apply mask penalty rule 4 and return the penalty. Calculate the ratio of dark cells and give
|
||||
* penalty if the ratio is far from 50%. It gives 10 penalty for 5% distance.
|
||||
*/
|
||||
static int applyMaskPenaltyRule4(ByteMatrix matrix) {
|
||||
int numDarkCells = 0;
|
||||
byte[][] array = matrix.getArray();
|
||||
int width = matrix.getWidth();
|
||||
int height = matrix.getHeight();
|
||||
for (int y = 0; y < height; ++y) {
|
||||
for (int x = 0; x < width; ++x) {
|
||||
if (array[y][x] == 1) {
|
||||
numDarkCells += 1;
|
||||
for (int y = 0; y < height; y++) {
|
||||
byte[] arrayY = array[y];
|
||||
for (int x = 0; x < width; x++) {
|
||||
if (arrayY[x] == 1) {
|
||||
numDarkCells++;
|
||||
}
|
||||
}
|
||||
}
|
||||
int numTotalCells = matrix.getHeight() * matrix.getWidth();
|
||||
double darkRatio = (double) numDarkCells / numTotalCells;
|
||||
return Math.abs((int) (darkRatio * 100 - 50)) / 5 * 10;
|
||||
int fivePercentVariances = (int) (Math.abs(darkRatio - 0.5) * 20.0); // * 100.0 / 5.0
|
||||
return fivePercentVariances * N4;
|
||||
}
|
||||
|
||||
// Return the mask bit for "getMaskPattern" at "x" and "y". See 8.8 of JISX0510:2004 for mask
|
||||
// pattern conditions.
|
||||
/**
|
||||
* Return the mask bit for "getMaskPattern" at "x" and "y". See 8.8 of JISX0510:2004 for mask
|
||||
* pattern conditions.
|
||||
*/
|
||||
static boolean getDataMaskBit(int maskPattern, int x, int y) {
|
||||
if (!QRCode.isValidMaskPattern(maskPattern)) {
|
||||
throw new IllegalArgumentException("Invalid mask pattern");
|
||||
}
|
||||
int intermediate;
|
||||
int temp;
|
||||
switch (maskPattern) {
|
||||
|
@ -174,43 +184,33 @@ final class MaskUtil {
|
|||
return intermediate == 0;
|
||||
}
|
||||
|
||||
// Helper function for applyMaskPenaltyRule1. We need this for doing this calculation in both
|
||||
// vertical and horizontal orders respectively.
|
||||
/**
|
||||
* Helper function for applyMaskPenaltyRule1. We need this for doing this calculation in both
|
||||
* vertical and horizontal orders respectively.
|
||||
*/
|
||||
private static int applyMaskPenaltyRule1Internal(ByteMatrix matrix, boolean isHorizontal) {
|
||||
int penalty = 0;
|
||||
int numSameBitCells = 0;
|
||||
int prevBit = -1;
|
||||
// Horizontal mode:
|
||||
// for (int i = 0; i < matrix.height(); ++i) {
|
||||
// for (int j = 0; j < matrix.width(); ++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.get(j, i);
|
||||
int iLimit = isHorizontal ? matrix.getHeight() : matrix.getWidth();
|
||||
int jLimit = isHorizontal ? matrix.getWidth() : matrix.getHeight();
|
||||
byte[][] array = matrix.getArray();
|
||||
for (int i = 0; i < iLimit; ++i) {
|
||||
for (int j = 0; j < jLimit; ++j) {
|
||||
for (int i = 0; i < iLimit; i++) {
|
||||
int numSameBitCells = 0;
|
||||
int prevBit = -1;
|
||||
for (int j = 0; j < jLimit; j++) {
|
||||
int bit = isHorizontal ? array[i][j] : array[j][i];
|
||||
if (bit == prevBit) {
|
||||
numSameBitCells += 1;
|
||||
// Found five repetitive cells with the same color (bit).
|
||||
// We'll give penalty of 3.
|
||||
if (numSameBitCells == 5) {
|
||||
penalty += 3;
|
||||
} else if (numSameBitCells > 5) {
|
||||
// After five repetitive cells, we'll add the penalty one
|
||||
// by one.
|
||||
penalty += 1;
|
||||
}
|
||||
numSameBitCells++;
|
||||
} else {
|
||||
if (numSameBitCells >= 5) {
|
||||
penalty += N1 + (numSameBitCells - 5);
|
||||
}
|
||||
numSameBitCells = 1; // Include the cell itself.
|
||||
prevBit = bit;
|
||||
}
|
||||
}
|
||||
numSameBitCells = 0; // Clear at each row/column.
|
||||
if (numSameBitCells > 5) {
|
||||
penalty += N1 + (numSameBitCells - 5);
|
||||
}
|
||||
}
|
||||
return penalty;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue