QR decoding with zbar

675 views Asked by At

I'm encoding a byte array into qr code using libqrencode and than try to decode it using zbar library. the programming language is c++.

The problem occurs when the values are >=128. for example when I decode the qr code which contains the following values:

unsigned char  data[17]={111, 127, 128, 224, 255, 178, 201,200, 192, 191,22, 17,20, 34, 65 ,23, 76};   

symbol->get_data_length() return 25 instead of 17 and when I tried to print the values using this small piece of code:

string input_data = symbol->get_data();
for(int k=0; k< 25; k++)
   cout<< (int)((unsigned char)input_data[k])<<", "; 

I got the following result:

111, 127, 194, 128, 195, 160, 195, 191, 194, 178, 195, 137, 195, 136, 195, 128, 194, 191, 22, 17, 20, 34, 65, 23, 76,

So as we can notice the values < 128 didn't effected but I got two bytes for every value >=128. Also I printed the values without casting to unsigned char:

for(int k=0; k< 25; k++)
   cout<< (int)input_data[k]<<", ";

and the result is:

111, 127, -62, -128, -61, -96, -61, -65, -62, -78, -61, -119, -61, -120, -61, -128, -62, -65, 22, 17, 20, 34, 65, 23, 76

I solve this problem by the following code:

void process_zbar_output(const string & input_data, vector<unsigned char> & output_data)
    {
        for (int i = 0; i < input_data.length(); i++)
        {
            int temp = (int) input_data[i];
 // if the original value is >=128 we need to process it to get the original value
            if (temp < 0)
            {
                // if the number is 62 than the original is between 128 and 191
                // if the number is 61 than the original is between 192 and 255
                if (temp == -62)
                    output_data.push_back(256 + ((int) input_data[i + 1]));
                else
                    output_data.push_back(256 + ((int) input_data[i + 1] + 64));
                i++;

            }
            else
            {
                output_data.push_back( input_data[i]);
            }
        }
    }

Can anybody help me with this problem and explain why I got these extra bytes?

0

There are 0 answers