简体   繁体   中英

Zxing barcode matching app for android

I'm trying to develop an app which will scan a barcode, store the value in a variable, then scan another barcode and match that value with the previous value and return a 'match' or 'no match' result. I have already got the first part working thanks to help from foamyguy, here's the initial code that I used,

package com.barcodesample;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class BarcodeSample extends Activity {

private Button scanBtn;
private TextView resultsTxt;


/** Called when the activity is first created. */;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    scanBtn = (Button) findViewById(R.id.scanBtn);
    resultsTxt = (TextView) findViewById(R.id.resultsTxt);




    scanBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            resultsTxt.setText("Scanning...");




        Intent intent = new intent("com.google.zxing.client.android.SCAN"); 


            try {
                startActivityForResult(intent, 0);
            } catch (ActivityNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

                new AlertDialog.Builder(v.getContext())
                .setTitle("WARNING:")
                            .setMessage("You don't have Barcode Scanner  installed. Please install it.")
                .setCancelable(false) 
                .setNeutralButton("Install it now", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");  
                        startActivity(new Intent(Intent.ACTION_VIEW, uri));                                         
                    }
                })
                .show();
            }

        }
    });



}

/*Here is where we come back after the Barcode Scanner is done*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            // contents contains whatever the code was
            String contents = intent.getStringExtra("SCAN_RESULT");

            // Format contains the type of code i.e. UPC, EAN, QRCode etc...
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

            // Handle successful scan. In this example I just put the results into the TextView
            resultsTxt.setText(format + "\n" + contents);
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel. If the user presses 'back' before a code is scanned.
            resultsTxt.setText("Canceled");
        }
    }
}

}

Now how do I start another intent with another scan button similar to the one above, and finally how to compare both values and return the match or no match in a new screen?

Thanks in advance for the valuable advice

Use the same code on the other button except following.

startActivityForResult(intent, 1); // right now it's 0 for button 1

Now add one more section in onActivityResult for returning results for 2nd button.

if (requestCode == 1) {
// your code here that will be used for comparing.
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM