mirror of
https://github.com/status-im/qzxing.git
synced 2025-02-16 12:58:31 +00:00
Progressing fixes for Qr Encoder. #10
This commit is contained in:
parent
e9b7f399a0
commit
295a28ee47
@ -33,7 +33,7 @@ Ref<GenericGFPoly> ReedSolomonEncoder::buildGenerator(int degree)
|
||||
return cachedGenerators_.at(degree); // ??? wont this through exception?
|
||||
}
|
||||
|
||||
void ReedSolomonEncoder::encode(std::vector<int> &toEncode, int ecBytes)
|
||||
void ReedSolomonEncoder::encode(std::vector<byte> &toEncode, int ecBytes)
|
||||
{
|
||||
if (ecBytes == 0) {
|
||||
throw Exception("No error correction bytes");
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <zxing/common/reedsolomon/GenericGFPoly.h>
|
||||
#include <zxing/common/reedsolomon/GenericGF.h>
|
||||
#include <zxing/common/Array.h>
|
||||
#include <zxing/common/Types.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
@ -18,7 +19,7 @@ private:
|
||||
public:
|
||||
ReedSolomonEncoder(Ref<GenericGF> field);
|
||||
|
||||
void encode(std::vector<int>& toEncode, int ecBytes);
|
||||
void encode(std::vector<byte> &toEncode, int ecBytes);
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ protected:
|
||||
int numDataBytes,
|
||||
int numRSBlocks);
|
||||
|
||||
static ArrayRef<byte> generateECBytes(const std::vector<byte> &dataBytes, int numEcBytesInBlock);
|
||||
static ArrayRef<byte> generateECBytes(std::vector<byte> &dataBytes, int numEcBytesInBlock);
|
||||
|
||||
static void appendNumericBytes(const QString& content, BitArray& bits);
|
||||
|
||||
|
@ -423,20 +423,20 @@ BitArray* Encoder::interleaveWithECBytes(const BitArray& bits,
|
||||
return result;
|
||||
}
|
||||
|
||||
ArrayRef<byte> Encoder::generateECBytes(const std::vector<byte>& dataBytes, int numEcBytesInBlock)
|
||||
ArrayRef<byte> Encoder::generateECBytes(std::vector<byte>& dataBytes, int numEcBytesInBlock)
|
||||
{
|
||||
int numDataBytes = dataBytes.size();
|
||||
std::vector<int> toEncode;
|
||||
toEncode.resize(numDataBytes + numEcBytesInBlock);
|
||||
for (int i = 0; i < numDataBytes; i++)
|
||||
toEncode[i] = dataBytes[i];
|
||||
// std::vector<int> toEncode(numDataBytes);
|
||||
//toEncode.resize(numDataBytes + numEcBytesInBlock);
|
||||
// for (int i = 0; i < numDataBytes; i++)
|
||||
// toEncode[i] = dataBytes[i];
|
||||
|
||||
zxing::ReedSolomonEncoder encoder(GenericGF::QR_CODE_FIELD_256);
|
||||
encoder.encode(toEncode, numEcBytesInBlock);
|
||||
encoder.encode(dataBytes, numEcBytesInBlock);
|
||||
|
||||
ArrayRef<byte> ecBytes(numEcBytesInBlock);
|
||||
for (int i = 0; i < numEcBytesInBlock; i++) {
|
||||
ecBytes[i] = (byte) toEncode[numDataBytes + i];
|
||||
ecBytes[i] = dataBytes[numDataBytes + i];
|
||||
}
|
||||
return ecBytes;
|
||||
}
|
||||
|
@ -84,6 +84,10 @@ protected:
|
||||
assertEquals(expected_str, actual);
|
||||
}
|
||||
|
||||
// void assertEquals(int expected, int actual) {
|
||||
// assertEquals(expected, actual);
|
||||
// }
|
||||
|
||||
void assertSame(qrcode::Mode &expected, qrcode::Mode &actual){
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
@ -101,8 +105,8 @@ protected:
|
||||
}
|
||||
|
||||
void assertDataEquals(const std::string &message,
|
||||
const std::vector<int> &expected,
|
||||
const std::vector<int> & received)
|
||||
const std::vector<byte> &expected,
|
||||
const std::vector<byte> & received)
|
||||
{
|
||||
if(expected.size() != received.size())
|
||||
assertTrue(false);
|
||||
@ -117,8 +121,8 @@ protected:
|
||||
}
|
||||
|
||||
void assertDataEquals(const std::string &message,
|
||||
const std::vector<int> &expected,
|
||||
const ArrayRef<int> &received)
|
||||
const std::vector<byte> &expected,
|
||||
const ArrayRef<byte> &received)
|
||||
{
|
||||
if(expected.size() != received->size())
|
||||
assertTrue(false);
|
||||
@ -132,6 +136,17 @@ protected:
|
||||
}
|
||||
}
|
||||
|
||||
void assertDataEquals(const std::string &message,
|
||||
const std::vector<byte> &expected,
|
||||
const ArrayRef<int> &received)
|
||||
{
|
||||
ArrayRef<byte> received_copy(received->size());
|
||||
for(int i=0; i<received_copy->size(); i++)
|
||||
received_copy[i] = received[i];
|
||||
|
||||
assertDataEquals(message, expected, received_copy);
|
||||
}
|
||||
|
||||
static void initializeRandom();
|
||||
static int generateRandomNumber(int range);
|
||||
|
||||
|
@ -43,7 +43,7 @@ void ReedSolomonTests::testQRCode()
|
||||
// testEncodeDecodeRandom(GenericGF::QR_CODE_FIELD_256, 220, 35);
|
||||
}
|
||||
|
||||
void ReedSolomonTests::corrupt(std::vector<int> &received, int howMany, int max)
|
||||
void ReedSolomonTests::corrupt(std::vector<byte> &received, int howMany, int max)
|
||||
{
|
||||
std::vector<bool> corrupted(received.size(), false);
|
||||
for (int j = 0; j < howMany; j++) {
|
||||
@ -63,9 +63,9 @@ void ReedSolomonTests::testEncodeDecodeRandom(Ref<GenericGF> field, int dataSize
|
||||
assertTrue(dataSize > 0 && dataSize <= field->getSize() - 3); /*"Invalid data size for " + field, */
|
||||
assertTrue(ecSize > 0 && ecSize + dataSize <= field->getSize()); /*"Invalid ECC size for " + field, */
|
||||
ReedSolomonEncoder encoder(field);
|
||||
std::vector<int> message;//(dataSize + ecSize);
|
||||
std::vector<int> dataWords(dataSize);
|
||||
std::vector<int> ecWords(ecSize);
|
||||
std::vector<byte> message;//(dataSize + ecSize);
|
||||
std::vector<byte> dataWords(dataSize);
|
||||
std::vector<byte> ecWords(ecSize);
|
||||
initializeRandom();
|
||||
int iterations = field->getSize() > 256 ? 1 : DECODER_RANDOM_TEST_ITERATIONS;
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
@ -83,20 +83,20 @@ void ReedSolomonTests::testEncodeDecodeRandom(Ref<GenericGF> field, int dataSize
|
||||
}
|
||||
|
||||
void ReedSolomonTests::testEncodeDecode(Ref<GenericGF> field,
|
||||
const std::vector<int> &dataWords,
|
||||
const std::vector<int> & ecWords)
|
||||
const std::vector<byte> &dataWords,
|
||||
const std::vector<byte> &ecWords)
|
||||
{
|
||||
testEncoder(field, dataWords, ecWords);
|
||||
testDecoder(field, dataWords, ecWords);
|
||||
}
|
||||
|
||||
void ReedSolomonTests::testEncoder(Ref<GenericGF> field,
|
||||
const std::vector<int> &dataWords,
|
||||
const std::vector<int> & ecWords)
|
||||
const std::vector<byte> &dataWords,
|
||||
const std::vector<byte> &ecWords)
|
||||
{
|
||||
ReedSolomonEncoder encoder(field);
|
||||
std::vector<int> messageExpected;
|
||||
std::vector<int> message(dataWords);
|
||||
std::vector<byte> messageExpected;
|
||||
std::vector<byte> message(dataWords);
|
||||
|
||||
messageExpected = dataWords;
|
||||
messageExpected.insert(std::end(messageExpected), std::begin(ecWords), std::end(ecWords));
|
||||
@ -107,11 +107,11 @@ void ReedSolomonTests::testEncoder(Ref<GenericGF> field,
|
||||
}
|
||||
|
||||
void ReedSolomonTests::testDecoder(Ref<GenericGF> field,
|
||||
const std::vector<int> &dataWords,
|
||||
const std::vector<int> & ecWords) {
|
||||
const std::vector<byte> &dataWords,
|
||||
const std::vector<byte> &ecWords) {
|
||||
ReedSolomonDecoder decoder(field);
|
||||
std::vector<int> message;
|
||||
std::vector<int> referenceMessage;
|
||||
std::vector<byte> message;
|
||||
std::vector<byte> referenceMessage;
|
||||
|
||||
int maxErrors = ecWords.size() / 2;
|
||||
initializeRandom();
|
||||
@ -133,6 +133,7 @@ void ReedSolomonTests::testDecoder(Ref<GenericGF> field,
|
||||
ArrayRef<int> messageArrayRef(message.size());
|
||||
for(int i=0; i<message.size(); i++)
|
||||
messageArrayRef[i] = message[i];
|
||||
|
||||
try {
|
||||
decoder.decode(messageArrayRef, ecWords.size());
|
||||
} catch(zxing::Exception &e) {
|
||||
|
@ -22,17 +22,17 @@ protected:
|
||||
void testQRCode();
|
||||
|
||||
private:
|
||||
void corrupt(std::vector<int> &received, int howMany, int max);
|
||||
void corrupt(std::vector<byte> &received, int howMany, int max);
|
||||
void testEncodeDecodeRandom(Ref<GenericGF> field, int dataSize, int ecSize);
|
||||
void testEncodeDecode(Ref<GenericGF> field,
|
||||
const std::vector<int> &dataWords,
|
||||
const std::vector<int> & ecWords);
|
||||
const std::vector<byte> &dataWords,
|
||||
const std::vector<byte> & ecWords);
|
||||
void testEncoder(Ref<GenericGF> field,
|
||||
const std::vector<int> &dataWords,
|
||||
const std::vector<int> & ecWords);
|
||||
const std::vector<byte> &dataWords,
|
||||
const std::vector<byte> & ecWords);
|
||||
void testDecoder(Ref<GenericGF> field,
|
||||
const std::vector<int> &dataWords,
|
||||
const std::vector<int> & ecWords);
|
||||
const std::vector<byte> &dataWords,
|
||||
const std::vector<byte> & ecWords);
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -354,15 +354,19 @@ void EncoderTests::testAppend8BitBytes()
|
||||
|
||||
void EncoderTests::testGenerateECBytes()
|
||||
{
|
||||
const byte dataBytes_arr[] = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
|
||||
const byte dataBytes_arr[] = {32, 65, 205, 69, 41, 220, 46, 128, 236};
|
||||
std::vector<byte> dataBytes (dataBytes_arr, dataBytes_arr + getArrayLength(dataBytes_arr));
|
||||
// std::vector<byte> dataBytes(getArrayLength(dataBytes_arr));
|
||||
// for(int i=0; i<dataBytes.size(); i++)
|
||||
// dataBytes[i] = dataBytes_arr[i];
|
||||
|
||||
ArrayRef<byte> ecBytes = Encoder::generateECBytes(dataBytes, 17);
|
||||
const int expected[] = {
|
||||
const byte expected[] = {
|
||||
42, 159, 74, 221, 244, 169, 239, 150, 138, 70, 237, 85, 224, 96, 74, 219, 61
|
||||
};
|
||||
assertEquals( getArrayLength(expected), ecBytes.count());
|
||||
for (int x = 0; x < getArrayLength(expected); x++) {
|
||||
assertEquals(expected[x], ecBytes[x] & 0xFF);
|
||||
assertEquals(expected[x], ecBytes[x]);
|
||||
}
|
||||
// dataBytes = new byte[] {67, 70, 22, 38, 54, 70, 86, 102, 118,
|
||||
// (byte)134, (byte)150, (byte)166, (byte)182, (byte)198, (byte)214};
|
||||
|
Loading…
x
Reference in New Issue
Block a user