In fragment, how to stop camera of ZXing Scanner?

11,356

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

Share:
11,356
Izzy Moriguchi
Author by

Izzy Moriguchi

Updated on June 04, 2022

Comments

  • Izzy Moriguchi
    Izzy Moriguchi almost 2 years

    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
       }
    
  • Izzy Moriguchi
    Izzy Moriguchi almost 8 years
    Unfortunately I'm getting NullPointerException. java.lang.NullPointerException: Attempt to invoke virtual method 'void me.dm7.barcodescanner.zxing.ZXingScannerView.stopCamera()' on a null object reference
  • iSandeep
    iSandeep over 7 years
    Anyone got the reason? I'm using Activity and called stopCamera() as I get the result but Camera remains ON. How to come back from Camera View?
  • Rahul
    Rahul over 7 years
    put some logs near startCamera() and check how many times its getting called or check stopCamera() should not throw any error, am sure there is some problem in your code