Search in sources :

Example 1 with DataCharacter

use of com.google.zxing.oned.rss.DataCharacter in project weex-example by KalicyZhou.

the class RSSExpandedReader method retrieveNextPair.

// not private for testing
ExpandedPair retrieveNextPair(BitArray row, List<ExpandedPair> previousPairs, int rowNumber) throws NotFoundException {
    boolean isOddPattern = previousPairs.size() % 2 == 0;
    if (startFromEven) {
        isOddPattern = !isOddPattern;
    }
    FinderPattern pattern;
    boolean keepFinding = true;
    int forcedOffset = -1;
    do {
        this.findNextPair(row, previousPairs, forcedOffset);
        pattern = parseFoundFinderPattern(row, rowNumber, isOddPattern);
        if (pattern == null) {
            forcedOffset = getNextSecondBar(row, this.startEnd[0]);
        } else {
            keepFinding = false;
        }
    } while (keepFinding);
    // When stacked symbol is split over multiple rows, there's no way to guess if this pair can be last or not.
    // boolean mayBeLast = checkPairSequence(previousPairs, pattern);
    DataCharacter leftChar = this.decodeDataCharacter(row, pattern, isOddPattern, true);
    if (!previousPairs.isEmpty() && previousPairs.get(previousPairs.size() - 1).mustBeLast()) {
        throw NotFoundException.getNotFoundInstance();
    }
    DataCharacter rightChar;
    try {
        rightChar = this.decodeDataCharacter(row, pattern, isOddPattern, false);
    } catch (NotFoundException ignored) {
        rightChar = null;
    }
    boolean mayBeLast = true;
    return new ExpandedPair(leftChar, rightChar, pattern, mayBeLast);
}
Also used : FinderPattern(com.google.zxing.oned.rss.FinderPattern) NotFoundException(com.google.zxing.NotFoundException) ResultPoint(com.google.zxing.ResultPoint) DataCharacter(com.google.zxing.oned.rss.DataCharacter)

Example 2 with DataCharacter

use of com.google.zxing.oned.rss.DataCharacter in project zxing by zxing.

the class BitArrayBuilderTest method buildBitArray.

private static BitArray buildBitArray(int[][] pairValues) {
    List<ExpandedPair> pairs = new ArrayList<>();
    for (int i = 0; i < pairValues.length; ++i) {
        int[] pair = pairValues[i];
        DataCharacter leftChar;
        if (i == 0) {
            leftChar = null;
        } else {
            leftChar = new DataCharacter(pair[0], 0);
        }
        DataCharacter rightChar;
        if (i == 0) {
            rightChar = new DataCharacter(pair[0], 0);
        } else if (pair.length == 2) {
            rightChar = new DataCharacter(pair[1], 0);
        } else {
            rightChar = null;
        }
        ExpandedPair expandedPair = new ExpandedPair(leftChar, rightChar, null, true);
        pairs.add(expandedPair);
    }
    return BitArrayBuilder.buildBitArray(pairs);
}
Also used : ArrayList(java.util.ArrayList) DataCharacter(com.google.zxing.oned.rss.DataCharacter)

Example 3 with DataCharacter

use of com.google.zxing.oned.rss.DataCharacter in project weex-example by KalicyZhou.

the class RSSExpandedReader method decodeDataCharacter.

DataCharacter decodeDataCharacter(BitArray row, FinderPattern pattern, boolean isOddPattern, boolean leftChar) throws NotFoundException {
    int[] counters = this.getDataCharacterCounters();
    counters[0] = 0;
    counters[1] = 0;
    counters[2] = 0;
    counters[3] = 0;
    counters[4] = 0;
    counters[5] = 0;
    counters[6] = 0;
    counters[7] = 0;
    if (leftChar) {
        recordPatternInReverse(row, pattern.getStartEnd()[0], counters);
    } else {
        recordPattern(row, pattern.getStartEnd()[1], counters);
        // reverse it
        for (int i = 0, j = counters.length - 1; i < j; i++, j--) {
            int temp = counters[i];
            counters[i] = counters[j];
            counters[j] = temp;
        }
    }
    //counters[] has the pixels of the module
    //left and right data characters have all the same length
    int numModules = 17;
    float elementWidth = (float) count(counters) / (float) numModules;
    // Sanity check: element width for pattern and the character should match
    float expectedElementWidth = (pattern.getStartEnd()[1] - pattern.getStartEnd()[0]) / 15.0f;
    if (Math.abs(elementWidth - expectedElementWidth) / expectedElementWidth > 0.3f) {
        throw NotFoundException.getNotFoundInstance();
    }
    int[] oddCounts = this.getOddCounts();
    int[] evenCounts = this.getEvenCounts();
    float[] oddRoundingErrors = this.getOddRoundingErrors();
    float[] evenRoundingErrors = this.getEvenRoundingErrors();
    for (int i = 0; i < counters.length; i++) {
        float value = 1.0f * counters[i] / elementWidth;
        // Round
        int count = (int) (value + 0.5f);
        if (count < 1) {
            if (value < 0.3f) {
                throw NotFoundException.getNotFoundInstance();
            }
            count = 1;
        } else if (count > 8) {
            if (value > 8.7f) {
                throw NotFoundException.getNotFoundInstance();
            }
            count = 8;
        }
        int offset = i / 2;
        if ((i & 0x01) == 0) {
            oddCounts[offset] = count;
            oddRoundingErrors[offset] = value - count;
        } else {
            evenCounts[offset] = count;
            evenRoundingErrors[offset] = value - count;
        }
    }
    adjustOddEvenCounts(numModules);
    int weightRowNumber = 4 * pattern.getValue() + (isOddPattern ? 0 : 2) + (leftChar ? 0 : 1) - 1;
    int oddSum = 0;
    int oddChecksumPortion = 0;
    for (int i = oddCounts.length - 1; i >= 0; i--) {
        if (isNotA1left(pattern, isOddPattern, leftChar)) {
            int weight = WEIGHTS[weightRowNumber][2 * i];
            oddChecksumPortion += oddCounts[i] * weight;
        }
        oddSum += oddCounts[i];
    }
    int evenChecksumPortion = 0;
    //int evenSum = 0;
    for (int i = evenCounts.length - 1; i >= 0; i--) {
        if (isNotA1left(pattern, isOddPattern, leftChar)) {
            int weight = WEIGHTS[weightRowNumber][2 * i + 1];
            evenChecksumPortion += evenCounts[i] * weight;
        }
    //evenSum += evenCounts[i];
    }
    int checksumPortion = oddChecksumPortion + evenChecksumPortion;
    if ((oddSum & 0x01) != 0 || oddSum > 13 || oddSum < 4) {
        throw NotFoundException.getNotFoundInstance();
    }
    int group = (13 - oddSum) / 2;
    int oddWidest = SYMBOL_WIDEST[group];
    int evenWidest = 9 - oddWidest;
    int vOdd = RSSUtils.getRSSvalue(oddCounts, oddWidest, true);
    int vEven = RSSUtils.getRSSvalue(evenCounts, evenWidest, false);
    int tEven = EVEN_TOTAL_SUBSET[group];
    int gSum = GSUM[group];
    int value = vOdd * tEven + vEven + gSum;
    return new DataCharacter(value, checksumPortion);
}
Also used : ResultPoint(com.google.zxing.ResultPoint) DataCharacter(com.google.zxing.oned.rss.DataCharacter)

Example 4 with DataCharacter

use of com.google.zxing.oned.rss.DataCharacter in project weex-example by KalicyZhou.

the class RSSExpandedReader method checkChecksum.

private boolean checkChecksum() {
    ExpandedPair firstPair = this.pairs.get(0);
    DataCharacter checkCharacter = firstPair.getLeftChar();
    DataCharacter firstCharacter = firstPair.getRightChar();
    if (firstCharacter == null) {
        return false;
    }
    int checksum = firstCharacter.getChecksumPortion();
    int s = 2;
    for (int i = 1; i < this.pairs.size(); ++i) {
        ExpandedPair currentPair = this.pairs.get(i);
        checksum += currentPair.getLeftChar().getChecksumPortion();
        s++;
        DataCharacter currentRightChar = currentPair.getRightChar();
        if (currentRightChar != null) {
            checksum += currentRightChar.getChecksumPortion();
            s++;
        }
    }
    checksum %= 211;
    int checkCharacterValue = 211 * (s - 4) + checksum;
    return checkCharacterValue == checkCharacter.getValue();
}
Also used : ResultPoint(com.google.zxing.ResultPoint) DataCharacter(com.google.zxing.oned.rss.DataCharacter)

Example 5 with DataCharacter

use of com.google.zxing.oned.rss.DataCharacter in project zxing by zxing.

the class RSSExpandedInternalTestCase method testDecodeDataCharacter.

@Test
public void testDecodeDataCharacter() throws Exception {
    BufferedImage image = readImage("3.png");
    BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));
    BitArray row = binaryMap.getBlackRow(binaryMap.getHeight() / 2, null);
    //image pixels where the A1 pattern starts (at 124) and ends (at 214)
    int[] startEnd = { 145, 243 };
    // A
    int value = 0;
    FinderPattern finderPatternA1 = new FinderPattern(value, startEnd, startEnd[0], startEnd[1], image.getHeight() / 2);
    //{1, 8, 4, 1, 1};
    RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
    DataCharacter dataCharacter = rssExpandedReader.decodeDataCharacter(row, finderPatternA1, true, false);
    assertEquals(19, dataCharacter.getValue());
    assertEquals(1007, dataCharacter.getChecksumPortion());
}
Also used : GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) FinderPattern(com.google.zxing.oned.rss.FinderPattern) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BitArray(com.google.zxing.common.BitArray) BinaryBitmap(com.google.zxing.BinaryBitmap) BufferedImage(java.awt.image.BufferedImage) DataCharacter(com.google.zxing.oned.rss.DataCharacter) Test(org.junit.Test)

Aggregations

DataCharacter (com.google.zxing.oned.rss.DataCharacter)9 ResultPoint (com.google.zxing.ResultPoint)6 FinderPattern (com.google.zxing.oned.rss.FinderPattern)4 BinaryBitmap (com.google.zxing.BinaryBitmap)2 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)2 NotFoundException (com.google.zxing.NotFoundException)2 BitArray (com.google.zxing.common.BitArray)2 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)2 BufferedImage (java.awt.image.BufferedImage)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1