title: How to create a modular class for barcode scanning using Firebase ML Kit
body: <p>I want to create a modular class that I can use anytime I want to scan a barcode. Is this possible using Firebase ML Kit? </p>
  
  <p>This is what I've got so far:</p>
  
  <pre><code>public List&lt;FirebaseVisionBarcode&gt; ScanBarcode(int... barcodeFormats)
  {
      //region Init, config and execution of the barcode scanning
      final FirebaseVisionBarcodeDetectorOptions.Builder BUILDER =
              new FirebaseVisionBarcodeDetectorOptions.Builder();
  
      //Set barcode formats based on arguments
      for (int formats : barcodeFormats)
      {
          BUILDER.setBarcodeFormats(formats);
      }
  
      final FirebaseVisionBarcodeDetectorOptions OPTIONS = BUILDER.build();
  
      final FirebaseVisionImage IMAGE = FirebaseVisionImage.fromBitmap(bitmap);
  
      final FirebaseVisionBarcodeDetector DETECTOR = FirebaseVision.getInstance()
              .getVisionBarcodeDetector(OPTIONS);
  
      DETECTOR.detectInImage(IMAGE)
              .addOnSuccessListener(new OnSuccessListener&lt;List&lt;FirebaseVisionBarcode&gt;&gt;()
              {
                  @Override
                  public void onSuccess(List&lt;FirebaseVisionBarcode&gt; _barcodes)
                  {
                      barcodes = _barcodes;
                  }
              })
  
              .addOnFailureListener(new OnFailureListener()
              {
                  @Override
                  public void onFailure(@NonNull Exception e)
                  {
                      barcodes = new ArrayList&lt;&gt;();
                  }
              });
  
      //endregion
  
      return barcodes;
  </code></pre>
  
  <p>Mostly taken from <a href="https://firebase.google.com/docs/ml-kit/" rel="nofollow noreferrer">https://firebase.google.com/docs/ml-kit/</a></p>
  
  <p>The problem here is that the listener, for the <code>DETECTOR</code>, is some kind of async call. Which means that it will return before <code>onSuccess</code> is called.</p>
  
  <p>The barcode scanning part works, but I'm having a hard time wrapping it in a class of it's own.</p>
  
  <p>Thanks.</p>