Asim Gulzar
Asim Gulzar

Reputation: 31

Unable to get result from ZXing barcode scanner

I am trying to implement barcode scanner activity in my Android application but I heard that it's not possible to do so without asking the user to install the ZXing bar code scanner.

So what I did is that I used the ZXing integrator class for initiating a scan here is my code. But I am unable to show the result in the two text views. I must mention that the scan is started and the result is shown in the barcode scanner window itself, but then nothing comes on the ScanCodeActivity layout which has two text views on it.

I also would like to know if it's possible to return the result from th eweb into my text views.

I used this link as reference: http://code.tutsplus.com/tutorials/android-sdk-create-a-barcode-reader--mobile-17162

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class ScanCodeActivity extends Activity {

    private Button scan;

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

        scan= (Button)findViewById(R.id.scan_button);

        Button scanBtn  = (Button)findViewById(R.id.scan_button);
        final TextView  formatTxt = (TextView)findViewById(R.id.scan_format);
        final TextView contentTxt = (TextView)findViewById(R.id.scan_content);

        scanBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentIntegrator scanIntegrator = new IntentIntegrator(ScanCodeActivity.this);
                scanIntegrator.initiateScan();
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
           if (requestCode == 0) {
               IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
               if (resultCode == RESULT_OK) {
                   if (scanningResult != null) {
                       final TextView  formatTxt = (TextView)findViewById(R.id.scan_format);
                       final TextView contentTxt = (TextView)findViewById(R.id.scan_content);
                       String scanContent = scanningResult.getContents();
                       String scanFormat = scanningResult.getFormatName();
                       Toast toast = Toast.makeText(this, "Content:" + scanContent + " Format:" + scanFormat , Toast.LENGTH_LONG);

                       formatTxt.setText("FORMAT: " + scanFormat);
                       contentTxt.setText("CONTENT: " + scanContent);
                     //we have a result
                     }
                   else{
                        Toast toast = Toast.makeText(getApplicationContext(), 
                            "No scan data received!", Toast.LENGTH_SHORT);
                        toast.show();
                    }
               } else if (resultCode == RESULT_CANCELED) {
                  // Handle cancel
                  Log.i("App","Scan unsuccessful");
               }
         }
      }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Upvotes: 1

Views: 2336

Answers (1)

cygery
cygery

Reputation: 2319

Remove the if (requestCode == 0) check from your onActivityResult() method similar to the example from the library's wiki page.

Note: You are checking for the wrong requestCode. The library uses 0x0000c0de (see the source) instead of 0.

Upvotes: 3

Related Questions