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
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.
– user28595
35151022986022000105590000400630036591770797|20151013131745|21.26| - There’s more, but I need that start
– Mario Puebla Junior
Add to the question by clicking [Edit] to make it more complete.
– user28595
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?– user28595
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)
– Mario Puebla Junior