Search code examples
androidqr-codezxingbarcode-scanner

Showing scan result in another activity


i want to show the result scan in another activity, im using the zxing library, any advice?

This is the button that triggers the scan camera on MainActivity

        scanQR.setOnClickListener(new View.OnClickListener(){
        private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
                result -> {
                    if(result.getContents() == null) {
                        Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(MainActivity.this, "Scan: " + result.getContents(), Toast.LENGTH_LONG).show();
                    }
                });
        @Override
        public void onClick(View view){
            ScanOptions options = new ScanOptions();
            options.setDesiredBarcodeFormats(ScanOptions.ALL_CODE_TYPES);
            options.setPrompt("Scan a product");
            options.setBeepEnabled(false);
            options.setBarcodeImageEnabled(false);
            options.setOrientationLocked(true);
            options.setCaptureActivity(CaptureActivity.class);
            barcodeLauncher.launch(options);
        }
    });

And this is the ScanProduct.java activity where i want to show the scan result

TextView resultScan;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan_product);

    resultScan = findViewById(R.id.result_scan);
    

}

Solution

  • Try this -

    1. store scan results in some variable.

    2. Pass that result data by Intent while calling ScanProduct.java Activity and pass data using intent.

       Intent intent = new Intent(MainActivity.this, ScanProduct.class);    
       intent.putExtra("result",result);
       startActivity(intent);
      
    3. receive this data in ScanProduct.class and use it.