Search code examples
javaandroidzxingbarcode-scanner

Unable to capture barcode scan results


I am using the Zxing library to scan an item and store the result. When I scan, the scan results (the set of numbers) is temporarily shown on the barcode scanner, but when I try to capture and display it via Toast for testing, it returns as null.

//this method occurs when I click a button
final String ZXING_SCAN = "com.google.zxing.client.android.SCAN";
public void scanItem(View view){
        try {
            //works. able to activate the barcode scanner. 
            i = new Intent(ZXING_SCAN);
            i.putExtra("SCAN_MODE", "PRODUCT_MODE");
            startActivityForResult(i, 0);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

//Using this method to capture the scan result. The toast prints out null for both values.
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 0){
            if(resultCode == RESULT_OK){
                String content = i.getStringExtra("SCAN_RESULT");
                String format = i.getStringExtra("SCAN_RESULT_FORMAT");

                //This is he toast that shows null for both. 
                Toast.makeText(getApplicationContext(), "Content: " + content + ", Format: " + format, Toast.LENGTH_LONG).show();
            }
            else {
                //Toast.makeText(getApplicationContext(), "Barcode not working...", Toast.LENGTH_LONG).show();
            }
        }
    }

Solution

  • Instead of

    String content = i.getStringExtra("SCAN_RESULT");  
    String format = i.getStringExtra("SCAN_RESULT_FORMAT"); 
    

    in your onActivityResult try

    String content = data.getStringExtra("SCAN_RESULT");
    String format = data.getStringExtra("SCAN_RESULT_FORMAT");