rss expanded add recognition

This commit is contained in:
Eism 2019-07-17 19:11:11 +02:00
parent ef0588198f
commit 635cfab83c
47 changed files with 3965 additions and 0 deletions

View File

@ -110,6 +110,29 @@ HEADERS += $$PWD/QZXing_global.h \
$$PWD/zxing/zxing/oned/rss/Pair.h \ $$PWD/zxing/zxing/oned/rss/Pair.h \
$$PWD/zxing/zxing/oned/rss/RSS14Reader.h \ $$PWD/zxing/zxing/oned/rss/RSS14Reader.h \
$$PWD/zxing/zxing/oned/rss/RSSUtils.h \ $$PWD/zxing/zxing/oned/rss/RSSUtils.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AbstractExpandedDecoder.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI013103decoder.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI01320xDecoder.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI01392xDecoder.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI01393xDecoder.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI013x0x1xDecoder.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI013x0xDecoder.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI01AndOtherAIs.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI01decoder.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI01weightDecoder.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AnyAIDecoder.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/BlockParsedResult.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/CurrentParsingState.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/DecodedChar.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/DecodedInformation.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/DecodedNumeric.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/DecodedObject.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/FieldParser.h \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/GeneralAppIdDecoder.h \
$$PWD/zxing/zxing/oned/rss/expanded/BitArrayBuilder.h \
$$PWD/zxing/zxing/oned/rss/expanded/ExpandedPair.h \
$$PWD/zxing/zxing/oned/rss/expanded/ExpandedRow.h \
$$PWD/zxing/zxing/oned/rss/expanded/RSSExpandedReader.h \
$$PWD/zxing/zxing/qrcode/Version.h \ $$PWD/zxing/zxing/qrcode/Version.h \
$$PWD/zxing/zxing/qrcode/QRCodeReader.h \ $$PWD/zxing/zxing/qrcode/QRCodeReader.h \
$$PWD/zxing/zxing/qrcode/FormatInformation.h \ $$PWD/zxing/zxing/qrcode/FormatInformation.h \
@ -233,6 +256,29 @@ SOURCES += $$PWD/CameraImageWrapper.cpp \
$$PWD/zxing/zxing/oned/rss/Pair.cpp \ $$PWD/zxing/zxing/oned/rss/Pair.cpp \
$$PWD/zxing/zxing/oned/rss/RSS14Reader.cpp \ $$PWD/zxing/zxing/oned/rss/RSS14Reader.cpp \
$$PWD/zxing/zxing/oned/rss/RSSUtils.cpp \ $$PWD/zxing/zxing/oned/rss/RSSUtils.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/BitArrayBuilder.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/ExpandedPair.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/ExpandedRow.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/RSSExpandedReader.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AbstractExpandedDecoder.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI01AndOtherAIs.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI01decoder.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI01weightDecoder.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI013x0x1xDecoder.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI013x0xDecoder.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI01320xDecoder.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI01392xDecoder.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI01393xDecoder.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AI013103decoder.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/AnyAIDecoder.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/BlockParsedResult.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/CurrentParsingState.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/DecodedChar.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/DecodedInformation.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/DecodedNumeric.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/DecodedObject.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/FieldParser.cpp \
$$PWD/zxing/zxing/oned/rss/expanded/decoders/GeneralAppIdDecoder.cpp \
$$PWD/zxing/zxing/qrcode/QRCodeReader.cpp \ $$PWD/zxing/zxing/qrcode/QRCodeReader.cpp \
$$PWD/zxing/zxing/multi/MultipleBarcodeReader.cpp \ $$PWD/zxing/zxing/multi/MultipleBarcodeReader.cpp \
$$PWD/zxing/zxing/multi/GenericMultipleBarcodeReader.cpp \ $$PWD/zxing/zxing/multi/GenericMultipleBarcodeReader.cpp \

View File

@ -0,0 +1,54 @@
#include "BitArrayBuilder.h"
namespace zxing {
namespace oned {
namespace rss {
Ref<BitArray> BitArrayBuilder::buildBitArray(std::vector<ExpandedPair> pairs)
{
int charNumber = static_cast<int>(pairs.size() * 2) - 1;
if (pairs[pairs.size() - 1].getRightChar().getValue() == 0) {
charNumber -= 1;
}
int size = 12 * charNumber;
Ref<BitArray> binary(new BitArray(int(size)));
int accPos = 0;
ExpandedPair firstPair = pairs[0];
int firstValue = firstPair.getRightChar().getValue();
for (int i = 11; i >= 0; --i) {
if ((firstValue & (1 << i)) != 0) {
binary->set(accPos);
}
accPos++;
}
for (size_t i = 1; i < pairs.size(); ++i) {
ExpandedPair currentPair = pairs[i];
int leftValue = currentPair.getLeftChar().getValue();
for (int j = 11; j >= 0; --j) {
if ((leftValue & (1 << j)) != 0) {
binary->set(accPos);
}
accPos++;
}
if (currentPair.getRightChar().getValue() != 0) {
int rightValue = currentPair.getRightChar().getValue();
for (int j = 11; j >= 0; --j) {
if ((rightValue & (1 << j)) != 0) {
binary->set(accPos);
}
accPos++;
}
}
}
return binary;
}
}
}
}

View File

@ -0,0 +1,54 @@
#ifndef BIT_ARRAY_BUILDER_H
#define BIT_ARRAY_BUILDER_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include <zxing/common/BitArray.h>
#include "ExpandedPair.h"
#include <vector>
namespace zxing {
namespace oned {
namespace rss {
class BitArrayBuilder
{
public:
static Ref<BitArray> buildBitArray(std::vector<ExpandedPair> pairs);
};
}
}
}
#endif

View File

@ -0,0 +1,64 @@
#include "ExpandedPair.h"
namespace zxing {
namespace oned {
namespace rss {
ExpandedPair::ExpandedPair(DataCharacter leftChar, DataCharacter rightChar, FinderPattern finderPattern)
: m_leftChar(leftChar),
m_rightChar(rightChar),
m_finderPattern(finderPattern)
{
}
ExpandedPair::ExpandedPair(const ExpandedPair *other)
{
m_leftChar = other != nullptr ? other->m_leftChar : DataCharacter();
m_rightChar = other != nullptr ? other->m_rightChar : DataCharacter();
m_finderPattern = other != nullptr ? other->m_finderPattern : nullptr;
}
DataCharacter& ExpandedPair::getLeftChar()
{
return m_leftChar;
}
DataCharacter& ExpandedPair::getRightChar()
{
return m_rightChar;
}
FinderPattern& ExpandedPair::getFinderPattern()
{
return m_finderPattern;
}
bool ExpandedPair::mustBeLast() const
{
return m_rightChar.getValue() == 0;
}
String ExpandedPair::toString() const
{
return String(String("[ ").getText() + m_leftChar.toString().getText() + String(" , ").getText() +
m_rightChar.toString().getText() + " : " +
(m_finderPattern.getValue() != 0 ? "null" : std::to_string(m_finderPattern.getValue())) + " ]");
}
bool ExpandedPair::equals(const ExpandedPair &other) const
{
return m_leftChar.equals(other.m_leftChar) &&
m_rightChar.equals(other.m_rightChar) &&
m_finderPattern.equals(other.m_finderPattern);
}
int ExpandedPair::hashCode() const
{
return m_leftChar.hashCode() & m_rightChar.hashCode() & m_finderPattern.hashCode();
}
}
}
}

View File

@ -0,0 +1,77 @@
#ifndef EXPANDED_PAIR_H
#define EXPANDED_PAIR_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include <zxing/oned/rss/FinderPattern.h>
#include <zxing/oned/rss/DataCharacter.h>
#include <zxing/common/Str.h>
namespace zxing {
namespace oned {
namespace rss {
class ExpandedPair
{
public:
ExpandedPair(DataCharacter leftChar,
DataCharacter rightChar,
FinderPattern finderPattern);
ExpandedPair(const ExpandedPair* other = nullptr);
DataCharacter &getLeftChar();
DataCharacter &getRightChar();
FinderPattern &getFinderPattern();
bool mustBeLast() const;
String toString() const;
bool equals(const ExpandedPair& other) const;
int hashCode() const;
private:
DataCharacter m_leftChar;
DataCharacter m_rightChar;
FinderPattern m_finderPattern;
};
}
}
}
#endif

View File

@ -0,0 +1,64 @@
#include "ExpandedRow.h"
namespace zxing {
namespace oned {
namespace rss {
ExpandedRow::ExpandedRow(std::vector<ExpandedPair> pairs, int rowNumber, bool wasReversed)
: m_pairs(pairs),
m_rowNumber(rowNumber),
m_wasReversed(wasReversed)
{
}
ExpandedRow::ExpandedRow(const ExpandedRow *other)
{
m_pairs = other != nullptr ? other->m_pairs : std::vector<ExpandedPair>();
m_rowNumber = other != nullptr ? other->m_rowNumber : 0;
m_wasReversed = other != nullptr ? other->m_wasReversed : false;
}
std::vector<ExpandedPair> &ExpandedRow::getPairs()
{
return m_pairs;
}
int ExpandedRow::getRowNumber()
{
return m_rowNumber;
}
bool ExpandedRow::isEquivalent(std::vector<ExpandedPair> otherPairs) const
{
if (m_pairs.size() != otherPairs.size()) {
return false;
}
for (size_t i = 0; i < m_pairs.size(); i++) {
if (!m_pairs[i].equals(otherPairs[i])) {
return false;
}
}
return true;
}
String ExpandedRow::toString()
{
String result("{ ");
for (auto i : m_pairs) {
result.append(i.toString().getText());
}
result.append(" }");
return result;
}
bool ExpandedRow::equals(const ExpandedRow &other) const
{
return isEquivalent(other.m_pairs) && m_wasReversed == other.m_wasReversed;
}
}
}
}

View File

@ -0,0 +1,67 @@
#ifndef EXPANDED_ROW_H
#define EXPANDED_ROW_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
#include "ExpandedPair.h"
#include <vector>
namespace zxing {
namespace oned {
namespace rss {
/**
* One row of an RSS Expanded Stacked symbol, consisting of 1+ expanded pairs.
*/
class ExpandedRow
{
public:
ExpandedRow(std::vector<ExpandedPair> pairs, int rowNumber, bool wasReversed);
ExpandedRow(const ExpandedRow* other = nullptr);
std::vector<ExpandedPair>& getPairs();
int getRowNumber();
bool isEquivalent(std::vector<ExpandedPair> otherPairs) const;
String toString();
/**
* Two rows are equal if they contain the same pairs in the same order.
*/
bool equals(const ExpandedRow& other) const;
private:
std::vector<ExpandedPair> m_pairs;
int m_rowNumber;
/** Did this row of the image have to be reversed (mirrored) to recognize the pairs? */
bool m_wasReversed;
};
}
}
}
#endif

View File

@ -0,0 +1,750 @@
#include "RSSExpandedReader.h"
namespace zxing {
namespace oned {
namespace rss {
using ::zxing::common::detector::MathUtils;
static const std::vector<int> SYMBOL_WIDEST = {7, 5, 4, 3, 1};
static const std::vector<int> EVEN_TOTAL_SUBSET = {4, 20, 52, 104, 204};
static const std::vector<int> GSUM = {0, 348, 1388, 2948, 3988};
const int FINDER_PATTERNS_[][4] = {
{1,8,4,1}, // A
{3,6,4,1}, // B
{3,4,6,1}, // C
{3,2,8,1}, // D
{2,6,5,1}, // E
{2,2,9,1} // F
};
#define VECTOR_INIT(v) v, v + sizeof(v)/sizeof(v[0])
const std::vector<int const*>
RSSExpandedReader::FINDER_PATTERNS (VECTOR_INIT(FINDER_PATTERNS_));
static const std::vector<std::vector<int>> WEIGHTS = {
{ 1, 3, 9, 27, 81, 32, 96, 77},
{ 20, 60, 180, 118, 143, 7, 21, 63},
{189, 145, 13, 39, 117, 140, 209, 205},
{193, 157, 49, 147, 19, 57, 171, 91},
{ 62, 186, 136, 197, 169, 85, 44, 132},
{185, 133, 188, 142, 4, 12, 36, 108},
{113, 128, 173, 97, 80, 29, 87, 50},
{150, 28, 84, 41, 123, 158, 52, 156},
{ 46, 138, 203, 187, 139, 206, 196, 166},
{ 76, 17, 51, 153, 37, 111, 122, 155},
{ 43, 129, 176, 106, 107, 110, 119, 146},
{ 16, 48, 144, 10, 30, 90, 59, 177},
{109, 116, 137, 200, 178, 112, 125, 164},
{ 70, 210, 208, 202, 184, 130, 179, 115},
{134, 191, 151, 31, 93, 68, 204, 190},
{148, 22, 66, 198, 172, 94, 71, 2},
{ 6, 18, 54, 162, 64, 192,154, 40},
{120, 149, 25, 75, 14, 42,126, 167},
{ 79, 26, 78, 23, 69, 207,199, 175},
{103, 98, 83, 38, 114, 131, 182, 124},
{161, 61, 183, 127, 170, 88, 53, 159},
{ 55, 165, 73, 8, 24, 72, 5, 15},
{ 45, 135, 194, 160, 58, 174, 100, 89}
};
static const int FINDER_PAT_A = 0;
static const int FINDER_PAT_B = 1;
static const int FINDER_PAT_C = 2;
static const int FINDER_PAT_D = 3;
static const int FINDER_PAT_E = 4;
static const int FINDER_PAT_F = 5;
static std::vector<std::vector<int>> FINDER_PATTERN_SEQUENCES = {
{ FINDER_PAT_A, FINDER_PAT_A },
{ FINDER_PAT_A, FINDER_PAT_B, FINDER_PAT_B },
{ FINDER_PAT_A, FINDER_PAT_C, FINDER_PAT_B, FINDER_PAT_D },
{ FINDER_PAT_A, FINDER_PAT_E, FINDER_PAT_B, FINDER_PAT_D, FINDER_PAT_C },
{ FINDER_PAT_A, FINDER_PAT_E, FINDER_PAT_B, FINDER_PAT_D, FINDER_PAT_D, FINDER_PAT_F },
{ FINDER_PAT_A, FINDER_PAT_E, FINDER_PAT_B, FINDER_PAT_D, FINDER_PAT_E, FINDER_PAT_F, FINDER_PAT_F },
{ FINDER_PAT_A, FINDER_PAT_A, FINDER_PAT_B, FINDER_PAT_B, FINDER_PAT_C, FINDER_PAT_C, FINDER_PAT_D, FINDER_PAT_D },
{ FINDER_PAT_A, FINDER_PAT_A, FINDER_PAT_B, FINDER_PAT_B, FINDER_PAT_C, FINDER_PAT_C, FINDER_PAT_D, FINDER_PAT_E, FINDER_PAT_E },
{ FINDER_PAT_A, FINDER_PAT_A, FINDER_PAT_B, FINDER_PAT_B, FINDER_PAT_C, FINDER_PAT_C, FINDER_PAT_D, FINDER_PAT_E, FINDER_PAT_F, FINDER_PAT_F },
{ FINDER_PAT_A, FINDER_PAT_A, FINDER_PAT_B, FINDER_PAT_B, FINDER_PAT_C, FINDER_PAT_D, FINDER_PAT_D, FINDER_PAT_E, FINDER_PAT_E, FINDER_PAT_F, FINDER_PAT_F },
};
static const int MAX_PAIRS = 11;
RSSExpandedReader::RSSExpandedReader()
: m_pairs(std::vector<ExpandedPair>(MAX_PAIRS)), m_rows(std::vector<ExpandedRow>()),
m_startEnd(std::vector<int>(2)), m_startFromEven(false)
{
}
Ref<Result> RSSExpandedReader::decodeRow(int rowNumber, Ref<BitArray> row, DecodeHints /*hints*/)
{
// Rows can start with even pattern in case in prev rows there where odd number of patters.
// So lets try twice
m_pairs.clear();
m_startFromEven = false;
try {
return constructResult(decodeRow2pairs(rowNumber, row));
} catch (NotFoundException e) {
// OK
}
m_pairs.clear();
m_startFromEven = true;
return constructResult(decodeRow2pairs(rowNumber, row));
}
void RSSExpandedReader::reset()
{
m_pairs.clear();
m_rows.clear();
}
std::vector<ExpandedPair> RSSExpandedReader::decodeRow2pairs(int rowNumber, Ref<BitArray> row)
{
bool done = false;
while (!done) {
try {
m_pairs.push_back(retrieveNextPair(row, m_pairs, rowNumber));
} catch (NotFoundException nfe) {
if (m_pairs.size() == 0) {
throw nfe;
}
// exit this loop when retrieveNextPair() fails and throws
done = true;
}
}
// TODO: verify sequence of finder patterns as in checkPairSequence()
if (checkChecksum()) {
return m_pairs;
}
bool tryStackedDecode = !(m_rows.size() == 0);
storeRow(rowNumber, false); // TODO: deal with reversed rows
if (tryStackedDecode) {
// When the image is 180-rotated, then rows are sorted in wrong direction.
// Try twice with both the directions.
std::vector<ExpandedPair> ps = checkRows(false);
if (ps.size() != 0) {
return ps;
}
ps = checkRows(true);
if (ps.size() != 0) {
return ps;
}
}
throw NotFoundException();
}
std::vector<ExpandedPair> RSSExpandedReader::checkRows(bool reverse)
{
// Limit number of rows we are checking
// We use recursive algorithm with pure complexity and don't want it to take forever
// Stacked barcode can have up to 11 rows, so 25 seems reasonable enough
if (m_rows.size() > 25) {
m_rows.clear(); // We will never have a chance to get result, so clear it
return {};
}
m_pairs.clear();
if (reverse) {
std::reverse(m_rows.begin(), m_rows.end());
}
std::vector<ExpandedPair> ps;
try {
ps = checkRows({}, 0);
} catch (NotFoundException e) {
// OK
}
if (reverse) {
std::reverse(m_rows.begin(), m_rows.end());
}
return ps;
}
std::vector<ExpandedPair> RSSExpandedReader::checkRows(std::vector<ExpandedRow> collectedRows, int currentRow)
{
for (size_t i = static_cast<size_t>(currentRow); i < m_rows.size(); i++) {
ExpandedRow row = m_rows[i];
m_pairs.clear();
for (ExpandedRow collectedRow : collectedRows) {
std::vector<ExpandedPair> collectedRowPairs = collectedRow.getPairs();
m_pairs.insert(m_pairs.end(), collectedRowPairs.begin(),
collectedRowPairs.end());
}
std::vector<ExpandedPair> rowPairs = row.getPairs();
m_pairs.insert(m_pairs.end(), rowPairs.begin(),
rowPairs.end());
if (isValidSequence(m_pairs)) {
if (checkChecksum()) {
return m_pairs;
}
std::vector<ExpandedRow> rs(collectedRows);
rs.push_back(row);
try {
// Recursion: try to add more rows
return checkRows(rs, static_cast<int>(i + 1));
} catch (NotFoundException e) {
// We failed, try the next candidate
}
}
}
throw NotFoundException();
}
bool RSSExpandedReader::isValidSequence(std::vector<ExpandedPair> pairs)
{
for (std::vector<int> sequence : FINDER_PATTERN_SEQUENCES) {
if (pairs.size() <= sequence.size()) {
bool stop = true;
for (size_t j = 0; j < pairs.size(); j++) {
if (pairs[j].getFinderPattern().getValue() != sequence[j]) {
stop = false;
break;
}
}
if (stop) {
return true;
}
}
}
return false;
}
void RSSExpandedReader::storeRow(int rowNumber, bool wasReversed)
{
// Discard if duplicate above or below; otherwise insert in order by row number.
size_t insertPos = 0;
bool prevIsSame = false;
bool nextIsSame = false;
while (insertPos < m_rows.size()) {
ExpandedRow erow = m_rows[insertPos];
if (erow.getRowNumber() > rowNumber) {
nextIsSame = erow.isEquivalent(m_pairs);
break;
}
prevIsSame = erow.isEquivalent(m_pairs);
insertPos++;
}
if (nextIsSame || prevIsSame) {
return;
}
// When the row was partially decoded (e.g. 2 pairs found instead of 3),
// it will prevent us from detecting the barcode.
// Try to merge partial rows
// Check whether the row is part of an already detected row
if (isPartialRow(m_pairs, m_rows)) {
return;
}
m_rows.insert(m_rows.begin() + insertPos, ExpandedRow(m_pairs, rowNumber, wasReversed));
removePartialRows();
}
void RSSExpandedReader::removePartialRows()
{
for (size_t i = 0; i < m_rows.size(); i++) {
if (m_rows[i].getPairs().size() != m_pairs.size()) {
bool allFound = true;
for (ExpandedPair p : m_rows[i].getPairs()) {
bool found = false;
for (ExpandedPair pp : m_pairs) {
if (p.equals(pp))
{
found = true;
break;
}
}
if (!found)
{
allFound = false;
break;
}
}
if (allFound) {
// 'pairs' contains all the pairs from the row 'r'
m_rows.erase(m_rows.begin() + i--);
}
}
}
}
bool RSSExpandedReader::isPartialRow(std::vector<ExpandedPair>& pairs, std::vector<ExpandedRow>& rows)
{
for (ExpandedRow r : rows) {
bool allFound = true;
for (ExpandedPair p : pairs) {
bool found = false;
for (ExpandedPair pp : r.getPairs()) {
if (p.equals(pp)) {
found = true;
break;
}
}
if (!found) {
allFound = false;
break;
}
}
if (allFound) {
// the row 'r' contain all the pairs from 'pairs'
return true;
}
}
return false;
}
std::vector<ExpandedRow> RSSExpandedReader::getRows() const
{
return m_rows;
}
Ref<Result> RSSExpandedReader::constructResult(std::vector<ExpandedPair> pairs)
{
Ref<BitArray> binary = BitArrayBuilder::buildBitArray(pairs);
AbstractExpandedDecoder* decoder = AbstractExpandedDecoder::createDecoder(binary);
String resultingString = decoder->parseInformation();
ArrayRef< Ref<ResultPoint> > firstPoints = pairs[0].getFinderPattern().getResultPoints();
ArrayRef< Ref<ResultPoint> > lastPoints = pairs[pairs.size() - 1].getFinderPattern().getResultPoints();
ArrayRef< Ref<ResultPoint> > resultPoints(4);
resultPoints[0] = firstPoints[0];
resultPoints[1] = firstPoints[1];
resultPoints[2] = lastPoints[0];
resultPoints[3] = lastPoints[1];
return Ref<Result>(new Result(
Ref<String>(new String(resultingString)),
nullptr,
resultPoints,
BarcodeFormat::RSS_EXPANDED
));
}
bool RSSExpandedReader::checkChecksum()
{
ExpandedPair firstPair = m_pairs[0];
DataCharacter checkCharacter = firstPair.getLeftChar();
DataCharacter firstCharacter = firstPair.getRightChar();
if (firstCharacter.getChecksumPortion() == 0) {
return false;
}
int checksum = firstCharacter.getChecksumPortion();
int s = 2;
for (size_t i = 1; i < m_pairs.size(); ++i) {
ExpandedPair currentPair = m_pairs[i];
checksum += currentPair.getLeftChar().getChecksumPortion();
s++;
DataCharacter currentRightChar = currentPair.getRightChar();
if (currentRightChar.getValue() != 0) {
checksum += currentRightChar.getChecksumPortion();
s++;
}
}
checksum %= 211;
int checkCharacterValue = 211 * (s - 4) + checksum;
return checkCharacterValue == checkCharacter.getValue();
}
int RSSExpandedReader::getNextSecondBar(Ref<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;
}
ExpandedPair RSSExpandedReader::retrieveNextPair(Ref<BitArray> row, std::vector<ExpandedPair>& previousPairs, int rowNumber)
{
bool isOddPattern = previousPairs.size() % 2 == 0;
if (m_startFromEven) {
isOddPattern = !isOddPattern;
}
FinderPattern pattern;
bool keepFinding = true;
int forcedOffset = -1;
do {
findNextPair(row, previousPairs, forcedOffset);
pattern = parseFoundFinderPattern(row, rowNumber, isOddPattern);
if (!pattern.isValid()) {
forcedOffset = getNextSecondBar(row, m_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.
// bool mayBeLast = checkPairSequence(previousPairs, pattern);
DataCharacter leftChar = decodeDataCharacter(row, pattern, isOddPattern, true);
if (!(previousPairs.size() == 0) && previousPairs[previousPairs.size() - 1].mustBeLast()) {
throw NotFoundException();
}
DataCharacter rightChar;
try {
rightChar = decodeDataCharacter(row, pattern, isOddPattern, false);
} catch (NotFoundException ignored) {
//rightChar = nullptr;
}
return ExpandedPair(leftChar, rightChar, pattern);
}
void RSSExpandedReader::findNextPair(Ref<BitArray> row, std::vector<ExpandedPair> previousPairs, int forcedOffset)
{
std::vector<int>& counters = getDecodeFinderCounters();
counters[0] = 0;
counters[1] = 0;
counters[2] = 0;
counters[3] = 0;
int width = row->getSize();
int rowOffset;
if (forcedOffset >= 0) {
rowOffset = forcedOffset;
} else if (previousPairs.size() == 0) {
rowOffset = 0;
} else {
ExpandedPair lastPair = previousPairs[previousPairs.size() - 1];
rowOffset = lastPair.getFinderPattern().getStartEnd()[1];
}
bool searchingEvenPair = previousPairs.size() % 2 != 0;
if (m_startFromEven) {
searchingEvenPair = !searchingEvenPair;
}
bool isWhite = false;
while (rowOffset < width) {
isWhite = !row->get(rowOffset);
if (!isWhite) {
break;
}
rowOffset++;
}
size_t counterPosition = 0;
int patternStart = rowOffset;
for (int x = rowOffset; x < width; x++) {
if (row->get(x) != isWhite) {
counters[counterPosition]++;
} else {
if (counterPosition == 3) {
if (searchingEvenPair) {
std::reverse(counters.begin(), counters.end());
}
if (isFinderPattern(counters)) {
m_startEnd[0] = patternStart;
m_startEnd[1] = x;
return;
}
if (searchingEvenPair) {
std::reverse(counters.begin(), counters.end());
}
patternStart += counters[0] + counters[1];
counters[0] = counters[2];
counters[1] = counters[3];
counters[2] = 0;
counters[3] = 0;
counterPosition--;
} else {
counterPosition++;
}
counters[counterPosition] = 1;
isWhite = !isWhite;
}
}
throw NotFoundException();
}
FinderPattern RSSExpandedReader::parseFoundFinderPattern(Ref<BitArray> row, int rowNumber, bool oddPattern)
{
// Actually we found elements 2-5.
int firstCounter;
int start;
int end;
if (oddPattern) {
// If pattern number is odd, we need to locate element 1 *before* the current block.
int firstElementStart = m_startEnd[0] - 1;
// Locate element 1
while (firstElementStart >= 0 && !row->get(firstElementStart)) {
firstElementStart--;
}
firstElementStart++;
firstCounter = m_startEnd[0] - firstElementStart;
start = firstElementStart;
end = m_startEnd[1];
} else {
// If pattern number is even, the pattern is reversed, so we need to locate element 1 *after* the current block.
start = m_startEnd[0];
end = row->getNextUnset(m_startEnd[1] + 1);
firstCounter = end - m_startEnd[1];
}
// Make 'counters' hold 1-4
std::vector<int>& counters = getDecodeFinderCounters();
std::vector<int> _counters = counters;
for (size_t i = 1; i < counters.size(); i++) {
counters[i] = _counters[i - 1];
}
counters[0] = firstCounter;
int value;
try {
value = parseFinderValue(counters, FINDER_PATTERNS);
} catch (NotFoundException ignored) {
return FinderPattern();
}
return FinderPattern(value, {start, end}, start, end, rowNumber);
}
DataCharacter RSSExpandedReader::decodeDataCharacter(Ref<BitArray> row, FinderPattern pattern, bool isOddPattern, bool leftChar)
{
std::vector<int>& counters = getDataCharacterCounters();
for (size_t x = 0; x < counters.size(); x++) {
counters[x] = 0;
}
if (leftChar) {
recordPatternInReverse(row, pattern.getStartEnd()[0], counters);
} else {
recordPattern(row, pattern.getStartEnd()[1], counters);
// reverse it
for (size_t i = 0, j = counters.size() - 1; i < j; i++, j--) {
int temp = counters[i];
counters[i] = counters[j];
counters[j] = temp;
}
} //counters[] has the pixels of the module
int numModules = 17; //left and right data characters have all the same length
float elementWidth = MathUtils::sum(counters) / static_cast<float>(numModules);
// Sanity check: element width for pattern and the character should match
float expectedElementWidth = (pattern.getStartEnd()[1] - pattern.getStartEnd()[0]) / 15.0f;
if (std::abs(elementWidth - expectedElementWidth) / expectedElementWidth > 0.3f) {
throw NotFoundException();
}
std::vector<int>& oddCounts(getOddCounts());
std::vector<int>& evenCounts(getEvenCounts());
std::vector<float>& oddRoundingErrors(getOddRoundingErrors());
std::vector<float>& evenRoundingErrors(getEvenRoundingErrors());
for (size_t i = 0; i < counters.size(); i++) {
float value = 1.0f * counters[i] / elementWidth;
int count = static_cast<int>(value + 0.5f); // Round
if (count < 1) {
if (value < 0.3f) {
throw NotFoundException();
}
count = 1;
} else if (count > 8) {
if (value > 8.7f) {
throw NotFoundException();
}
count = 8;
}
size_t offset = i / 2;
if ((i & 0x01) == 0) {
oddCounts[offset] = count;
oddRoundingErrors[offset] = value - count;
} else {
evenCounts[offset] = count;
evenRoundingErrors[offset] = value - count;
}
}
adjustOddEvenCounts(numModules);
size_t weightRowNumber = static_cast<size_t>(4 * pattern.getValue() + (isOddPattern ? 0 : 2) + (leftChar ? 0 : 1) - 1);
int oddSum = 0;
int oddChecksumPortion = 0;
for (int i = oddCounts.size() - 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;
for (int i = evenCounts.size() - 1; i >= 0; i--) {
if (isNotA1left(pattern, isOddPattern, leftChar)) {
int weight = WEIGHTS[weightRowNumber][2 * i + 1];
evenChecksumPortion += evenCounts[i] * weight;
}
}
int checksumPortion = oddChecksumPortion + evenChecksumPortion;
if ((oddSum & 0x01) != 0 || oddSum > 13 || oddSum < 4) {
throw NotFoundException();
}
size_t group = static_cast<size_t>((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 DataCharacter(value, checksumPortion);
}
bool RSSExpandedReader::isNotA1left(FinderPattern pattern, bool isOddPattern, bool leftChar) {
// A1: pattern.getValue is 0 (A), and it's an oddPattern, and it is a left char
return !(pattern.getValue() == 0 && isOddPattern && leftChar);
}
void RSSExpandedReader::adjustOddEvenCounts(int numModules){
int oddSum = MathUtils::sum(getOddCounts());
int evenSum = MathUtils::sum(getEvenCounts());
bool incrementOdd = false;
bool decrementOdd = false;
if (oddSum > 13) {
decrementOdd = true;
} else if (oddSum < 4) {
incrementOdd = true;
}
bool incrementEven = false;
bool decrementEven = false;
if (evenSum > 13) {
decrementEven = true;
} else if (evenSum < 4) {
incrementEven = true;
}
int mismatch = oddSum + evenSum - numModules;
bool oddParityBad = (oddSum & 0x01) == 1;
bool evenParityBad = (evenSum & 0x01) == 0;
switch (mismatch) {
case 1:
if (oddParityBad) {
if (evenParityBad) {
throw NotFoundException();
}
decrementOdd = true;
} else {
if (!evenParityBad) {
throw NotFoundException();
}
decrementEven = true;
}
break;
case -1:
if (oddParityBad) {
if (evenParityBad) {
throw NotFoundException();
}
incrementOdd = true;
} else {
if (!evenParityBad) {
throw NotFoundException();
}
incrementEven = true;
}
break;
case 0:
if (oddParityBad) {
if (!evenParityBad) {
throw NotFoundException();
}
// Both bad
if (oddSum < evenSum) {
incrementOdd = true;
decrementEven = true;
} else {
decrementOdd = true;
incrementEven = true;
}
} else {
if (evenParityBad) {
throw NotFoundException();
}
// Nothing to do!
}
break;
default:
throw NotFoundException();
}
if (incrementOdd) {
if (decrementOdd) {
throw NotFoundException();
}
std::vector<int>& oddCounts(getOddCounts());
std::vector<float>& oddRoundingErrors(getOddRoundingErrors());
increment(oddCounts, oddRoundingErrors);
}
if (decrementOdd) {
std::vector<int>& oddCounts(getOddCounts());
std::vector<float>& oddRoundingErrors(getOddRoundingErrors());
decrement(oddCounts, oddRoundingErrors);
}
if (incrementEven) {
if (decrementEven) {
throw NotFoundException();
}
std::vector<int>& evenCounts(getEvenCounts());
std::vector<float>& oddRoundingErrors(getOddRoundingErrors());
increment(evenCounts, oddRoundingErrors);
}
if (decrementEven) {
std::vector<int>& evenCounts(getEvenCounts());
std::vector<float>& evenRoundingErrors(getEvenRoundingErrors());
decrement(evenCounts, evenRoundingErrors);
}
}
}
}
}

View File

@ -0,0 +1,121 @@
#ifndef RSS_EXPANDED_READER_H
#define RSS_EXPANDED_READER_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "ExpandedPair.h"
#include "ExpandedRow.h"
#include "BitArrayBuilder.h"
#include "decoders/AbstractExpandedDecoder.h"
#include <zxing/oned/rss/AbstractRSSReader.h>
#include <zxing/common/detector/MathUtils.h>
#include <zxing/oned/rss/RSSUtils.h>
#include <vector>
#include <algorithm>
namespace zxing {
namespace oned {
namespace rss {
class RSSExpandedReader : public AbstractRSSReader
{
public:
static const std::vector<int const*> FINDER_PATTERNS;
RSSExpandedReader();
Ref<Result> decodeRow(int rowNumber,
Ref<BitArray> row, DecodeHints);
void reset();
// Not private for testing
std::vector<ExpandedPair> decodeRow2pairs(int rowNumber, Ref<BitArray> row);
std::vector<ExpandedPair> checkRows(bool reverse);
// Try to construct a valid rows sequence
// Recursion is used to implement backtracking
std::vector<ExpandedPair> checkRows(std::vector<ExpandedRow> collectedRows, int currentRow);
// Whether the pairs form a valid find pattern sequence,
// either complete or a prefix
static bool isValidSequence(std::vector<ExpandedPair> pairs);
void storeRow(int rowNumber, bool wasReversed);
// Remove all the rows that contains only specified pairs
void removePartialRows();
// Returns true when one of the rows already contains all the pairs
static bool isPartialRow(std::vector<ExpandedPair> &pairs, std::vector<ExpandedRow> &rows);
// Only used for unit testing
std::vector<ExpandedRow> getRows() const;
// Not private for unit testing
static Ref<Result> constructResult(std::vector<ExpandedPair> pairs);
bool checkChecksum();
static int getNextSecondBar(Ref<BitArray> row, int initialPos);
// not private for testing
ExpandedPair retrieveNextPair(Ref<BitArray> row, std::vector<ExpandedPair> &previousPairs, int rowNumber);
void findNextPair(Ref<BitArray> row, std::vector<ExpandedPair> previousPairs, int forcedOffset);
FinderPattern parseFoundFinderPattern(Ref<BitArray> row, int rowNumber, bool oddPattern);
DataCharacter decodeDataCharacter(Ref<BitArray> row,
FinderPattern pattern,
bool isOddPattern,
bool leftChar);
static bool isNotA1left(FinderPattern pattern, bool isOddPattern, bool leftChar);
void adjustOddEvenCounts(int numModules);
private:
std::vector<ExpandedPair> m_pairs;
std::vector<ExpandedRow> m_rows;
std::vector<int> m_startEnd;
bool m_startFromEven;
};
}
}
}
#endif

View File

@ -0,0 +1,25 @@
#include "AI013103decoder.h"
namespace zxing {
namespace oned {
namespace rss {
AI013103decoder::AI013103decoder(Ref<BitArray> information)
: AI013x0xDecoder(information)
{
}
void AI013103decoder::addWeightCode(String &buf, int /*weight*/)
{
buf.append("(3103)");
}
int AI013103decoder::checkWeight(int weight)
{
return weight;
}
}
}
}

View File

@ -0,0 +1,57 @@
#ifndef AI013_103X_DECODER_H
#define AI013_103X_DECODER_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "AI013x0xDecoder.h"
#include <zxing/common/BitArray.h>
namespace zxing {
namespace oned {
namespace rss {
class AI013103decoder : public AI013x0xDecoder
{
public:
AI013103decoder(Ref<BitArray> information);
protected:
virtual void addWeightCode(String &buf, int) override;
protected:
virtual int checkWeight(int weight) override;
};
}
}
}
#endif

View File

@ -0,0 +1,32 @@
#include "AI01320xDecoder.h"
namespace zxing {
namespace oned {
namespace rss {
AI01320xDecoder::AI01320xDecoder(Ref<BitArray> information)
: AI013x0xDecoder(information)
{
}
void AI01320xDecoder::addWeightCode(String &buf, int weight)
{
if (weight < 10000) {
buf.append("(3202)");
} else {
buf.append("(3203)");
}
}
int AI01320xDecoder::checkWeight(int weight)
{
if (weight < 10000) {
return weight;
}
return weight - 10000;
}
}
}
}

View File

@ -0,0 +1,57 @@
#ifndef AI013_20X_DECODER_H
#define AI013_20X_DECODER_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "AI013x0xDecoder.h"
#include <zxing/common/BitArray.h>
namespace zxing {
namespace oned {
namespace rss {
class AI01320xDecoder : public AI013x0xDecoder
{
public:
AI01320xDecoder(Ref<BitArray> information);
protected:
virtual void addWeightCode(String &buf, int weight) override;
virtual int checkWeight(int weight) override;
};
}
}
}
#endif

View File

@ -0,0 +1,40 @@
#include "AI01392xDecoder.h"
namespace zxing {
namespace oned {
namespace rss {
AI01392xDecoder::AI01392xDecoder(Ref<BitArray> information)
: AI01decoder(information)
{
}
String AI01392xDecoder::parseInformation()
{
if (getInformation()->getSize() < HEADER_SIZE + GTIN_SIZE) {
throw NotFoundException();
}
String buf("");
encodeCompressedGtin(buf, HEADER_SIZE);
int lastAIdigit =
getGeneralDecoder().extractNumericValueFromBitArray(HEADER_SIZE + GTIN_SIZE, LAST_DIGIT_SIZE);
buf.append("(392");
buf.append(std::to_string(lastAIdigit));
buf.append(')');
String stub("");
DecodedInformation decodedInformation =
getGeneralDecoder().decodeGeneralPurposeField(HEADER_SIZE + GTIN_SIZE + LAST_DIGIT_SIZE, stub);
buf.append(decodedInformation.getNewString().getText());
return buf;
}
}
}
}

View File

@ -0,0 +1,61 @@
#ifndef AI01_392X_DECODER_H
#define AI01_392X_DECODER_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "AI01decoder.h"
#include "DecodedInformation.h"
#include <zxing/FormatException.h>
#include <zxing/NotFoundException.h>
#include <zxing/common/BitArray.h>
namespace zxing {
namespace oned {
namespace rss {
class AI01392xDecoder : public AI01decoder
{
static const int HEADER_SIZE = 5 + 1 + 2;
static const int LAST_DIGIT_SIZE = 2;
public:
AI01392xDecoder(Ref<BitArray> information);
virtual String parseInformation() override;
};
}
}
}
#endif

View File

@ -0,0 +1,55 @@
#include "AI01393xDecoder.h"
#include "DecodedInformation.h"
#include <zxing/FormatException.h>
#include <zxing/NotFoundException.h>
namespace zxing {
namespace oned {
namespace rss {
AI01393xDecoder::AI01393xDecoder(Ref<BitArray> information)
: AI01decoder(information)
{
}
String AI01393xDecoder::parseInformation()
{
if (getInformation()->getSize() < HEADER_SIZE + GTIN_SIZE) {
throw NotFoundException();
}
String buf("");
encodeCompressedGtin(buf, HEADER_SIZE);
int lastAIdigit =
getGeneralDecoder().extractNumericValueFromBitArray(HEADER_SIZE + GTIN_SIZE, LAST_DIGIT_SIZE);
buf.append("(393");
buf.append(std::to_string(lastAIdigit));
buf.append(')');
int firstThreeDigits = getGeneralDecoder().extractNumericValueFromBitArray(
HEADER_SIZE + GTIN_SIZE + LAST_DIGIT_SIZE, FIRST_THREE_DIGITS_SIZE);
if (firstThreeDigits / 100 == 0) {
buf.append('0');
}
if (firstThreeDigits / 10 == 0) {
buf.append('0');
}
buf.append(std::to_string(firstThreeDigits));
String stub("");
DecodedInformation generalInformation = getGeneralDecoder().decodeGeneralPurposeField(
HEADER_SIZE + GTIN_SIZE + LAST_DIGIT_SIZE + FIRST_THREE_DIGITS_SIZE, stub);
buf.append(generalInformation.getNewString().getText());
return buf;
}
}
}
}

View File

@ -0,0 +1,58 @@
#ifndef AI01_393X_DECODER_H
#define AI01_393X_DECODER_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "AI01decoder.h"
#include <zxing/common/BitArray.h>
namespace zxing {
namespace oned {
namespace rss {
class AI01393xDecoder : public AI01decoder
{
static const int HEADER_SIZE = 5 + 1 + 2;
static const int LAST_DIGIT_SIZE = 2;
static const int FIRST_THREE_DIGITS_SIZE = 10;
public:
AI01393xDecoder(Ref<BitArray> information);
virtual String parseInformation() override;
};
}
}
}
#endif

View File

@ -0,0 +1,74 @@
#include "AI013x0x1xDecoder.h"
namespace zxing {
namespace oned {
namespace rss {
AI013x0x1xDecoder::AI013x0x1xDecoder(Ref<BitArray> information, String firstAIdigits, String dateCode)
: AI01weightDecoder(information), m_dateCode(dateCode), m_firstAIdigits(firstAIdigits)
{
}
String AI013x0x1xDecoder::parseInformation()
{
if (getInformation()->getSize() != HEADER_SIZE + GTIN_SIZE + WEIGHT_SIZE + DATE_SIZE) {
throw NotFoundException();
}
String buf("");
encodeCompressedGtin(buf, HEADER_SIZE);
encodeCompressedWeight(buf, HEADER_SIZE + GTIN_SIZE, WEIGHT_SIZE);
encodeCompressedDate(buf, HEADER_SIZE + GTIN_SIZE + WEIGHT_SIZE);
return buf;
}
void AI013x0x1xDecoder::encodeCompressedDate(String &buf, int currentPos)
{
int numericDate = getGeneralDecoder().extractNumericValueFromBitArray(currentPos, DATE_SIZE);
if (numericDate == 38400) {
return;
}
buf.append('(');
buf.append(m_dateCode.getText());
buf.append(')');
int day = numericDate % 32;
numericDate /= 32;
int month = numericDate % 12 + 1;
numericDate /= 12;
int year = numericDate;
if (year / 10 == 0) {
buf.append('0');
}
buf.append(std::to_string(year));
if (month / 10 == 0) {
buf.append('0');
}
buf.append(std::to_string(month));
if (day / 10 == 0) {
buf.append('0');
}
buf.append(std::to_string(day));
}
void AI013x0x1xDecoder::addWeightCode(String &buf, int weight)
{
buf.append('(');
buf.append(m_firstAIdigits.getText());
buf.append(std::to_string(weight / 100000));
buf.append(')');
}
int AI013x0x1xDecoder::checkWeight(int weight)
{
return weight % 100000;
}
}
}
}

View File

@ -0,0 +1,70 @@
#ifndef AI013_X0X1X_DECODER_H
#define AI013_X0X1X_DECODER_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "AI01weightDecoder.h"
#include <zxing/NotFoundException.h>
#include <zxing/common/BitArray.h>
namespace zxing {
namespace oned {
namespace rss {
class AI013x0x1xDecoder : public AI01weightDecoder
{
static const int HEADER_SIZE = 7 + 1;
static const int WEIGHT_SIZE = 20;
static const int DATE_SIZE = 16;
public:
AI013x0x1xDecoder(Ref<BitArray> information, String firstAIdigits, String dateCode);
virtual String parseInformation() override;
private:
void encodeCompressedDate(String &buf, int currentPos);
protected:
virtual void addWeightCode(String &buf, int weight) override;
virtual int checkWeight(int weight) override;
private:
String m_dateCode;
String m_firstAIdigits;
};
}
}
}
#endif

View File

@ -0,0 +1,29 @@
#include "AI013x0xDecoder.h"
namespace zxing {
namespace oned {
namespace rss {
AI013x0xDecoder::AI013x0xDecoder(Ref<BitArray> information)
: AI01weightDecoder(information)
{
}
String AI013x0xDecoder::parseInformation()
{
if (getInformation()->getSize() != HEADER_SIZE + GTIN_SIZE + WEIGHT_SIZE) {
throw NotFoundException();
}
String buf("");
encodeCompressedGtin(buf, HEADER_SIZE);
encodeCompressedWeight(buf, HEADER_SIZE + GTIN_SIZE, WEIGHT_SIZE);
return buf;
}
}
}
}

View File

@ -0,0 +1,62 @@
#ifndef AI013_X0X_DECODER_H
#define AI013_X0X_DECODER_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "AI01weightDecoder.h"
#include <zxing/NotFoundException.h>
#include <zxing/common/BitArray.h>
namespace zxing {
namespace oned {
namespace rss {
/**
* @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
*/
class AI013x0xDecoder : public AI01weightDecoder
{
static const int HEADER_SIZE = 4 + 1;
static const int WEIGHT_SIZE = 15;
public:
AI013x0xDecoder(Ref<BitArray> information);
virtual String parseInformation() override;
};
}
}
}
#endif

View File

@ -0,0 +1,28 @@
#include "AI01AndOtherAIs.h"
namespace zxing {
namespace oned {
namespace rss {
AI01AndOtherAIs::AI01AndOtherAIs(Ref<BitArray> information)
: AI01decoder(information)
{
}
String AI01AndOtherAIs::parseInformation()
{
String buff("(01)");
int initialGtinPosition = buff.length();
int firstGtinDigit = getGeneralDecoder().extractNumericValueFromBitArray(HEADER_SIZE, 4);
buff.append(std::to_string(firstGtinDigit));
encodeCompressedGtinWithoutAI(buff, HEADER_SIZE + 4, initialGtinPosition);
return getGeneralDecoder().decodeAllCodes(buff, HEADER_SIZE + 44);
}
}
}
}

View File

@ -0,0 +1,59 @@
#ifndef AI01_AND_OTHER_AIS_H
#define AI01_AND_OTHER_AIS_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "AI01decoder.h"
#include <zxing/FormatException.h>
#include <zxing/NotFoundException.h>
#include <zxing/common/BitArray.h>
#include <zxing/common/Str.h>
namespace zxing {
namespace oned {
namespace rss {
class AI01AndOtherAIs : public AI01decoder
{
static const int HEADER_SIZE = 1 + 1 + 2; //first bit encodes the linkage flag,
//the second one is the encodation method, and the other two are for the variable length
public:
AI01AndOtherAIs(Ref<BitArray> information);
virtual String parseInformation() override;
};
}
}
}
#endif

View File

@ -0,0 +1,57 @@
#include "AI01decoder.h"
namespace zxing {
namespace oned {
namespace rss {
AI01decoder::AI01decoder(Ref<BitArray> information)
: AbstractExpandedDecoder(information)
{
}
void AI01decoder::encodeCompressedGtin(String &buf, int currentPos)
{
buf.append("(01)");
int initialPosition = buf.length();
buf.append('9');
encodeCompressedGtinWithoutAI(buf, currentPos, initialPosition);
}
void AI01decoder::encodeCompressedGtinWithoutAI(String &buf, int currentPos, int initialBufferPosition)
{
for (int i = 0; i < 4; ++i) {
int currentBlock = getGeneralDecoder().extractNumericValueFromBitArray(currentPos + 10 * i, 10);
if (currentBlock / 100 == 0) {
buf.append("0");
}
if (currentBlock / 10 == 0) {
buf.append("0");
}
buf.append(std::to_string(currentBlock));
}
appendCheckDigit(buf, initialBufferPosition);
}
void AI01decoder::appendCheckDigit(String &buf, int currentPos)
{
int checkDigit = 0;
for (int i = 0; i < 13; i++) {
int digit = buf.charAt(i + currentPos) - '0';
checkDigit += (i & 0x01) == 0 ? 3 * digit : digit;
}
checkDigit = 10 - (checkDigit % 10);
if (checkDigit == 10) {
checkDigit = 0;
}
buf.append(std::to_string(checkDigit));
}
}
}
}

View File

@ -0,0 +1,62 @@
#ifndef AI01_DECODER_H
#define AI01_DECODER_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "AbstractExpandedDecoder.h"
#include <zxing/common/BitArray.h>
#include <zxing/common/Str.h>
namespace zxing {
namespace oned {
namespace rss {
class AI01decoder : public AbstractExpandedDecoder
{
public:
static const int GTIN_SIZE = 40;
AI01decoder(Ref<BitArray> information);
void encodeCompressedGtin(String &buf, int currentPos);
void encodeCompressedGtinWithoutAI(String &buf, int currentPos, int initialBufferPosition);
static void appendCheckDigit(String &buf, int currentPos);
};
}
}
}
#endif

View File

@ -0,0 +1,32 @@
#include "AI01weightDecoder.h"
namespace zxing {
namespace oned {
namespace rss {
AI01weightDecoder::AI01weightDecoder(Ref<BitArray> information)
: AI01decoder(information)
{
}
void AI01weightDecoder::encodeCompressedWeight(String &buf, int currentPos, int weightSize)
{
int originalWeightNumeric = getGeneralDecoder().extractNumericValueFromBitArray(currentPos, weightSize);
addWeightCode(buf, originalWeightNumeric);
int weightNumeric = checkWeight(originalWeightNumeric);
int currentDivisor = 100000;
for (int i = 0; i < 5; ++i) {
if (weightNumeric / currentDivisor == 0) {
buf.append('0');
}
currentDivisor /= 10;
}
buf.append(std::to_string(weightNumeric));
}
}
}
}

View File

@ -0,0 +1,63 @@
#ifndef AI01_WEIGHT_DECODER_H
#define AI01_WEIGHT_DECODER_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "AI01decoder.h"
#include <zxing/common/BitArray.h>
#include <zxing/common/Str.h>
namespace zxing {
namespace oned {
namespace rss {
/**
* @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
*/
class AI01weightDecoder : public AI01decoder
{
public:
AI01weightDecoder(Ref<BitArray> information);
void encodeCompressedWeight(String &buf, int currentPos, int weightSize);
protected:
virtual void addWeightCode(String &buf, int weight) = 0;
virtual int checkWeight(int weight) = 0;
};
}
}
}
#endif

View File

@ -0,0 +1,70 @@
#include "AbstractExpandedDecoder.h"
#include "AI01AndOtherAIs.h"
#include "AI013x0x1xDecoder.h"
#include "AnyAIDecoder.h"
#include "AI013103decoder.h"
#include "AI01320xDecoder.h"
#include "AI01392xDecoder.h"
#include "AI01393xDecoder.h"
namespace zxing {
namespace oned {
namespace rss {
AbstractExpandedDecoder::AbstractExpandedDecoder(Ref<BitArray> information)
: m_information(information), m_generalDecoder(GeneralAppIdDecoder(information))
{
}
Ref<BitArray> AbstractExpandedDecoder::getInformation() const
{
return m_information;
}
GeneralAppIdDecoder AbstractExpandedDecoder::getGeneralDecoder()
{
return m_generalDecoder;
}
AbstractExpandedDecoder *AbstractExpandedDecoder::createDecoder(Ref<BitArray> information)
{
if (information->get(1)) {
return new AI01AndOtherAIs(information);
}
if (!information->get(2)) {
return new AnyAIDecoder(information);
}
int fourBitEncodationMethod = GeneralAppIdDecoder::extractNumericValueFromBitArray(information, 1, 4);
switch (fourBitEncodationMethod) {
case 4: return new AI013103decoder(information);
case 5: return new AI01320xDecoder(information);
}
int fiveBitEncodationMethod = GeneralAppIdDecoder::extractNumericValueFromBitArray(information, 1, 5);
switch (fiveBitEncodationMethod) {
case 12: return new AI01392xDecoder(information);
case 13: return new AI01393xDecoder(information);
}
int sevenBitEncodationMethod = GeneralAppIdDecoder::extractNumericValueFromBitArray(information, 1, 7);
switch (sevenBitEncodationMethod) {
case 56: return new AI013x0x1xDecoder(information, String("310"), String("11"));
case 57: return new AI013x0x1xDecoder(information, String("320"), String("11"));
case 58: return new AI013x0x1xDecoder(information, String("310"), String("13"));
case 59: return new AI013x0x1xDecoder(information, String("320"), String("13"));
case 60: return new AI013x0x1xDecoder(information, String("310"), String("15"));
case 61: return new AI013x0x1xDecoder(information, String("320"), String("15"));
case 62: return new AI013x0x1xDecoder(information, String("310"), String("17"));
case 63: return new AI013x0x1xDecoder(information, String("320"), String("17"));
}
throw IllegalStateException();
}
}
}
}

View File

@ -0,0 +1,71 @@
#ifndef ABSTRACT_EXPANDED_DECODER_H
#define ABSTRACT_EXPANDED_DECODER_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "GeneralAppIdDecoder.h"
#include <zxing/FormatException.h>
#include <zxing/NotFoundException.h>
#include <zxing/IllegalStateException.h>
#include <zxing/common/Str.h>
namespace zxing {
namespace oned {
namespace rss {
class AbstractExpandedDecoder
{
public:
AbstractExpandedDecoder(Ref<BitArray> information);
virtual ~AbstractExpandedDecoder() {}
Ref<BitArray> getInformation() const;
virtual GeneralAppIdDecoder getGeneralDecoder();
virtual String parseInformation() = 0;
static AbstractExpandedDecoder* createDecoder(Ref<BitArray> information);
protected:
Ref<BitArray> m_information;
GeneralAppIdDecoder m_generalDecoder;
};
}
}
}
#endif

View File

@ -0,0 +1,21 @@
#include "AnyAIDecoder.h"
namespace zxing {
namespace oned {
namespace rss {
AnyAIDecoder::AnyAIDecoder(Ref<BitArray> information)
: AbstractExpandedDecoder(information)
{
}
String AnyAIDecoder::parseInformation()
{
String buf("");
return getGeneralDecoder().decodeAllCodes(buf, HEADER_SIZE);
}
}
}
}

View File

@ -0,0 +1,57 @@
#ifndef ANY_AI_DECODER_H
#define ANY_AI_DECODER_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include <zxing/FormatException.h>
#include <zxing/NotFoundException.h>
#include <zxing/common/BitArray.h>
#include "AbstractExpandedDecoder.h"
namespace zxing {
namespace oned {
namespace rss {
class AnyAIDecoder : public AbstractExpandedDecoder
{
static const int HEADER_SIZE = 2 + 1 + 2;
public:
AnyAIDecoder(Ref<BitArray> information);
String parseInformation() override;
};
}
}
}
#endif

View File

@ -0,0 +1,37 @@
#include "BlockParsedResult.h"
namespace zxing {
namespace oned {
namespace rss {
BlockParsedResult::BlockParsedResult(bool finished)
: m_decodedInformation(nullptr), m_finished(finished)
{
}
BlockParsedResult::BlockParsedResult(const DecodedInformation &information, bool finished)
{
m_finished = finished;
m_decodedInformation = information;
}
BlockParsedResult::BlockParsedResult(const BlockParsedResult &other)
{
m_finished = other.m_finished;
m_decodedInformation = other.m_decodedInformation;
}
DecodedInformation BlockParsedResult::getDecodedInformation()
{
return m_decodedInformation;
}
bool BlockParsedResult::isFinished()
{
return m_finished;
}
}
}
}

View File

@ -0,0 +1,63 @@
#ifndef BLOCK_PARSED_RESULT_H
#define BLOCK_PARSED_RESULT_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "DecodedInformation.h"
namespace zxing {
namespace oned {
namespace rss {
class BlockParsedResult
{
public:
BlockParsedResult(bool finished);
BlockParsedResult(const DecodedInformation& information, bool finished);
BlockParsedResult(const BlockParsedResult& other);
DecodedInformation getDecodedInformation();
bool isFinished();
private:
DecodedInformation m_decodedInformation;
bool m_finished;
};
}
}
}
#endif

View File

@ -0,0 +1,60 @@
#include "CurrentParsingState.h"
namespace zxing {
namespace oned {
namespace rss {
CurrentParsingState::CurrentParsingState()
{
position = 0;
encoding = State::NUMERIC;
}
int CurrentParsingState::getPosition() const
{
return position;
}
void CurrentParsingState::setPosition(int _position)
{
position = _position;
}
void CurrentParsingState::incrementPosition(int delta)
{
position += delta;
}
bool CurrentParsingState::isAlpha() const
{
return encoding == State::ALPHA;
}
bool CurrentParsingState::isNumeric() const
{
return encoding == State::NUMERIC;
}
bool CurrentParsingState::isIsoIec646() const
{
return encoding == State::ISO_IEC_646;
}
void CurrentParsingState::setNumeric()
{
encoding = State::NUMERIC;
}
void CurrentParsingState::setAlpha()
{
encoding = State::ALPHA;
}
void CurrentParsingState::setIsoIec646()
{
encoding = State::ISO_IEC_646;
}
}
}
}

View File

@ -0,0 +1,78 @@
#ifndef CURRENT_PARSING_STATE_H
#define CURRENT_PARSING_STATE_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
namespace zxing {
namespace oned {
namespace rss {
class CurrentParsingState
{
public:
enum State {
NUMERIC,
ALPHA,
ISO_IEC_646
};
CurrentParsingState();
int getPosition() const;
void setPosition(int _position);
void incrementPosition(int delta);
bool isAlpha() const;
bool isNumeric() const;
bool isIsoIec646() const;
void setNumeric();
void setAlpha();
void setIsoIec646();
private:
int position;
State encoding;
};
}
}
}
#endif

View File

@ -0,0 +1,25 @@
#include "DecodedChar.h"
namespace zxing {
namespace oned {
namespace rss {
DecodedChar::DecodedChar(int newPosition, char value)
: DecodedObject (newPosition), m_value(value)
{
}
char DecodedChar::getValue() const
{
return m_value;
}
bool DecodedChar::isFNC1() const
{
return m_value == FNC1;
}
}
}
}

View File

@ -0,0 +1,61 @@
#ifndef DECODED_CHAR_H
#define DECODED_CHAR_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "DecodedObject.h"
namespace zxing {
namespace oned {
namespace rss {
class DecodedChar : public DecodedObject
{
public:
static const char FNC1 = '$'; // It's not in Alphanumeric neither in ISO/IEC 646 charset
DecodedChar(int newPosition, char value);
char getValue() const;
bool isFNC1() const;
private:
char m_value;
};
}
}
}
#endif

View File

@ -0,0 +1,51 @@
#include "DecodedInformation.h"
namespace zxing {
namespace oned {
namespace rss {
DecodedInformation::DecodedInformation(const DecodedInformation *other)
: DecodedObject (other == nullptr ? 0 : other->m_newPosition),
m_newString(other == nullptr ? String("") : other->m_newString)
{
m_newString = other == nullptr ? String("") : other->m_newString;
m_remaining = other == nullptr ? false : other->m_remaining;
m_remainingValue = other == nullptr ? 0 : other->m_remainingValue;
}
DecodedInformation::DecodedInformation(int newPosition, String newString)
: DecodedObject (newPosition),
m_newString(newString),
m_remainingValue(0),
m_remaining(false)
{
}
DecodedInformation::DecodedInformation(int newPosition, String newString, int remainingValue)
: DecodedObject (newPosition),
m_newString(newString),
m_remainingValue(remainingValue),
m_remaining(true)
{
}
String DecodedInformation::getNewString() const
{
return m_newString;
}
bool DecodedInformation::isRemaining() const
{
return m_remaining;
}
int DecodedInformation::getRemainingValue() const
{
return m_remainingValue;
}
}
}
}

View File

@ -0,0 +1,69 @@
#ifndef DECODED_INFORMATION_H
#define DECODED_INFORMATION_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "DecodedObject.h"
#include <zxing/common/Str.h>
namespace zxing {
namespace oned {
namespace rss {
class DecodedInformation : public DecodedObject
{
public:
DecodedInformation(const DecodedInformation* other = nullptr);
DecodedInformation(int newPosition, String newString);
DecodedInformation(int newPosition, String newString, int remainingValue);
String getNewString() const;
bool isRemaining() const;
int getRemainingValue() const;
private:
String m_newString;
int m_remainingValue;
bool m_remaining;
};
}
}
}
#endif

View File

@ -0,0 +1,55 @@
#include "DecodedNumeric.h"
namespace zxing {
namespace oned {
namespace rss {
DecodedNumeric::DecodedNumeric(int newPosition, int firstDigit, int secondDigit)
: DecodedObject(newPosition)
{
if (firstDigit < 0 || firstDigit > 10 || secondDigit < 0 || secondDigit > 10) {
throw FormatException::getFormatInstance();
}
m_newPosition = newPosition;
m_firstDigit = firstDigit;
m_secondDigit = secondDigit;
}
DecodedNumeric::DecodedNumeric(const DecodedNumeric *other)
: DecodedObject (other == nullptr ? 0 : other->m_newPosition)
{
m_newPosition = other == nullptr ? 0 : other->m_newPosition;
m_firstDigit = other == nullptr ? 0 : other->m_firstDigit;
m_secondDigit = other == nullptr ? 0 : other->m_secondDigit;
}
int DecodedNumeric::getFirstDigit() const
{
return m_firstDigit;
}
int DecodedNumeric::getSecondDigit() const
{
return m_secondDigit;
}
int DecodedNumeric::getValue() const
{
return m_firstDigit * 10 + m_secondDigit;
}
bool DecodedNumeric::isFirstDigitFNC1() const
{
return m_firstDigit == FNC1;
}
bool DecodedNumeric::isSecondDigitFNC1() const
{
return m_secondDigit == FNC1;
}
}
}
}

View File

@ -0,0 +1,71 @@
#ifndef DECODED_NUMERIC_H
#define DECODED_NUMERIC_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include "DecodedObject.h"
#include <zxing/FormatException.h>
namespace zxing {
namespace oned {
namespace rss {
class DecodedNumeric : public DecodedObject
{
public:
static const int FNC1 = 10;
DecodedNumeric(int newPosition, int firstDigit, int secondDigit);
DecodedNumeric(const DecodedNumeric* other = nullptr);
int getFirstDigit() const;
int getSecondDigit() const;
int getValue() const;
bool isFirstDigitFNC1() const;
bool isSecondDigitFNC1() const;
private:
int m_firstDigit;
int m_secondDigit;
};
}
}
}
#endif

View File

@ -0,0 +1,20 @@
#include "DecodedObject.h"
namespace zxing {
namespace oned {
namespace rss {
DecodedObject::DecodedObject(int newPosition)
: m_newPosition(newPosition)
{
}
int DecodedObject::getNewPosition() const
{
return m_newPosition;
}
}
}
}

View File

@ -0,0 +1,52 @@
#ifndef DECODED_OBJECT_H
#define DECODED_OBJECT_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
namespace zxing {
namespace oned {
namespace rss {
class DecodedObject
{
public:
DecodedObject(int newPosition);
int getNewPosition() const;
protected:
int m_newPosition;
};
}
}
}
#endif

View File

@ -0,0 +1,274 @@
#include "FieldParser.h"
namespace zxing {
namespace oned {
namespace rss {
static const int VARIABLE_LENGTH = 99999;
struct DigitData {
std::string digit;
int variableLength;
int length;
};
static const DigitData TWO_DIGIT_DATA_LENGTH[] {
// "DIGITS", new Integer(LENGTH)
// or
// "DIGITS", VARIABLE_LENGTH, new Integer(MAX_SIZE)
{ "00", 18, 0},
{ "01", 14, 0},
{ "02", 14, 0},
{ "10", VARIABLE_LENGTH, 20},
{ "11", 6, 0},
{ "12", 6, 0},
{ "13", 6, 0},
{ "15", 6, 0},
{ "17", 6, 0},
{ "20", 2, 0},
{ "21", VARIABLE_LENGTH, 20},
{ "22", VARIABLE_LENGTH, 29},
{ "30", VARIABLE_LENGTH, 8},
{ "37", VARIABLE_LENGTH, 8},
//internal company codes
{ "90", VARIABLE_LENGTH, 30},
{ "91", VARIABLE_LENGTH, 30},
{ "92", VARIABLE_LENGTH, 30},
{ "93", VARIABLE_LENGTH, 30},
{ "94", VARIABLE_LENGTH, 30},
{ "95", VARIABLE_LENGTH, 30},
{ "96", VARIABLE_LENGTH, 30},
{ "97", VARIABLE_LENGTH, 30},
{ "98", VARIABLE_LENGTH, 30},
{ "99", VARIABLE_LENGTH, 30},
};
static const DigitData THREE_DIGIT_DATA_LENGTH[] {
// Same format as above
{ "240", VARIABLE_LENGTH, 30},
{ "241", VARIABLE_LENGTH, 30},
{ "242", VARIABLE_LENGTH, 6},
{ "250", VARIABLE_LENGTH, 30},
{ "251", VARIABLE_LENGTH, 30},
{ "253", VARIABLE_LENGTH, 17},
{ "254", VARIABLE_LENGTH, 20},
{ "400", VARIABLE_LENGTH, 30},
{ "401", VARIABLE_LENGTH, 30},
{ "402", 17, 0},
{ "403", VARIABLE_LENGTH, 30},
{ "410", 13, 0},
{ "411", 13, 0},
{ "412", 13, 0},
{ "413", 13, 0},
{ "414", 13, 0},
{ "420", VARIABLE_LENGTH, 20},
{ "421", VARIABLE_LENGTH, 15},
{ "422", 3, 0},
{ "423", VARIABLE_LENGTH, 15},
{ "424", 3, 0},
{ "425", 3, 0},
{ "426", 3, 0},
};
static const DigitData THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH[] {
// Same format as above
{ "310", 6, 0},
{ "311", 6, 0},
{ "312", 6, 0},
{ "313", 6, 0},
{ "314", 6, 0},
{ "315", 6, 0},
{ "316", 6, 0},
{ "320", 6, 0},
{ "321", 6, 0},
{ "322", 6, 0},
{ "323", 6, 0},
{ "324", 6, 0},
{ "325", 6, 0},
{ "326", 6, 0},
{ "327", 6, 0},
{ "328", 6, 0},
{ "329", 6, 0},
{ "330", 6, 0},
{ "331", 6, 0},
{ "332", 6, 0},
{ "333", 6, 0},
{ "334", 6, 0},
{ "335", 6, 0},
{ "336", 6, 0},
{ "340", 6, 0},
{ "341", 6, 0},
{ "342", 6, 0},
{ "343", 6, 0},
{ "344", 6, 0},
{ "345", 6, 0},
{ "346", 6, 0},
{ "347", 6, 0},
{ "348", 6, 0},
{ "349", 6, 0},
{ "350", 6, 0},
{ "351", 6, 0},
{ "352", 6, 0},
{ "353", 6, 0},
{ "354", 6, 0},
{ "355", 6, 0},
{ "356", 6, 0},
{ "357", 6, 0},
{ "360", 6, 0},
{ "361", 6, 0},
{ "362", 6, 0},
{ "363", 6, 0},
{ "364", 6, 0},
{ "365", 6, 0},
{ "366", 6, 0},
{ "367", 6, 0},
{ "368", 6, 0},
{ "369", 6, 0},
{ "390", VARIABLE_LENGTH, 15},
{ "391", VARIABLE_LENGTH, 18},
{ "392", VARIABLE_LENGTH, 15},
{ "393", VARIABLE_LENGTH, 18},
{ "703", VARIABLE_LENGTH, 30},
};
static const DigitData FOUR_DIGIT_DATA_LENGTH[] {
// Same format as above
{ "7001", 13, 0},
{ "7002", VARIABLE_LENGTH, 30},
{ "7003", 10, 0},
{ "8001", 14, 0},
{ "8002", VARIABLE_LENGTH, 20},
{ "8003", VARIABLE_LENGTH, 30},
{ "8004", VARIABLE_LENGTH, 30},
{ "8005", 6, 0},
{ "8006", 18, 0},
{ "8007", VARIABLE_LENGTH, 30},
{ "8008", VARIABLE_LENGTH, 12},
{ "8018", 18, 0},
{ "8020", VARIABLE_LENGTH, 25},
{ "8100", 6, 0},
{ "8101", 10, 0},
{ "8102", 2, 0},
{ "8110", VARIABLE_LENGTH, 70},
{ "8200", VARIABLE_LENGTH, 70},
};
String FieldParser::parseFieldsInGeneralPurpose(String rawInformation)
{
if (rawInformation.getText().empty()) {
return String("");
}
// Processing 2-digit AIs
if (rawInformation.length() < 2) {
throw NotFoundException();
}
String firstTwoDigits(rawInformation.substring(0, 2)->getText());
for (DigitData dataLength : TWO_DIGIT_DATA_LENGTH) {
if (dataLength.digit == firstTwoDigits.getText()) {
if (dataLength.variableLength == VARIABLE_LENGTH) {
return processVariableAI(2, dataLength.length, rawInformation);
}
return processFixedAI(2, dataLength.variableLength, rawInformation);
}
}
if (rawInformation.length() < 3) {
throw NotFoundException();
}
String firstThreeDigits(rawInformation.substring(0, 3)->getText());
for (DigitData dataLength : THREE_DIGIT_DATA_LENGTH) {
if (dataLength.digit == firstThreeDigits.getText()) {
if (dataLength.variableLength == VARIABLE_LENGTH) {
return processVariableAI(3, dataLength.length, rawInformation);
}
return processFixedAI(3, dataLength.variableLength, rawInformation);
}
}
for (DigitData dataLength : THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH) {
if (dataLength.digit == firstThreeDigits.getText()) {
if (dataLength.variableLength == VARIABLE_LENGTH) {
return processVariableAI(4, dataLength.length, rawInformation);
}
return processFixedAI(4, dataLength.variableLength, rawInformation);
}
}
if (rawInformation.length() < 4) {
throw NotFoundException();
}
String firstFourDigits(rawInformation.substring(0, 4)->getText());
for (DigitData dataLength : FOUR_DIGIT_DATA_LENGTH) {
if (dataLength.digit == firstFourDigits.getText()) {
if (dataLength.variableLength == VARIABLE_LENGTH) {
return processVariableAI(4, dataLength.length, rawInformation);
}
return processFixedAI(4, dataLength.variableLength, rawInformation);
}
}
throw NotFoundException();
}
String FieldParser::processFixedAI(int aiSize, int fieldSize, String rawInformation)
{
if (rawInformation.length() < aiSize) {
throw NotFoundException();
}
String ai(rawInformation.substring(0, aiSize)->getText());
if (rawInformation.length() < aiSize + fieldSize) {
throw NotFoundException();
}
String field(rawInformation.substring(aiSize, /*aiSize +*/ fieldSize)->getText());
String remaining(rawInformation.substring(aiSize + fieldSize)->getText());
String result('(' + ai.getText() + ')' + field.getText());
String parsedAI = parseFieldsInGeneralPurpose(remaining);
if (parsedAI.getText() == "") {
return result;
} else {
result.append(parsedAI.getText());
return result;
}
}
String FieldParser::processVariableAI(int aiSize, int variableFieldSize, String rawInformation)
{
String ai(rawInformation.substring(0, aiSize)->getText());
int maxSize = std::min(rawInformation.length(), aiSize + variableFieldSize);
String field(rawInformation.substring(aiSize, maxSize - aiSize)->getText());
String remaining(rawInformation.substring(maxSize)->getText());
String result('(' + ai.getText() + ')' + field.getText());
String parsedAI = parseFieldsInGeneralPurpose(remaining);
if (parsedAI.getText() == "") {
return result;
} else {
result.append(parsedAI.getText());
return result;
}
}
}
}
}

View File

@ -0,0 +1,60 @@
#ifndef FIELD_PARSER_H
#define FIELD_PARSER_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include <zxing/common/Str.h>
#include <zxing/NotFoundException.h>
#include <string>
namespace zxing {
namespace oned {
namespace rss {
class FieldParser
{
public:
static String parseFieldsInGeneralPurpose(String rawInformation);
static String processFixedAI(int aiSize, int fieldSize, String rawInformation);
static String processVariableAI(int aiSize, int variableFieldSize, String rawInformation);
};
}
}
}
#endif

View File

@ -0,0 +1,453 @@
#include "GeneralAppIdDecoder.h"
namespace zxing {
namespace oned {
namespace rss {
GeneralAppIdDecoder::GeneralAppIdDecoder(Ref<BitArray> information)
: m_information(information),
m_buffer("")
{
}
String GeneralAppIdDecoder::decodeAllCodes(String &buff, int initialPosition)
{
int currentPosition = initialPosition;
String remaining("");
do {
DecodedInformation info(decodeGeneralPurposeField(currentPosition, remaining));
String parsedFields = FieldParser::parseFieldsInGeneralPurpose(info.getNewString());
if (parsedFields.length() > 0) {
buff.append(parsedFields.getText());
}
if (info.isRemaining()) {
remaining = String(info.getRemainingValue());
} else {
remaining = String("");
}
if (currentPosition == info.getNewPosition()) { // No step forward!
break;
}
currentPosition = info.getNewPosition();
} while (true);
return buff;
}
bool GeneralAppIdDecoder::isStillNumeric(int pos) const
{
// It's numeric if it still has 7 positions
// and one of the first 4 bits is "1".
if (pos + 7 > m_information->getSize()) {
return pos + 4 <= m_information->getSize();
}
for (int i = pos; i < pos + 3; ++i) {
if (m_information->get(i)) {
return true;
}
}
return m_information->get(pos + 3);
}
DecodedNumeric* GeneralAppIdDecoder::decodeNumeric(int pos)
{
if (pos + 7 > m_information->getSize()) {
int numeric = extractNumericValueFromBitArray(pos, 4);
if (numeric == 0) {
return new DecodedNumeric(m_information->getSize(), DecodedNumeric::FNC1, DecodedNumeric::FNC1);
}
return new DecodedNumeric(m_information->getSize(), numeric - 1, DecodedNumeric::FNC1);
}
int numeric = extractNumericValueFromBitArray(pos, 7);
int digit1 = (numeric - 8) / 11;
int digit2 = (numeric - 8) % 11;
return new DecodedNumeric(pos + 7, digit1, digit2);
}
int GeneralAppIdDecoder::extractNumericValueFromBitArray(int pos, int bits)
{
return extractNumericValueFromBitArray(m_information, pos, bits);
}
int GeneralAppIdDecoder::extractNumericValueFromBitArray(Ref<BitArray> information, int pos, int bits)
{
int value = 0;
for (int i = 0; i < bits; ++i) {
if (information->get(pos + i)) {
value |= 1 << (bits - i - 1);
}
}
return value;
}
DecodedInformation GeneralAppIdDecoder::decodeGeneralPurposeField(int pos, String &remaining)
{
m_buffer = String("");
if (remaining.length() > 0) {
m_buffer.append(remaining.getText());
}
m_current.setPosition(pos);
DecodedInformation lastDecoded(parseBlocks());
if (lastDecoded.getNewString().length() > 0 && lastDecoded.isRemaining()) {
return DecodedInformation(m_current.getPosition(), m_buffer, lastDecoded.getRemainingValue());
}
return DecodedInformation(m_current.getPosition(), m_buffer);
}
DecodedInformation GeneralAppIdDecoder::parseBlocks()
{
bool isFinished;
BlockParsedResult* result;
do {
int initialPosition = m_current.getPosition();
if (m_current.isAlpha()) {
result = parseAlphaBlock();
isFinished = result->isFinished();
} else if (m_current.isIsoIec646()) {
result = parseIsoIec646Block();
isFinished = result->isFinished();
} else { // it must be numeric
result = parseNumericBlock();
isFinished = result->isFinished();
}
bool positionChanged = initialPosition != m_current.getPosition();
if (!positionChanged && !isFinished) {
break;
}
} while (!isFinished);
return result->getDecodedInformation();
}
BlockParsedResult* GeneralAppIdDecoder::parseNumericBlock()
{
while (isStillNumeric(m_current.getPosition())) {
DecodedNumeric numeric(decodeNumeric(m_current.getPosition()));
m_current.setPosition(numeric.getNewPosition());
if (numeric.isFirstDigitFNC1()) {
DecodedInformation information(0, String(""));
if (numeric.isSecondDigitFNC1()) {
return new BlockParsedResult(DecodedInformation(m_current.getPosition(), m_buffer), true);
} else {
return new BlockParsedResult(DecodedInformation(m_current.getPosition(), m_buffer, numeric.getSecondDigit()), true);
}
}
m_buffer.append(std::to_string(numeric.getFirstDigit()));
if (numeric.isSecondDigitFNC1()) {
DecodedInformation information(m_current.getPosition(), m_buffer);
return new BlockParsedResult(information, true);
}
m_buffer.append(std::to_string(numeric.getSecondDigit()));
}
if (isNumericToAlphaNumericLatch(m_current.getPosition())) {
m_current.setAlpha();
m_current.incrementPosition(4);
}
return new BlockParsedResult(false);
}
BlockParsedResult* GeneralAppIdDecoder::parseIsoIec646Block()
{
while (isStillIsoIec646(m_current.getPosition())) {
DecodedChar iso = decodeIsoIec646(m_current.getPosition());
m_current.setPosition(iso.getNewPosition());
if (iso.isFNC1()) {
DecodedInformation information(m_current.getPosition(), m_buffer);
return new BlockParsedResult(information, true);
}
m_buffer.append(iso.getValue());
}
if (isAlphaOr646ToNumericLatch(m_current.getPosition())) {
m_current.incrementPosition(3);
m_current.setNumeric();
} else if (isAlphaTo646ToAlphaLatch(m_current.getPosition())) {
if (m_current.getPosition() + 5 < m_information->getSize()) {
m_current.incrementPosition(5);
} else {
m_current.setPosition(m_information->getSize());
}
m_current.setAlpha();
}
return new BlockParsedResult(false);
}
BlockParsedResult* GeneralAppIdDecoder::parseAlphaBlock()
{
while (isStillAlpha(m_current.getPosition())) {
DecodedChar alpha(decodeAlphanumeric(m_current.getPosition()));
m_current.setPosition(alpha.getNewPosition());
if (alpha.isFNC1()) {
DecodedInformation information(m_current.getPosition(), m_buffer);
return new BlockParsedResult(information, true); //end of the char block
}
m_buffer.append(alpha.getValue());
}
if (isAlphaOr646ToNumericLatch(m_current.getPosition())) {
m_current.incrementPosition(3);
m_current.setNumeric();
} else if (isAlphaTo646ToAlphaLatch(m_current.getPosition())) {
if (m_current.getPosition() + 5 < m_information->getSize()) {
m_current.incrementPosition(5);
} else {
m_current.setPosition(m_information->getSize());
}
m_current.setIsoIec646();
}
return new BlockParsedResult(false);
}
bool GeneralAppIdDecoder::isStillIsoIec646(int pos)
{
if (pos + 5 > m_information->getSize()) {
return false;
}
int fiveBitValue = extractNumericValueFromBitArray(pos, 5);
if (fiveBitValue >= 5 && fiveBitValue < 16) {
return true;
}
if (pos + 7 > m_information->getSize()) {
return false;
}
int sevenBitValue = extractNumericValueFromBitArray(pos, 7);
if (sevenBitValue >= 64 && sevenBitValue < 116) {
return true;
}
if (pos + 8 > m_information->getSize()) {
return false;
}
int eightBitValue = extractNumericValueFromBitArray(pos, 8);
return eightBitValue >= 232 && eightBitValue < 253;
}
DecodedChar GeneralAppIdDecoder::decodeIsoIec646(int pos)
{
int fiveBitValue = extractNumericValueFromBitArray(pos, 5);
if (fiveBitValue == 15) {
return DecodedChar(pos + 5, DecodedChar::FNC1);
}
if (fiveBitValue >= 5 && fiveBitValue < 15) {
return DecodedChar(pos + 5, static_cast<char>('0' + fiveBitValue - 5));
}
int sevenBitValue = extractNumericValueFromBitArray(pos, 7);
if (sevenBitValue >= 64 && sevenBitValue < 90) {
return DecodedChar(pos + 7, static_cast<char>(sevenBitValue + 1));
}
if (sevenBitValue >= 90 && sevenBitValue < 116) {
return DecodedChar(pos + 7, static_cast<char>(sevenBitValue + 7));
}
int eightBitValue = extractNumericValueFromBitArray(pos, 8);
char c;
switch (eightBitValue) {
case 232:
c = '!';
break;
case 233:
c = '"';
break;
case 234:
c = '%';
break;
case 235:
c = '&';
break;
case 236:
c = '\'';
break;
case 237:
c = '(';
break;
case 238:
c = ')';
break;
case 239:
c = '*';
break;
case 240:
c = '+';
break;
case 241:
c = ',';
break;
case 242:
c = '-';
break;
case 243:
c = '.';
break;
case 244:
c = '/';
break;
case 245:
c = ':';
break;
case 246:
c = ';';
break;
case 247:
c = '<';
break;
case 248:
c = '=';
break;
case 249:
c = '>';
break;
case 250:
c = '?';
break;
case 251:
c = '_';
break;
case 252:
c = ' ';
break;
default:
throw FormatException::getFormatInstance();
}
return DecodedChar(pos + 8, c);
}
bool GeneralAppIdDecoder::isStillAlpha(int pos)
{
if (pos + 5 > m_information->getSize()) {
return false;
}
// We now check if it's a valid 5-bit value (0..9 and FNC1)
int fiveBitValue = extractNumericValueFromBitArray(pos, 5);
if (fiveBitValue >= 5 && fiveBitValue < 16) {
return true;
}
if (pos + 6 > m_information->getSize()) {
return false;
}
int sixBitValue = extractNumericValueFromBitArray(pos, 6);
return sixBitValue >= 16 && sixBitValue < 63; // 63 not included
}
DecodedChar GeneralAppIdDecoder::decodeAlphanumeric(int pos)
{
int fiveBitValue = extractNumericValueFromBitArray(pos, 5);
if (fiveBitValue == 15) {
return DecodedChar(pos + 5, DecodedChar::FNC1);
}
if (fiveBitValue >= 5 && fiveBitValue < 15) {
return DecodedChar(pos + 5, static_cast<char>('0' + fiveBitValue - 5));
}
int sixBitValue = extractNumericValueFromBitArray(pos, 6);
if (sixBitValue >= 32 && sixBitValue < 58) {
return DecodedChar(pos + 6, static_cast<char>(sixBitValue + 33));
}
char c;
switch (sixBitValue) {
case 58:
c = '*';
break;
case 59:
c = ',';
break;
case 60:
c = '-';
break;
case 61:
c = '.';
break;
case 62:
c = '/';
break;
default:
throw new IllegalStateException("Decoding invalid alphanumeric value: " + sixBitValue);
}
return DecodedChar(pos + 6, c);
}
bool GeneralAppIdDecoder::isAlphaTo646ToAlphaLatch(int pos)
{
if (pos + 1 > m_information->getSize()) {
return false;
}
for (int i = 0; i < 5 && i + pos < m_information->getSize(); ++i) {
if (i == 2) {
if (!m_information->get(pos + 2)) {
return false;
}
} else if (m_information->get(pos + i)) {
return false;
}
}
return true;
}
bool GeneralAppIdDecoder::isAlphaOr646ToNumericLatch(int pos)
{
// Next is alphanumeric if there are 3 positions and they are all zeros
if (pos + 3 > m_information->getSize()) {
return false;
}
for (int i = pos; i < pos + 3; ++i) {
if (m_information->get(i)) {
return false;
}
}
return true;
}
bool GeneralAppIdDecoder::isNumericToAlphaNumericLatch(int pos) {
// Next is alphanumeric if there are 4 positions and they are all zeros, or
// if there is a subset of this just before the end of the symbol
if (pos + 1 > m_information->getSize()) {
return false;
}
for (int i = 0; i < 4 && i + pos < m_information->getSize(); ++i) {
if (m_information->get(pos + i)) {
return false;
}
}
return true;
}
}
}
}

View File

@ -0,0 +1,99 @@
#ifndef GENERAL_APP_ID_DECODER_H
#define GENERAL_APP_ID_DECODER_H
/*
* Copyright (C) 2010 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.
*
* 2019-07-17 translation from Java into C++
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
#include <zxing/common/Counted.h>
#include <zxing/common/BitArray.h>
#include <zxing/common/Str.h>
#include <zxing/IllegalStateException.h>
#include "CurrentParsingState.h"
#include "DecodedInformation.h"
#include "DecodedNumeric.h"
#include "FieldParser.h"
#include "BlockParsedResult.h"
#include "DecodedChar.h"
// VC++
namespace zxing {
namespace oned {
namespace rss {
class GeneralAppIdDecoder
{
public:
GeneralAppIdDecoder(Ref<BitArray> information);
String decodeAllCodes(String &buff, int initialPosition);
bool isStillNumeric(int pos) const;
DecodedNumeric *decodeNumeric(int pos);
int extractNumericValueFromBitArray(int pos, int bits);
static int extractNumericValueFromBitArray(Ref<BitArray> information, int pos, int bits);
DecodedInformation decodeGeneralPurposeField(int pos, String &remaining);
DecodedInformation parseBlocks();
BlockParsedResult *parseNumericBlock();
BlockParsedResult *parseIsoIec646Block();
BlockParsedResult *parseAlphaBlock();
bool isStillIsoIec646(int pos);
DecodedChar decodeIsoIec646(int pos);
bool isStillAlpha(int pos);
DecodedChar decodeAlphanumeric(int pos);
bool isAlphaTo646ToAlphaLatch(int pos);
bool isAlphaOr646ToNumericLatch(int pos);
bool isNumericToAlphaNumericLatch(int pos);
private:
Ref<BitArray> m_information;
CurrentParsingState m_current;
String m_buffer;
};
}
}
}
#endif