title: mobile vision Qrcode Call intent multiple time
body: <p>I made a <strong>QrScanner</strong> apps for scanning using google mobile vision. App is simple like scannig a Qrcode , app decode the Qrcode ,deliver it to result class and result shows in the Result layout. But the problem is When im trying to Scan Qrcode , Some how it call result class multiple time ... heres my MainActivity code : </p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  private static final String TAG = "MainActivity";
  SurfaceView surfaceView;
  BarcodeDetector barcodeDetector;
  CameraSource cameraSource;
  final int RequestCameraID = 1001;
  BoxDetector boxDetector;
  
  
  
  
  @Override
  protected void onStop() {
  
      super.onStop();
      Toast.makeText(getApplicationContext(),"Stop",Toast.LENGTH_LONG).show();
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      surfaceView = findViewById(R.id.cameraView);
      surfaceView.setZOrderMediaOverlay(true);
      if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PERMISSION_GRANTED) {
          ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSION_GRANTED);
      }
      Casting();
  }
  
  private void Casting() {
     barcodeDetector = new BarcodeDetector.Builder(this)
              .setBarcodeFormats(Barcode.QR_CODE)
              .build();
      boxDetector = new BoxDetector(barcodeDetector, 300, 300);
  
      if (!barcodeDetector.isOperational()) {
          Toast.makeText(MainActivity.this, "Sorry couldn't setup the detector", Toast.LENGTH_LONG).show();
      }
  
      cameraSource = new CameraSource.Builder(MainActivity.this, boxDetector)
              .setFacing(CameraSource.CAMERA_FACING_BACK)
              .setRequestedFps(30)
              .setAutoFocusEnabled(true)
              .setRequestedPreviewSize(1280, 720)
              .build();
  
      surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
          @Override
          public void surfaceCreated(SurfaceHolder holder) {
              if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
  
                  ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA},RequestCameraID);
                  return;
              }
  
              try {
                  cameraSource.start(surfaceView.getHolder());
  
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
  
          @Override
          public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
  
          @Override
          public void surfaceDestroyed(SurfaceHolder holder) {
              cameraSource.stop();
  
          }
      });
  
  
      boxDetector.setProcessor(new Detector.Processor&lt;Barcode&gt;() {
          @Override
          public void release() {
          }
  
          @Override
          public void receiveDetections(Detector.Detections&lt;Barcode&gt; detections) {
              final SparseArray&lt;Barcode&gt; barcodeSparseArray = detections.getDetectedItems();
              if(barcodeSparseArray.size()!=0){
                  Handler handler = new Handler(Looper.getMainLooper());
                  handler.post(new Runnable() {
                      @Override
                      public void run() {
                          cameraSource.release();
                          barcodeDetector.release();
  
                          Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                          intent.putExtra("Result",barcodeSparseArray.valueAt(0).displayValue);
                          Casting();
                          startActivity(intent);
                      }
                  });
              }
          }
      });
  }
  
  public void cS() {
      barcodeDetector = new BarcodeDetector.Builder(this)
              .setBarcodeFormats(Barcode.QR_CODE)
              .build();
      boxDetector = new BoxDetector(barcodeDetector, 300, 300);
  
      if (!barcodeDetector.isOperational()) {
          Toast.makeText(MainActivity.this, "Sorry couldn't setup the detector", Toast.LENGTH_LONG).show();
      }
  
      cameraSource = new CameraSource.Builder(MainActivity.this, boxDetector)
              .setFacing(CameraSource.CAMERA_FACING_BACK)
              .setRequestedFps(30)
              .setAutoFocusEnabled(true)
              .setRequestedPreviewSize(1280, 720)
              .build();
  }
  
  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
      switch (requestCode) {
          case RequestCameraID: {
              if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                      return;
                  }
                  try {
                      cameraSource.start(surfaceView.getHolder());
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
  }}
  </code></pre>
  
  <p>BoxDetector Code is:</p>
  
  <pre><code>public class BoxDetector extends Detector {
  private Detector mDelegate;
  private int mBoxWidth, mBoxHeight;
  
  
  
  BoxDetector(Detector delegate, int boxWidth, int boxHeight) {
      mDelegate = delegate;
      mBoxWidth = boxWidth;
      mBoxHeight = boxHeight;
  
  }
  
  public SparseArray detect(Frame frame) {
  
      int width = frame.getMetadata().getWidth();
      int height = frame.getMetadata().getHeight();
      int right = ((width / 2) + (mBoxHeight / 2)) -150 ;
      int left = ((width / 2) - (mBoxHeight / 2)) - 150;
      int bottom = ((height / 2) + (mBoxWidth / 2));
      int top = ((height / 2) - ((mBoxWidth) / 2));
  
      YuvImage yuvImage = new YuvImage(frame.getGrayscaleImageData().array(), ImageFormat.NV21, width, height, null);
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      yuvImage.compressToJpeg(new Rect(left, top, right, bottom), 100, byteArrayOutputStream);
  
      byte[] jpegArray = byteArrayOutputStream.toByteArray();
      Bitmap bitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);
  
      Frame croppedFrame =
              new Frame.Builder()
                      .setBitmap(bitmap)
                      .setRotation(frame.getMetadata().getRotation())
                      .build();
  
  
      return mDelegate.detect(croppedFrame);
  
  }
  
  public void run(){
      try {
          Thread.sleep(300);
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  }
  
  
  
  public boolean setFocus(int id) {
      return mDelegate.setFocus(id);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Thats it Thank You!</p>