Java Code Examples for com.google.zxing.pdf417.PDF417Common#getBitCountSum()

The following examples show how to use com.google.zxing.pdf417.PDF417Common#getBitCountSum() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDF417CodewordDecoder.java    From ZXing-Orient with Apache License 2.0 6 votes vote down vote up
private static int[] sampleBitCounts(int[] moduleBitCount) {
  float bitCountSum = PDF417Common.getBitCountSum(moduleBitCount);
  int[] result = new int[PDF417Common.BARS_IN_MODULE];
  int bitCountIndex = 0;
  int sumPreviousBits = 0;
  for (int i = 0; i < PDF417Common.MODULES_IN_CODEWORD; i++) {
    float sampleIndex = 
        bitCountSum / (2 * PDF417Common.MODULES_IN_CODEWORD) + 
        (i * bitCountSum) / PDF417Common.MODULES_IN_CODEWORD;
    if (sumPreviousBits + moduleBitCount[bitCountIndex] <= sampleIndex) {
      sumPreviousBits += moduleBitCount[bitCountIndex];
      bitCountIndex++;
    }
    result[bitCountIndex]++;
  }
  return result;
}
 
Example 2
Source File: PDF417CodewordDecoder.java    From ZXing-Orient with Apache License 2.0 6 votes vote down vote up
private static int getClosestDecodedValue(int[] moduleBitCount) {
  int bitCountSum = PDF417Common.getBitCountSum(moduleBitCount);
  float[] bitCountRatios = new float[PDF417Common.BARS_IN_MODULE];
  for (int i = 0; i < bitCountRatios.length; i++) {
    bitCountRatios[i] = moduleBitCount[i] / (float) bitCountSum;
  }
  float bestMatchError = Float.MAX_VALUE;
  int bestMatch = -1;
  for (int j = 0; j < RATIOS_TABLE.length; j++) {
    float error = 0.0f;
    float[] ratioTableRow = RATIOS_TABLE[j];
    for (int k = 0; k < PDF417Common.BARS_IN_MODULE; k++) {
      float diff = ratioTableRow[k] - bitCountRatios[k];
      error += diff * diff;
      if (error >= bestMatchError) {
        break;
      }
    }
    if (error < bestMatchError) {
      bestMatchError = error;
      bestMatch = PDF417Common.SYMBOL_TABLE[j];
    }
  }
  return bestMatch;
}
 
Example 3
Source File: PDF417CodewordDecoder.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 6 votes vote down vote up
private static int[] sampleBitCounts(int[] moduleBitCount) {
  float bitCountSum = PDF417Common.getBitCountSum(moduleBitCount);
  int[] result = new int[PDF417Common.BARS_IN_MODULE];
  int bitCountIndex = 0;
  int sumPreviousBits = 0;
  for (int i = 0; i < PDF417Common.MODULES_IN_CODEWORD; i++) {
    float sampleIndex = 
        bitCountSum / (2 * PDF417Common.MODULES_IN_CODEWORD) + 
        (i * bitCountSum) / PDF417Common.MODULES_IN_CODEWORD;
    if (sumPreviousBits + moduleBitCount[bitCountIndex] <= sampleIndex) {
      sumPreviousBits += moduleBitCount[bitCountIndex];
      bitCountIndex++;
    }
    result[bitCountIndex]++;
  }
  return result;
}
 
Example 4
Source File: PDF417CodewordDecoder.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 6 votes vote down vote up
private static int getClosestDecodedValue(int[] moduleBitCount) {
  int bitCountSum = PDF417Common.getBitCountSum(moduleBitCount);
  float[] bitCountRatios = new float[PDF417Common.BARS_IN_MODULE];
  for (int i = 0; i < bitCountRatios.length; i++) {
    bitCountRatios[i] = moduleBitCount[i] / (float) bitCountSum;
  }
  float bestMatchError = Float.MAX_VALUE;
  int bestMatch = -1;
  for (int j = 0; j < RATIOS_TABLE.length; j++) {
    float error = 0.0f;
    float[] ratioTableRow = RATIOS_TABLE[j];
    for (int k = 0; k < PDF417Common.BARS_IN_MODULE; k++) {
      float diff = ratioTableRow[k] - bitCountRatios[k];
      error += diff * diff;
      if (error >= bestMatchError) {
        break;
      }
    }
    if (error < bestMatchError) {
      bestMatchError = error;
      bestMatch = PDF417Common.SYMBOL_TABLE[j];
    }
  }
  return bestMatch;
}
 
Example 5
Source File: PDF417CodewordDecoder.java    From weex with Apache License 2.0 6 votes vote down vote up
private static int[] sampleBitCounts(int[] moduleBitCount) {
  float bitCountSum = PDF417Common.getBitCountSum(moduleBitCount);
  int[] result = new int[PDF417Common.BARS_IN_MODULE];
  int bitCountIndex = 0;
  int sumPreviousBits = 0;
  for (int i = 0; i < PDF417Common.MODULES_IN_CODEWORD; i++) {
    float sampleIndex = 
        bitCountSum / (2 * PDF417Common.MODULES_IN_CODEWORD) + 
        (i * bitCountSum) / PDF417Common.MODULES_IN_CODEWORD;
    if (sumPreviousBits + moduleBitCount[bitCountIndex] <= sampleIndex) {
      sumPreviousBits += moduleBitCount[bitCountIndex];
      bitCountIndex++;
    }
    result[bitCountIndex]++;
  }
  return result;
}
 
Example 6
Source File: PDF417CodewordDecoder.java    From weex with Apache License 2.0 6 votes vote down vote up
private static int getClosestDecodedValue(int[] moduleBitCount) {
  int bitCountSum = PDF417Common.getBitCountSum(moduleBitCount);
  float[] bitCountRatios = new float[PDF417Common.BARS_IN_MODULE];
  for (int i = 0; i < bitCountRatios.length; i++) {
    bitCountRatios[i] = moduleBitCount[i] / (float) bitCountSum;
  }
  float bestMatchError = Float.MAX_VALUE;
  int bestMatch = -1;
  for (int j = 0; j < RATIOS_TABLE.length; j++) {
    float error = 0.0f;
    float[] ratioTableRow = RATIOS_TABLE[j];
    for (int k = 0; k < PDF417Common.BARS_IN_MODULE; k++) {
      float diff = ratioTableRow[k] - bitCountRatios[k];
      error += diff * diff;
      if (error >= bestMatchError) {
        break;
      }
    }
    if (error < bestMatchError) {
      bestMatchError = error;
      bestMatch = PDF417Common.SYMBOL_TABLE[j];
    }
  }
  return bestMatch;
}
 
Example 7
Source File: PDF417ScanningDecoder.java    From ZXing-Orient with Apache License 2.0 4 votes vote down vote up
private static Codeword detectCodeword(BitMatrix image,
                                       int minColumn,
                                       int maxColumn,
                                       boolean leftToRight,
                                       int startColumn,
                                       int imageRow,
                                       int minCodewordWidth,
                                       int maxCodewordWidth) {
  startColumn = adjustCodewordStartColumn(image, minColumn, maxColumn, leftToRight, startColumn, imageRow);
  // we usually know fairly exact now how long a codeword is. We should provide minimum and maximum expected length
  // and try to adjust the read pixels, e.g. remove single pixel errors or try to cut off exceeding pixels.
  // min and maxCodewordWidth should not be used as they are calculated for the whole barcode an can be inaccurate
  // for the current position
  int[] moduleBitCount = getModuleBitCount(image, minColumn, maxColumn, leftToRight, startColumn, imageRow);
  if (moduleBitCount == null) {
    return null;
  }
  int endColumn;
  int codewordBitCount = PDF417Common.getBitCountSum(moduleBitCount);
  if (leftToRight) {
    endColumn = startColumn + codewordBitCount;
  } else {
    for (int i = 0; i < moduleBitCount.length / 2; i++) {
      int tmpCount = moduleBitCount[i];
      moduleBitCount[i] = moduleBitCount[moduleBitCount.length - 1 - i];
      moduleBitCount[moduleBitCount.length - 1 - i] = tmpCount;
    }
    endColumn = startColumn;
    startColumn = endColumn - codewordBitCount;
  }
  // TODO implement check for width and correction of black and white bars
  // use start (and maybe stop pattern) to determine if blackbars are wider than white bars. If so, adjust.
  // should probably done only for codewords with a lot more than 17 bits. 
  // The following fixes 10-1.png, which has wide black bars and small white bars
  //    for (int i = 0; i < moduleBitCount.length; i++) {
  //      if (i % 2 == 0) {
  //        moduleBitCount[i]--;
  //      } else {
  //        moduleBitCount[i]++;
  //      }
  //    }

  // We could also use the width of surrounding codewords for more accurate results, but this seems
  // sufficient for now
  if (!checkCodewordSkew(codewordBitCount, minCodewordWidth, maxCodewordWidth)) {
    // We could try to use the startX and endX position of the codeword in the same column in the previous row,
    // create the bit count from it and normalize it to 8. This would help with single pixel errors.
    return null;
  }

  int decodedValue = PDF417CodewordDecoder.getDecodedValue(moduleBitCount);
  int codeword = PDF417Common.getCodeword(decodedValue);
  if (codeword == -1) {
    return null;
  }
  return new Codeword(startColumn, endColumn, getCodewordBucketNumber(decodedValue), codeword);
}
 
Example 8
Source File: PDF417ScanningDecoder.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 4 votes vote down vote up
private static Codeword detectCodeword(BitMatrix image,
                                       int minColumn,
                                       int maxColumn,
                                       boolean leftToRight,
                                       int startColumn,
                                       int imageRow,
                                       int minCodewordWidth,
                                       int maxCodewordWidth) {
  startColumn = adjustCodewordStartColumn(image, minColumn, maxColumn, leftToRight, startColumn, imageRow);
  // we usually know fairly exact now how long a codeword is. We should provide minimum and maximum expected length
  // and try to adjust the read pixels, e.g. remove single pixel errors or try to cut off exceeding pixels.
  // min and maxCodewordWidth should not be used as they are calculated for the whole barcode an can be inaccurate
  // for the current position
  int[] moduleBitCount = getModuleBitCount(image, minColumn, maxColumn, leftToRight, startColumn, imageRow);
  if (moduleBitCount == null) {
    return null;
  }
  int endColumn;
  int codewordBitCount = PDF417Common.getBitCountSum(moduleBitCount);
  if (leftToRight) {
    endColumn = startColumn + codewordBitCount;
  } else {
    for (int i = 0; i < moduleBitCount.length / 2; i++) {
      int tmpCount = moduleBitCount[i];
      moduleBitCount[i] = moduleBitCount[moduleBitCount.length - 1 - i];
      moduleBitCount[moduleBitCount.length - 1 - i] = tmpCount;
    }
    endColumn = startColumn;
    startColumn = endColumn - codewordBitCount;
  }
  // TODO implement check for width and correction of black and white bars
  // use start (and maybe stop pattern) to determine if blackbars are wider than white bars. If so, adjust.
  // should probably done only for codewords with a lot more than 17 bits. 
  // The following fixes 10-1.png, which has wide black bars and small white bars
  //    for (int i = 0; i < moduleBitCount.length; i++) {
  //      if (i % 2 == 0) {
  //        moduleBitCount[i]--;
  //      } else {
  //        moduleBitCount[i]++;
  //      }
  //    }

  // We could also use the width of surrounding codewords for more accurate results, but this seems
  // sufficient for now
  if (!checkCodewordSkew(codewordBitCount, minCodewordWidth, maxCodewordWidth)) {
    // We could try to use the startX and endX position of the codeword in the same column in the previous row,
    // create the bit count from it and normalize it to 8. This would help with single pixel errors.
    return null;
  }

  int decodedValue = PDF417CodewordDecoder.getDecodedValue(moduleBitCount);
  int codeword = PDF417Common.getCodeword(decodedValue);
  if (codeword == -1) {
    return null;
  }
  return new Codeword(startColumn, endColumn, getCodewordBucketNumber(decodedValue), codeword);
}
 
Example 9
Source File: PDF417ScanningDecoder.java    From weex with Apache License 2.0 4 votes vote down vote up
private static Codeword detectCodeword(BitMatrix image,
                                       int minColumn,
                                       int maxColumn,
                                       boolean leftToRight,
                                       int startColumn,
                                       int imageRow,
                                       int minCodewordWidth,
                                       int maxCodewordWidth) {
  startColumn = adjustCodewordStartColumn(image, minColumn, maxColumn, leftToRight, startColumn, imageRow);
  // we usually know fairly exact now how long a codeword is. We should provide minimum and maximum expected length
  // and try to adjust the read pixels, e.g. remove single pixel errors or try to cut off exceeding pixels.
  // min and maxCodewordWidth should not be used as they are calculated for the whole barcode an can be inaccurate
  // for the current position
  int[] moduleBitCount = getModuleBitCount(image, minColumn, maxColumn, leftToRight, startColumn, imageRow);
  if (moduleBitCount == null) {
    return null;
  }
  int endColumn;
  int codewordBitCount = PDF417Common.getBitCountSum(moduleBitCount);
  if (leftToRight) {
    endColumn = startColumn + codewordBitCount;
  } else {
    for (int i = 0; i < moduleBitCount.length / 2; i++) {
      int tmpCount = moduleBitCount[i];
      moduleBitCount[i] = moduleBitCount[moduleBitCount.length - 1 - i];
      moduleBitCount[moduleBitCount.length - 1 - i] = tmpCount;
    }
    endColumn = startColumn;
    startColumn = endColumn - codewordBitCount;
  }
  // TODO implement check for width and correction of black and white bars
  // use start (and maybe stop pattern) to determine if blackbars are wider than white bars. If so, adjust.
  // should probably done only for codewords with a lot more than 17 bits. 
  // The following fixes 10-1.png, which has wide black bars and small white bars
  //    for (int i = 0; i < moduleBitCount.length; i++) {
  //      if (i % 2 == 0) {
  //        moduleBitCount[i]--;
  //      } else {
  //        moduleBitCount[i]++;
  //      }
  //    }

  // We could also use the width of surrounding codewords for more accurate results, but this seems
  // sufficient for now
  if (!checkCodewordSkew(codewordBitCount, minCodewordWidth, maxCodewordWidth)) {
    // We could try to use the startX and endX position of the codeword in the same column in the previous row,
    // create the bit count from it and normalize it to 8. This would help with single pixel errors.
    return null;
  }

  int decodedValue = PDF417CodewordDecoder.getDecodedValue(moduleBitCount);
  int codeword = PDF417Common.getCodeword(decodedValue);
  if (codeword == -1) {
    return null;
  }
  return new Codeword(startColumn, endColumn, getCodewordBucketNumber(decodedValue), codeword);
}