简体   繁体   中英

How to store results from QR Code after scanning?

I'm relatively new to Android Studio. I'm working on a QR code scanner. I found this online and I'm just working with it:

import android.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
    private ZXingScannerView mScannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
        setContentView(mScannerView);

        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();         // Start camera
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause
    }

    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here

        Log.e("handler", rawResult.getText()); // Prints scan results
        Log.e("handler", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode)

        // show the scanner result into dialog box.
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Scan Result");
        builder.setMessage(rawResult.getText());
        AlertDialog alert1 = builder.create();
        alert1.show();


        // If you would like to resume scanning, call this method below:
        mScannerView.resumeCameraPreview(this);
    }
}

I want to store the scan results in a ListView in another activity but I'm not sure how to get that to work. I already know how to create a ListView, I just want to be able to pass the results to that next activity. I already looked around here but didn't find anything that helped. Any bit of help would be greatly appreciated. Thank you very much.

From what you have described I am assuming you have to scan a bunch of barcodes together/continuously and pass them on to the next activity which is a list view.

In your sender activity you need to store all the scanned results in a HashMap/Arraylist depending on you choice of Collection. I am explaining for a HashMap and how to send it across activities.

Sender Activity:

Intent intent = new Intent(FromActivity.this, ToActivity.class);
intent.putExtra("SCAN_RESULTS", hashMap);
startActivity(intent);

Receiver Activity:

Intent intent = getIntent();    
HashMap<String, String> hashMap = (HashMap<String, String>)      intent.getSerializableExtra("SCAN_RESULTS");

Hope this helps.

I was doing something similar but using the google vision APIs, I'd recommend looking at these packages:

  • com.google.android.gms.vision.barcode.Barcode
  • com.google.android.gms.vision.barcode.BarcodeDetector

As well as this stack overflow post .

I believe the general answer to your question though of how to send the data between activities is to use an Intent object and specify the item you want to send as "extra" within that intent.

Intent data = new Intent( getApplicationContext(), MyListViewActivity.class );
data.putExtra( "Barcode", barcodeObject );
startActivity( data );

Another option would be to start your scan activity from your ListView activity and use the "startActivityForResult" method.

Intent scanIntent = new Intent( getApplicationContext(), MyScanActivity.class );
startActivityForResult( scanIntent, "BARCODE_CAPTURE" );

Then you would implement onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == "BARCODE_CAPTURE") {
        if( resultCode == CommonStatusCodes.SUCCESS ) {
            if (data != null) {
                Barcode barcode = data.getParcelableExtra("Barcode");
             ...
            }}}}

In your Barcode capture activity you would then return the results in an Intent.

 Intent data = new Intent();
 data.putExtra("Barcode", barcode);
 setResult(CommonStatusCodes.SUCCESS, data);
 finish();

You can use

   ArrayList<String> listOfBarcodes = new ArrayList<String>();
   //whenever you capture a new barcode add it to this list
   listOfBarcodes.add(currentBarcode);

   //when you are done adding barcodes call the ListView activity and pass
   //it the listOfBarcodes as a Serializable.
   Intent intent = new Intent(getApplicationContext(), ActivityToStart.class);
   intent.putExtra("Barcodes", listOfBarcodes);
   startActivity(Intent);

Then in that Activity you will have a big list of barcodes. Add the new barcodes to it and refresh the ListView

  Intent callingIntent= getIntent();
  ArrayList<String> listOfBarcode = callingIntent.getSerializableExtra("Barcodes");

  completeList.add(listOfBarcodes);
  ArrayAdapter<String> aA = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, completeList);
  ListView lV = (ListView) findViewById(R.id.my_list_id);
  lV.setAdapter(aA);

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