Bring QR Code data to my fields

Asked

Viewed 1,576 times

2

I am reading a QR Code as 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 that when reading the QR Code, go back to min ha application already filled fields.

Campos:

- CNPJ
- Data
- COO
- Total
  • 1

    You can post the string or part of it in the question, to see if you have any patterns to treat and separate as you want.

  • 35151022986022000105590000400630036591770797|20151013131745|21.26| - There’s more, but I need that start

  • 1

    Add to the question by clicking [Edit] to make it more complete.

  • 1

    Apparently there’s a pattern in this string, they’re separated by |. But in this section you posted, there are only 3 separate sequences, which is the order of the information?

  • In these three sequences you have the four fields I need. the CNPJ starts in box 7 (22.986.022/0001-05), then the COO in box 31 (003659), then the date in the second block (13/10/2015), and finally the value in the last block (21.26)

2 answers

2

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

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

Ai just go through this array and collect the information you need.

It seems to me that the first course is a nfe key, correct!?

So the CNPJ is at position 7 with 14 digits, to pick up with Substring use:

String CNPJ = resp[0].subsring(7, 14);
  • thanks for the reply. No Resp[0] he’s telling me Array type expected; found: 'java.land.String. Could you help me with this, please?

  • 1

    String[] Resp = result.gettext(). toString(). split("|");

  • The program ran, but I have not yet brought the data to my field...I believe I have to point out which field to go to. For example, I use a lot of findViewById, has something similar?

  • What happens in debug mode?

  • Thanks @Hiagosouza for the editions!

  • Dispo @Celsomarigojr, can’t change a part, but you can where this "String CNPJ = Resp[0]. subsring(7, 14);" is actually "String CNPJ = Resp[0]. substring(7, 14);"... lacked a "T"

  • Thus, error it shows nothing. It collects the QR Code string, but when it closes the reader, my field is still empty. In this @Celsomarigojr solution I kind of do not inform which field should be filled, no?

  • What is the need to pass the string to an array and then use substring? It would no longer be practical to pick directly from the string, since the OP already knows where to pick up the substrings?

  • @Mariopueblajunior "txtView" this variable is referencing the textView that you want to put the correct answer? If you are trying to put txtView.setText( CNPJ );

  • @Hiagosouza actually these two lines of command I put in just to test and see if the app would bring the giant string she brought.

  • @Diegofelipe Have any suggestions then in the code?

  • @Mariopueblajunior just handle the string directly, using substring to get the data you told me in response to your question, as suggested in this code.

  • @Diegofelipe Put it like this, see if there’s something wrong: EditText cnpj = (EditText) findViewById(R.id.cnpj); cnpj.setText(result.getText().substring(7,14);. Nothing happened, something’s wrong?

  • @Hiagosouza looks at the answer above

  • I will post the reply @Mariopueblajunior

Show 10 more comments

1


Complementing the @Celsomarigojr response, you can use the method split to facilitate the processing of data, however, there is a correction to be made: the method said will not function properly without the character | is escaped, because the split method waits as one of its parameters a regular expression, and pipe is a special ER character, as per this answer in Soen.

Then change:

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

for:

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

Making a test with the string informed by you 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];

Behold here the working code.

On date you can use the class SimpleFormatDate to format the date and display it in a more user-friendly way.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.