How to retrieve variables from a code qr

1

Good, I have a QR code in my android application, until the moment it is working as it should, the question is that I require that once you read the code that is being passed to you can handle the variables, until now I have no idea of how to do it, because I only have one complete string, but I would like to know how I can retrieve variables passed through the string, I put the code of the scanner and the one that receives the data:

pre-scan activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pre_scan);

    scanbtn = (Button) findViewById(R.id.btnscan);
    result = (TextView) findViewById(R.id.result);
    if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, PERMISSION_REQUEST);
    }
    scanbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(PreScan.this, LeerQR.class);
            startActivityForResult(intent, REQUEST_CODE);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUEST_CODE && resultCode == RESULT_OK){
        if(data != null){
            final Barcode barcode = data.getParcelableExtra("barcode");
            result.post(new Runnable() {
                @Override
                public void run() {
                    result.setText(barcode.displayValue);
                    scanbtn.setVisibility(View.GONE);
                }
            });
        }
    }
}

Scan Activity:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leer_qr);
        cameraView = (SurfaceView) findViewById(R.id.cameraView);
        cameraView.setZOrderMediaOverlay(true);
        holder = cameraView.getHolder();
        barcode = new BarcodeDetector.Builder(this)
                .setBarcodeFormats(Barcode.QR_CODE)
                .build();
        if(!barcode.isOperational()){
            Toast.makeText(getApplicationContext(), "Sorry, Couldn't setup the detector", Toast.LENGTH_LONG).show();
            this.finish();
        }
        cameraSource = new CameraSource.Builder(this, barcode)
                .setFacing(CameraSource.CAMERA_FACING_BACK)
                .setRequestedFps(24)
                .setAutoFocusEnabled(true)
                .setRequestedPreviewSize(1920,1024)
                .build();
        cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try{
                    if(ContextCompat.checkSelfPermission(LeerQR.this, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
                        cameraSource.start(cameraView.getHolder());
                    }
                }
                catch (IOException e){
                    e.printStackTrace();
                }
            }



            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                cameraSource.stop();

            }
        });
        barcode.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {

            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {
                final SparseArray<Barcode> barcodes =  detections.getDetectedItems();
                if(barcodes.size() > 0){
                    Intent intent = new Intent();
                    intent.putExtra("barcode", barcodes.valueAt(0));
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
    }

The fact is that I do not know how to process the variables that he reads from the scanner, it would usually be a numeric variable and an email, but no idea how he would read it.

I update:

Identify in which part of the code the variable brings me, it brings it to me complete as a text string, it is here: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == REQUEST_CODE && resultCode == RESULT_OK){

        if(data != null){

// Here I have a single text string, which I want to separate into several // variables to be able to use it in my application                 final Barcode barcode = data.getParcelableExtra ("barcode");                 result.post (new Runnable () {                     @Override                     public void run () { // This TextView does not need it, but I do not know how to convert this to a string // more than doing it the way it appears here                         result.setText (barcode.displayValue);                         String result = result.getText (). ToString (); // The toast was to test if I could convert the result to a string, and if it can, but I must show the TextView that should not be shown
Toast.makeText (PreScan.this, result, Toast.LENGTH_SHORT) .show ();                         //scanbtn.setVisibility(View.GONE);                     }                 });             }         }     }

They gave me the idea of the string split, but no idea how to apply it

    
asked by Jesus Moran 11.08.2017 в 04:57
source

1 answer

1

In your question, do not indicate the format of the chain that you retrieve from the QR. For cases where you need to separate a string into multiple strings it is necessary that the original string have some character that is used as a delimiter and use the Split () method.

// Suponiendo que barcode tienen un formato tipo: "var1;var2;var3"
String[] variables = barcode.split(":"); // split() debe regresar un arreglo de cadenas con 3 elementos

// Inspeccionando los elementos del array utilizando el índice.
variables[0]; // esto contendrá "var1"
variables[2]; // esto contendrá "var3"

// Tambien puedes usar un loop for para inspeccionar el arreglo
for (String var : variables)
{
   // aqui puedes usar var directamente
}

You can delve more into the following league:   link

    
answered by 11.08.2017 / 10:26
source