Search code examples
androidandroid-fragmentsandroid-cameraqr-codezxing

In fragment, how to stop camera of ZXing Scanner?


Following this tutorial(http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/), I made Android tabs With fragments and ViewPager. So far I added a QR code scanner (using Barcode Scanner library based on ZXing) to one of the fragments and that is working fine.

The problem is that when I go to other tabs(fragments), the camera itself keeps working internally(the internal camera doesn't stop). So even though I don't see a camera on the screen on different fragments, when I place my phone (the camera part) close to a QR code, it reads it and start a new activity.

So how can I stop camera when I go to other fragments?

This is the fragment for my QR code scanner.

public class TabFragment1 extends Fragment implements ZXingScannerView.ResultHandler
{
   private ZXingScannerView mScannerView;
   private LinearLayout qrCameraLayout;

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
   {
      View v = inflater.inflate(R.layout.tab_fragment_1, container, false);

      qrCameraLayout = (LinearLayout) v.findViewById(R.id.ll_qrcamera);
      mScannerView = new ZXingScannerView(getActivity().getApplicationContext());
      mScannerView.setLayoutParams(new LinearLayout.LayoutParams(
         LinearLayout.LayoutParams.MATCH_PARENT,
         LinearLayout.LayoutParams.MATCH_PARENT));
      qrCameraLayout.addView(mScannerView);

      List<BarcodeFormat> formats = new ArrayList<>();
      formats.add(BarcodeFormat.QR_CODE);
      mScannerView.setFormats(formats);

      return v;
   }

   @Override
   public void onResume()
   {
      super.onResume();
      mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
      mScannerView.startCamera();          // Start camera on resume
   }

   @Override
   public void onPause()
   {
      super.onPause();
      mScannerView.stopCamera();
   }

   @Override
   public void handleResult(final Result result)
   {
      //handling results
   }

Solution

  • I have tried some way to get solve this problem and finally got the solution.

    public void handleResult(Result result) {
            //Hold result
            Log.e("handler", result.getText()); // Prints scan results
            Log.e("handler", result.getBarcodeFormat().toString()); // Prints the scan format (qrcode)
            mScannerView.removeAllViews(); //<- here remove all the views, it will make an Activity having no View
            mScannerView.stopCamera(); //<- then stop the camera
            setContentView(R.layout.activity_Main); //<- and set the View again.
            final String vString = result.getText();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(mContext,vString,Toast.LENGTH_LONG).show();
                }
            });
            // to resume scanning
            // mScannerView.resumeCameraPreview(this);<br />
        }
    }
    

    Setting the ContentView again solved the issue for m