How to get original bytes from the Qr code generated via a LVGL

Viewed 56

I'm using LVGL library to generate the Qr code.

uint8_t test[150];
uint8_t testSize = 140;

for (unint8_t i = 0; i< testSize; i++)
{
   test[u] = i;
}

lv_qrcode_update(qr,test,testSize); //for generating qr code

Note: Qrcode holds the value from 0 to 140.

Generated qr code

I'm using zxing android library to scan the QR code and it returns the result i.e

!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~�

Secondly, if I query for raw bytes then it gives

[64, 11, 0, 0, 16, 32, 48, 64, 80, 96, 112, -128, -112, -96, -80, -64, -48, -32, -15, 1, 17, 33, 49, 65, 81, 97, 113, -127, -111, -95, -79, -63, -47, -31, -14, 2, 18, 34, 50, 66, 82, 98, 114, -126, -110, -94, -78, -62, -46, -30, -13, 3, 19, 35, 51, 67, 83, 99, 115, -125, -109, -93, -77, -61, -45, -29, -12, 4, 20, 36, 52, 68, 84, 100, 116, -124, -108, -92, -76, -60, -44, -28, -11, 5, 21, 37, 53, 69, 85, 101, 117, -123, -107, -91, -75, -59, -43, -27, -10, 6, +116 more]

I tried to decode the rawBytes using DecodedBitStreamParser but it fails. Need your help in getting the original value that was passed while generating the QR code.

DecodedBitStreamParser.decode(result.rawBytes, Version.getVersionForNumber(40))

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.zxing.DecodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.common.BitSource;
import com.google.zxing.common.DecoderResult;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.qrcode.decoder.Mode;
import com.google.zxing.qrcode.decoder.Version;

public class DecodedBitStreamParser {

    private DecodedBitStreamParser() {
    }

    public static DecoderResult decode(byte[] bytes, Version version) throws FormatException {
        return decode(bytes, version, null, null);
    }

    public static DecoderResult decode(byte[] bytes, Version version, ErrorCorrectionLevel ecLevel, Map<DecodeHintType, ?> hints) throws FormatException {
        BitSource bits = new BitSource(bytes);

        List<byte[]> byteSegments = new ArrayList<byte[]>(1);
        Mode mode;
        do {
            // While still another segment to read...
            if (bits.available() < 4) {
                // OK, assume we're done. Really, a TERMINATOR mode should have been recorded here
                mode = Mode.TERMINATOR;
            } else {
                try {
                    mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits
                } catch (IllegalArgumentException iae) {
                    throw FormatException.getFormatInstance();
                }
            }
            if (mode != Mode.TERMINATOR) {
                // "Normal" QR code modes:
                // How many characters will follow, encoded in this mode?
                int count = bits.readBits(mode.getCharacterCountBits(version));
                if (mode == Mode.BYTE) {
                    byteSegments.add(decodeByteSegment(bits, count));
                } else {
                    throw FormatException.getFormatInstance();
                }
            }
        } while (mode != Mode.TERMINATOR);

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        for (byte[] bs : byteSegments) {
            try {
                bout.write(bs);
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }

        DecoderResult result = new DecoderResult(bout.toByteArray(), "", null, null);

        return result;
    }

    private static byte[] decodeByteSegment(BitSource bits, int count) throws FormatException {
        // Don't crash trying to read more bits than we have available.
        if (count << 3 > bits.available()) {
            throw FormatException.getFormatInstance();
        }

        byte[] readBytes = new byte[count];
        for (int i = 0; i < count; i++) {
            readBytes[i] = (byte) bits.readBits(8);
        }

        return readBytes;
    }

}
0 Answers
Related