Bring QR Code data to my fields

2

I'm reading a QR Code as per the code below:

// Botão para abrir câmera e usar o QR Code (Irá abrir a Store para baixar um app nativo)
    btnQR = (Button) findViewById(R.id.btnQR);
    btnQR.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
            startActivityForResult(intent, 0);
        }

        public void onActivityResult(int requestCode, int resultCode, Intent intent) {
            if (requestCode == 0) {
                if (resultCode == RESULT_OK) {

                    String contents = intent.getStringExtra("SCAN_RESULT");
                    String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

                    // Handle successful scan

                } else if (resultCode == RESULT_CANCELED) {
                    // Handle cancel
                    Log.i("App","Scan unsuccessful");
                }
            }
        }

        private final String TAG = MainActivity.class.getSimpleName();
        private CompoundBarcodeView barcodeView;

        private BarcodeCallback callback = new BarcodeCallback() {
            @Override
            public void barcodeResult(BarcodeResult result) {
                if (result.getText() != null) {
                    barcodeView.setStatusText(result.getText());
                }

                String barcode = "35151022986022000105590000400630036591770797|20151013131745|21.26|";
                String[] resp = barcode.split("\|");

                String cnpj = resp[0].substring(6, 20); // são 14 dígitos, iniciado da posição 7
                String coo = resp[0].substring(30, 35); // supondo que tenham sempre 6 dígitos
                String data = resp[1].substring(0, 5); // 6 primeiros dígitos corresponde a data
                String total = resp[2];

                System.out.println(cnpj);
                System.out.println(coo);
                System.out.println(data);
                System.out.println(total);

                try {
                    Date date = new SimpleDateFormat("yyyyMMdd").parse(data);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String dataFormatada = new SimpleDateFormat("dd/MM/yyyy").format(data);

            }

            @Override
            public void possibleResultPoints(List<ResultPoint> resultPoints) {
            }
        };
    });

I need you to read the QR Code, go back to the application with the fields already filled in.

Fields:

- CNPJ
- Data
- COO
- Total
    
asked by anonymous 28.10.2015 / 12:07

2 answers

1

Complementing @CelsoMarigoJr's response, you can use the split " to make it easier to handle the data, however, there is a correction to be made: the said method will not work correctly without the | character being escaped, because the split method waits as one of its parameters a regular expression , and the pipe is a special character of ER, as this answer in SOEn .

Then change:

String[] resp = result.getText().split("|");

To:

String[] resp = result.getText().split("\|");

By doing a test with the string that you reported in the comments, see the result:

String barcode = "35151022986022000105590000400630036591770797|20151013131745|21.26|";        
String[] resp = barcode.split("\|");
String cnpj = resp[0].substring(6, 20); //são 14 digitos, iniciado da posicao 7
String coo = resp[0].substring(30, 35); // supondo que tenham sempre 6 digitos
String data = resp[1].substring(0, 5); // 6 primeiros digitos sao da data
String total = resp[2];

See here working code.

At date you can use the SimpleFormatDate class for format the date and display it more user-friendly.

Date date = new SimpleDateFormat("yyyyMMdd").parse(data);
String dataFormatada = new SimpleDateFormat("dd/MM/yyyy").format(date);
    
28.10.2015 / 18:06
2

You can use Split to separate the String into an array using a delimiter, in the example you posted, your "delimiter" is the "|", so do something like this:

String[] resp = result.getText().split("|");

Just go through this array and collect the information you need.

It seems to me that the first field is a NFe key , correct!?

Then the CNPJ is in position 7 with 14 digits, to pick up with Substring > use:

String CNPJ = resp[0].subsring(7, 14);
    
28.10.2015 / 12:29