下面列出了com.google.zxing.common.BitArray#getNextSet ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static int c(BitArray bitarray)
{
int j = bitarray.getSize();
int k = bitarray.getNextSet(0);
if (k == j)
{
throw NotFoundException.getNotFoundInstance();
} else
{
return k;
}
}
private static int[] findAsteriskPattern(BitArray row, int[] counters) throws NotFoundException {
int width = row.getSize();
int rowOffset = row.getNextSet(0);
int counterPosition = 0;
int patternStart = rowOffset;
boolean isWhite = false;
int patternLength = counters.length;
for (int i = rowOffset; i < width; i++) {
if (row.get(i) != isWhite) {
counters[counterPosition]++;
} else {
if (counterPosition == patternLength - 1) {
// Look for whitespace before start pattern, >= 50% of width of start pattern
if (toNarrowWidePattern(counters) == ASTERISK_ENCODING &&
row.isRange(Math.max(0, patternStart - ((i - patternStart) / 2)), patternStart, false)) {
return new int[]{patternStart, i};
}
patternStart += counters[0] + counters[1];
System.arraycopy(counters, 2, counters, 0, counterPosition - 1);
counters[counterPosition - 1] = 0;
counters[counterPosition] = 0;
counterPosition--;
} else {
counterPosition++;
}
counters[counterPosition] = 1;
isWhite = !isWhite;
}
}
throw NotFoundException.getNotFoundInstance();
}
/**
* @param row row of black/white values to search
* @param rowOffset position to start search
* @param whiteFirst if true, indicates that the pattern specifies white/black/white/...
* pixel counts, otherwise, it is interpreted as black/white/black/...
* @param pattern pattern of counts of number of black and white pixels that are being
* searched for as a pattern
* @param counters array of counters, as long as pattern, to re-use
* @return start/end horizontal offset of guard pattern, as an array of two ints
* @throws NotFoundException if pattern is not found
*/
private static int[] findGuardPattern(BitArray row,
int rowOffset,
boolean whiteFirst,
int[] pattern,
int[] counters) throws NotFoundException {
int patternLength = pattern.length;
int width = row.getSize();
boolean isWhite = whiteFirst;
rowOffset = whiteFirst ? row.getNextUnset(rowOffset) : row.getNextSet(rowOffset);
int counterPosition = 0;
int patternStart = rowOffset;
for (int x = rowOffset; x < width; x++) {
if (row.get(x) ^ isWhite) {
counters[counterPosition]++;
} else {
if (counterPosition == patternLength - 1) {
if (patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) {
return new int[]{patternStart, x};
}
patternStart += counters[0] + counters[1];
System.arraycopy(counters, 2, counters, 0, patternLength - 2);
counters[patternLength - 2] = 0;
counters[patternLength - 1] = 0;
counterPosition--;
} else {
counterPosition++;
}
counters[counterPosition] = 1;
isWhite = !isWhite;
}
}
throw NotFoundException.getNotFoundInstance();
}
private int[] findAsteriskPattern(BitArray row) throws NotFoundException {
int width = row.getSize();
int rowOffset = row.getNextSet(0);
Arrays.fill(counters, 0);
int[] theCounters = counters;
int patternStart = rowOffset;
boolean isWhite = false;
int patternLength = theCounters.length;
int counterPosition = 0;
for (int i = rowOffset; i < width; i++) {
if (row.get(i) ^ isWhite) {
theCounters[counterPosition]++;
} else {
if (counterPosition == patternLength - 1) {
if (toPattern(theCounters) == ASTERISK_ENCODING) {
return new int[] { patternStart, i };
}
patternStart += theCounters[0] + theCounters[1];
System.arraycopy(theCounters, 2, theCounters, 0, patternLength - 2);
theCounters[patternLength - 2] = 0;
theCounters[patternLength - 1] = 0;
counterPosition--;
} else {
counterPosition++;
}
theCounters[counterPosition] = 1;
isWhite = !isWhite;
}
}
throw NotFoundException.getNotFoundInstance();
}
private static int getNextSecondBar(BitArray row, int initialPos){
int currentPos;
if (row.get(initialPos)) {
currentPos = row.getNextUnset(initialPos);
currentPos = row.getNextSet(currentPos);
} else {
currentPos = row.getNextSet(initialPos);
currentPos = row.getNextUnset(currentPos);
}
return currentPos;
}
private int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString) throws NotFoundException {
int[] counters = decodeMiddleCounters;
counters[0] = 0;
counters[1] = 0;
counters[2] = 0;
counters[3] = 0;
int end = row.getSize();
int rowOffset = startRange[1];
int checkParity = 0;
for (int x = 0; x < 2 && rowOffset < end; x++) {
int bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS);
resultString.append((char) ('0' + bestMatch % 10));
for (int counter : counters) {
rowOffset += counter;
}
if (bestMatch >= 10) {
checkParity |= 1 << (1 - x);
}
if (x != 1) {
// Read off separator if not last
rowOffset = row.getNextSet(rowOffset);
rowOffset = row.getNextUnset(rowOffset);
}
}
if (resultString.length() != 2) {
throw NotFoundException.getNotFoundInstance();
}
if (Integer.parseInt(resultString.toString()) % 4 != checkParity) {
throw NotFoundException.getNotFoundInstance();
}
return rowOffset;
}
private static int getNextSecondBar(BitArray row, int initialPos) {
int currentPos;
if (row.get(initialPos)) {
currentPos = row.getNextUnset(initialPos);
currentPos = row.getNextSet(currentPos);
} else {
currentPos = row.getNextSet(initialPos);
currentPos = row.getNextUnset(currentPos);
}
return currentPos;
}
/**
* Skip all whitespace until we get to the first black line.
*
* @param row row of black/white values to search
* @return index of the first black line.
* @throws NotFoundException Throws exception if no black lines are found in the row
*/
private static int skipWhiteSpace(BitArray row) throws NotFoundException {
int width = row.getSize();
int endStart = row.getNextSet(0);
if (endStart == width) {
throw NotFoundException.getNotFoundInstance();
}
return endStart;
}
private static int getNextSecondBar(BitArray row, int initialPos) {
int currentPos;
if (row.get(initialPos)) {
currentPos = row.getNextUnset(initialPos);
currentPos = row.getNextSet(currentPos);
} else {
currentPos = row.getNextSet(initialPos);
currentPos = row.getNextUnset(currentPos);
}
return currentPos;
}
int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString) throws NotFoundException {
int[] counters = decodeMiddleCounters;
counters[0] = 0;
counters[1] = 0;
counters[2] = 0;
counters[3] = 0;
int end = row.getSize();
int rowOffset = startRange[1];
int lgPatternFound = 0;
for (int x = 0; x < 5 && rowOffset < end; x++) {
int bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS);
resultString.append((char) ('0' + bestMatch % 10));
for (int counter : counters) {
rowOffset += counter;
}
if (bestMatch >= 10) {
lgPatternFound |= 1 << (4 - x);
}
if (x != 4) {
// Read off separator if not last
rowOffset = row.getNextSet(rowOffset);
rowOffset = row.getNextUnset(rowOffset);
}
}
if (resultString.length() != 5) {
throw NotFoundException.getNotFoundInstance();
}
int checkDigit = determineCheckDigit(lgPatternFound);
if (extensionChecksum(resultString.toString()) != checkDigit) {
throw NotFoundException.getNotFoundInstance();
}
return rowOffset;
}
/**
* Skip all whitespace until we get to the first black line.
*
* @param row row of black/white values to search
* @return index of the first black line.
* @throws NotFoundException Throws exception if no black lines are found in the row
*/
private static int skipWhiteSpace(BitArray row) throws NotFoundException {
int width = row.getSize();
int endStart = row.getNextSet(0);
if (endStart == width) {
throw NotFoundException.getNotFoundInstance();
}
return endStart;
}
private static int getNextSecondBar(BitArray row, int initialPos) {
int currentPos;
if (row.get(initialPos)) {
currentPos = row.getNextUnset(initialPos);
currentPos = row.getNextSet(currentPos);
} else {
currentPos = row.getNextSet(initialPos);
currentPos = row.getNextUnset(currentPos);
}
return currentPos;
}
private int[] findAsteriskPattern(BitArray row) throws NotFoundException {
int width = row.getSize();
int rowOffset = row.getNextSet(0);
Arrays.fill(counters, 0);
int[] theCounters = counters;
int patternStart = rowOffset;
boolean isWhite = false;
int patternLength = theCounters.length;
int counterPosition = 0;
for (int i = rowOffset; i < width; i++) {
if (row.get(i) ^ isWhite) {
theCounters[counterPosition]++;
} else {
if (counterPosition == patternLength - 1) {
if (toPattern(theCounters) == ASTERISK_ENCODING) {
return new int[]{patternStart, i};
}
patternStart += theCounters[0] + theCounters[1];
System.arraycopy(theCounters, 2, theCounters, 0, patternLength - 2);
theCounters[patternLength - 2] = 0;
theCounters[patternLength - 1] = 0;
counterPosition--;
} else {
counterPosition++;
}
theCounters[counterPosition] = 1;
isWhite = !isWhite;
}
}
throw NotFoundException.getNotFoundInstance();
}
private static int[] findAsteriskPattern(BitArray row, int[] counters) throws NotFoundException {
int width = row.getSize();
int rowOffset = row.getNextSet(0);
int counterPosition = 0;
int patternStart = rowOffset;
boolean isWhite = false;
int patternLength = counters.length;
for (int i = rowOffset; i < width; i++) {
if (row.get(i) ^ isWhite) {
counters[counterPosition]++;
} else {
if (counterPosition == patternLength - 1) {
// Look for whitespace before start pattern, >= 50% of width of start pattern
if (toNarrowWidePattern(counters) == ASTERISK_ENCODING &&
row.isRange(Math.max(0, patternStart - ((i - patternStart) >> 1)), patternStart, false)) {
return new int[]{patternStart, i};
}
patternStart += counters[0] + counters[1];
System.arraycopy(counters, 2, counters, 0, patternLength - 2);
counters[patternLength - 2] = 0;
counters[patternLength - 1] = 0;
counterPosition--;
} else {
counterPosition++;
}
counters[counterPosition] = 1;
isWhite = !isWhite;
}
}
throw NotFoundException.getNotFoundInstance();
}
/**
* @param row row of black/white values to search
* @param rowOffset position to start search
* @param whiteFirst if true, indicates that the pattern specifies white/black/white/...
* pixel counts, otherwise, it is interpreted as black/white/black/...
* @param pattern pattern of counts of number of black and white pixels that are being
* searched for as a pattern
* @param counters array of counters, as long as pattern, to re-use
* @return start/end horizontal offset of guard pattern, as an array of two ints
* @throws NotFoundException if pattern is not found
*/
private static int[] findGuardPattern(BitArray row,
int rowOffset,
boolean whiteFirst,
int[] pattern,
int[] counters) throws NotFoundException {
int patternLength = pattern.length;
int width = row.getSize();
boolean isWhite = whiteFirst;
rowOffset = whiteFirst ? row.getNextUnset(rowOffset) : row.getNextSet(rowOffset);
int counterPosition = 0;
int patternStart = rowOffset;
for (int x = rowOffset; x < width; x++) {
if (row.get(x) ^ isWhite) {
counters[counterPosition]++;
} else {
if (counterPosition == patternLength - 1) {
if (patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) {
return new int[]{patternStart, x};
}
patternStart += counters[0] + counters[1];
System.arraycopy(counters, 2, counters, 0, patternLength - 2);
counters[patternLength - 2] = 0;
counters[patternLength - 1] = 0;
counterPosition--;
} else {
counterPosition++;
}
counters[counterPosition] = 1;
isWhite = !isWhite;
}
}
throw NotFoundException.getNotFoundInstance();
}
private static int[] findAsteriskPattern(BitArray row, int[] counters) throws NotFoundException {
int width = row.getSize();
int rowOffset = row.getNextSet(0);
int counterPosition = 0;
int patternStart = rowOffset;
boolean isWhite = false;
int patternLength = counters.length;
for (int i = rowOffset; i < width; i++) {
if (row.get(i) ^ isWhite) {
counters[counterPosition]++;
} else {
if (counterPosition == patternLength - 1) {
// Look for whitespace before start pattern, >= 50% of width of start pattern
if (toNarrowWidePattern(counters) == ASTERISK_ENCODING &&
row.isRange(Math.max(0, patternStart - ((i - patternStart) / 2)), patternStart, false)) {
return new int[]{patternStart, i};
}
patternStart += counters[0] + counters[1];
System.arraycopy(counters, 2, counters, 0, patternLength - 2);
counters[patternLength - 2] = 0;
counters[patternLength - 1] = 0;
counterPosition--;
} else {
counterPosition++;
}
counters[counterPosition] = 1;
isWhite = !isWhite;
}
}
throw NotFoundException.getNotFoundInstance();
}
private static int[] findStartPattern(BitArray row) throws NotFoundException {
int width = row.getSize();
int rowOffset = row.getNextSet(0);
int counterPosition = 0;
int[] counters = new int[6];
int patternStart = rowOffset;
boolean isWhite = false;
int patternLength = counters.length;
for (int i = rowOffset; i < width; i++) {
if (row.get(i) != isWhite) {
counters[counterPosition]++;
} else {
if (counterPosition == patternLength - 1) {
float bestVariance = MAX_AVG_VARIANCE;
int bestMatch = -1;
for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
float variance = patternMatchVariance(counters, CODE_PATTERNS[startCode],
MAX_INDIVIDUAL_VARIANCE);
if (variance < bestVariance) {
bestVariance = variance;
bestMatch = startCode;
}
}
// Look for whitespace before start pattern, >= 50% of width of start pattern
if (bestMatch >= 0 &&
row.isRange(Math.max(0, patternStart - (i - patternStart) / 2), patternStart, false)) {
return new int[]{patternStart, i, bestMatch};
}
patternStart += counters[0] + counters[1];
System.arraycopy(counters, 2, counters, 0, counterPosition - 1);
counters[counterPosition - 1] = 0;
counters[counterPosition] = 0;
counterPosition--;
} else {
counterPosition++;
}
counters[counterPosition] = 1;
isWhite = !isWhite;
}
}
throw NotFoundException.getNotFoundInstance();
}
private static int[] a(BitArray bitarray)
{
int i = bitarray.getSize();
int j = bitarray.getNextSet(0);
int ai[] = new int[6];
int k = ai.length;
int l = j;
boolean flag = false;
int i1 = 0;
while (l < i)
{
if (flag ^ bitarray.get(l))
{
ai[i1] = 1 + ai[i1];
} else
{
if (i1 == k - 1)
{
if (a(ai) == d)
{
return (new int[] {
j, l
});
}
j += ai[0] + ai[1];
System.arraycopy(ai, 2, ai, 0, k - 2);
ai[k - 2] = 0;
ai[k - 1] = 0;
i1--;
} else
{
i1++;
}
ai[i1] = 1;
if (!flag)
{
flag = true;
} else
{
flag = false;
}
}
l++;
}
throw NotFoundException.getNotFoundInstance();
}
public Result decodeRow(int i, BitArray bitarray, Map map)
{
int ai[] = a(bitarray);
ai[1] = 0;
int j = bitarray.getNextSet(ai[1]);
int k = bitarray.getSize();
StringBuilder stringbuilder = new StringBuilder();
int ai1[] = new int[7];
do
{
for (int l = 0; l < ai1.length; l++)
{
ai1[l] = 0;
}
recordPattern(bitarray, j, ai1);
char c1 = a(ai1);
if (c1 == '!')
{
throw NotFoundException.getNotFoundInstance();
}
stringbuilder.append(c1);
int i1 = ai1.length;
int j1 = 0;
int k1 = j;
for (; j1 < i1; j1++)
{
k1 += ai1[j1];
}
int l1 = bitarray.getNextSet(k1);
if (l1 >= k)
{
int i2 = 0;
int j2 = ai1.length;
for (int k2 = 0; k2 < j2; k2++)
{
i2 += ai1[k2];
}
int l2 = l1 - j - i2;
if (l1 != k && l2 / 2 < i2)
{
throw NotFoundException.getNotFoundInstance();
}
if (stringbuilder.length() < 2)
{
throw NotFoundException.getNotFoundInstance();
}
char c2 = stringbuilder.charAt(0);
if (!a(e, c2))
{
throw NotFoundException.getNotFoundInstance();
}
int i3 = 1;
do
{
label0:
{
if (i3 < stringbuilder.length())
{
if (stringbuilder.charAt(i3) != c2 || i3 + 1 == stringbuilder.length())
{
break label0;
}
stringbuilder.delete(i3 + 1, -1 + stringbuilder.length());
}
if (stringbuilder.length() <= 6)
{
throw NotFoundException.getNotFoundInstance();
} else
{
stringbuilder.deleteCharAt(-1 + stringbuilder.length());
stringbuilder.deleteCharAt(0);
float f = (float)(ai[1] + ai[0]) / 2.0F;
float f1 = (float)(j + l1) / 2.0F;
String s = stringbuilder.toString();
ResultPoint aresultpoint[] = new ResultPoint[2];
aresultpoint[0] = new ResultPoint(f, i);
aresultpoint[1] = new ResultPoint(f1, i);
return new Result(s, null, aresultpoint, BarcodeFormat.CODABAR);
}
}
i3++;
} while (true);
}
j = l1;
} while (true);
}
private static int[] findStartPattern(BitArray row) throws NotFoundException {
int width = row.getSize();
int rowOffset = row.getNextSet(0);
int counterPosition = 0;
int[] counters = new int[6];
int patternStart = rowOffset;
boolean isWhite = false;
int patternLength = counters.length;
for (int i = rowOffset; i < width; i++) {
if (row.get(i) ^ isWhite) {
counters[counterPosition]++;
} else {
if (counterPosition == patternLength - 1) {
float bestVariance = MAX_AVG_VARIANCE;
int bestMatch = -1;
for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
float variance = patternMatchVariance(counters, CODE_PATTERNS[startCode],
MAX_INDIVIDUAL_VARIANCE);
if (variance < bestVariance) {
bestVariance = variance;
bestMatch = startCode;
}
}
// Look for whitespace before start pattern, >= 50% of width
// of start pattern
if (bestMatch >= 0
&& row.isRange(Math.max(0, patternStart - (i - patternStart) / 2), patternStart, false)) {
return new int[] { patternStart, i, bestMatch };
}
patternStart += counters[0] + counters[1];
System.arraycopy(counters, 2, counters, 0, patternLength - 2);
counters[patternLength - 2] = 0;
counters[patternLength - 1] = 0;
counterPosition--;
} else {
counterPosition++;
}
counters[counterPosition] = 1;
isWhite = !isWhite;
}
}
throw NotFoundException.getNotFoundInstance();
}