Search code examples
androidzbar

Get String from QR Symbol


I am using ZBAR in a project to read barcodes, and also QR. After reading a QR, I receive a String like this:

eyJ1cmxTZXJ2bGV0IjoiaHR0cHM6Ly9kZW1vcy5pbmZhcGxpYy5lcy9pbnZlbnRhcmlvd2ViL0ludmVudGFyaW9KU09OIiwidG9rZW4iOiI1MjFjY2I5Ny0wNWZkLTRjZGEtYjYxMi00ZTM4YTcwM2MwODkiLCJpZENsaWVudGUiOiJkZW1vcyJ3

The thing is, I KNOW that is a JSON, and I need one of the values inside that JSON. But the same barcode scanner must be used to read regular barcodes, too.

How could I know if its a QR, or a regular barcode what was read? And, once I know, how can I extract the JSON from that weird String?

My code is as follows:

PreviewCallback CallbackCamara = new PreviewCallback()
{
    public void onPreviewFrame(byte[] data, Camera camera) {
        Camera.Parameters parameters = camera.getParameters();
        Size dimensiones = parameters.getPreviewSize();

        Image barcode = new Image(dimensiones.width, dimensiones.height,"Y800");
        barcode.setData(data);

        int result = escaner.scanImage(barcode);

        if (result != 0) {
            previsualizandoCamara = false;
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            SymbolSet syms = escaner.getResults();
            for (Symbol sym : syms) {

                valor = sym;

                barcodeScanned = true;
                DevolverCodigo(sym.getData());
            }
        }
    }
};

Solution

  • I figured it out, its a BASE64 String, to decode it:

    String stringFromBase = new String(Base64.decode(sym.getData(), Base64.DEFAULT));
    

    And in that String I have my JSON object.