简体   繁体   中英

Serial scan using Zxing library - android

i'm developing a scan Barcode app on android , my application is simple and composed from an activity which contains a button and a textView which will receive the result of the scan..
The app works well but i want that i could realise serial scan in a raw. so after scanning a barcode i need that the capture Activity stay and the appli don't back to the button activity so i can scan the next Barcode. any solution please ? this is my main java code :

public class MainActivity extends Activity {
    private Button scan;

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

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

   scan.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View v) {
           // TODO Auto-generated method stub  
           Intent intent = new Intent("com.google.zxing.client.android.SCAN");
           intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
           startActivityForResult(intent, 0);
       }
   });
}


public boolean onTouchEvent(final MotionEvent event) {

    IntentIntegrator integrator = new IntentIntegrator();
    integrator.initiateScan(null);

    return true;
    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

   super.onActivityResult(requestCode, resultCode, data);
   if (requestCode == 0) {
       if (resultCode == RESULT_OK) {

               String contents = data.getStringExtra("SCAN_RESULT");
               TextView tv = (TextView) findViewById(R.id.scanResult);
               tv.setText(data.getStringExtra("SCAN_RESULT"));//this is the result
       } else 

       if (resultCode == RESULT_CANCELED) {

         // Handle cancel

   } }
}
@Override
public void onConfigurationChanged(Configuration newConfig){        
   super.onConfigurationChanged(newConfig);
}
}

I had the same problem when I did my scanner activity, and the solution that I found was to make my mainActivity extends Zxing CaptureActivity, like this I overrided handleDecode and I avoided to switch between different activities (as you have to do to obtain your scanner result).

Anyhow, to restart the scanning process after a precedent scan I called the method

  restartPreviewAfterDelay(0L)

(that is a method of CaptureActivity) in the onClick function of a button.

Take a look at that method, I think that it is what you need.

i finally found the solution i had just to add this code in the onActivityResult() declaration

           Intent intent = new Intent("com.google.zxing.client.android.SCAN");
           intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE",              "QR_CODE_MODE");
           startActivityForResult(intent, 0);

So after the scan is finished the app is ready to scan again instead of going back to the home activity

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