title: ML Kit Vision - barcode detection using a camerax in memory image results in null pointer exception due to 'int android.graphics.Bitmap.getWidth()'
body: <p>I'm getting the following error when trying to detect barcodes in an in memory image.
  <a href="https://i.stack.imgur.com/CFh5X.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CFh5X.png" alt="enter image description here"></a></p>
  
  <p>Is there something wrong with my camera configuration? There are some optimizations on the image capturing I still need to do, but I wanted to get the barcode detection working. It seems to capture the image successfully and is in the jpeg format, which <code>FireBaseVisionImage.FromMediaImage</code> supports.</p>
  
  <p>Camera/scanner activity</p>
  
  <pre><code>package com.example.kusdinventoryscanner;
  
  import androidx.annotation.NonNull;
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.camera.core.Camera;
  import androidx.camera.core.CameraSelector;
  import androidx.camera.core.ImageCapture;
  import androidx.camera.core.ImageCaptureException;
  import androidx.camera.core.ImageProxy;
  import androidx.camera.core.Preview;
  import androidx.camera.lifecycle.ProcessCameraProvider;
  import androidx.camera.view.PreviewView;
  import androidx.core.app.ActivityCompat;
  import androidx.core.content.ContextCompat;
  import androidx.lifecycle.LifecycleOwner;
  
  import android.Manifest;
  import android.content.pm.PackageManager;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  
  import com.google.common.util.concurrent.ListenableFuture;
  
  import java.util.concurrent.ExecutionException;
  import java.util.concurrent.Executors;
  import java.util.concurrent.ExecutorService;
  
  
  public class ScannerActivity extends AppCompatActivity implements View.OnClickListener {
      private static final String TAG = ScannerActivity.class.getSimpleName();
      private static final int PERMISSIONS_REQUEST_CODE = 42;
      private ImageAnalyzer imageAnalyzer;
      private ListenableFuture&lt;ProcessCameraProvider&gt; cameraProviderFuture;
      private ImageCapture imageCapture;
      private ExecutorService exec;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_scanner);
  
          if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                  == PackageManager.PERMISSION_DENIED)
              ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CODE);
  
          imageAnalyzer = new ImageAnalyzer();
          exec = Executors.newSingleThreadExecutor();
  
          cameraProviderFuture = ProcessCameraProvider.getInstance(this);
          cameraProviderFuture.addListener(() -&gt; {
              try {
                  ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
                  bindPreview(cameraProvider);
              } catch (ExecutionException | InterruptedException e) {
                  // No errors need to be handled for this Future.
                  // This should never be reached.
              }
          }, ContextCompat.getMainExecutor(this));
  
          imageCapture =
                  new ImageCapture.Builder()
                          .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
                          .build();
      }
  
      void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
          Preview preview = new Preview.Builder()
                  .build();
  
          CameraSelector cameraSelector = new CameraSelector.Builder()
                  .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                  .build();
  
          Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector, imageCapture, preview);
          PreviewView previewView = (PreviewView) findViewById(R.id.preview_view);
          preview.setSurfaceProvider(previewView.createSurfaceProvider(camera.getCameraInfo()));
      }
  
      @Override
      @androidx.camera.core.ExperimentalGetImage
      public void onClick(View v) {
          if (v.getId() == R.id.scan_barcode) {
              Log.d(TAG, "ENTERED CLICK HANDLER");
              imageCapture.takePicture(exec,  new ImageCapture.OnImageCapturedCallback() {
                  @Override
                  public void onCaptureSuccess(ImageProxy image) {
                      Log.d(TAG, "SUCCESS: Image captured successfully.");
                      imageAnalyzer.analyze(image);
                  }
                  public void onError(ImageCaptureException e) {
                      Log.d(TAG, "CAPTURE ERROR: " +  e.toString());
                  }
              });
          }
      }
  
      @Override
      protected void onStop() {
          super.onStop();
  
          exec.shutdown();
      }
  }
  </code></pre>
  
  <p>activity_scanner.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".ScannerActivity"&gt;
  
      &lt;androidx.constraintlayout.widget.ConstraintLayout
          android:id="@+id/container"
          android:layout_width="394dp"
          android:layout_height="318dp"
          android:layout_marginTop="32dp"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.47"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"&gt;
  
          &lt;androidx.camera.view.PreviewView
              android:id="@+id/preview_view"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" /&gt;
      &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  
      &lt;Button
          android:id="@+id/scan_barcode"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="272dp"
          android:onClick="onClick"
          android:text="@string/scan_image"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.498"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/container" /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p>Analyzer </p>
  
  <pre><code>package com.example.kusdinventoryscanner;
  
  import android.media.Image;
  import android.util.Log;
  
  import androidx.annotation.NonNull;
  import androidx.camera.core.ImageAnalysis;
  import androidx.camera.core.ImageProxy;
  
  import com.google.android.gms.tasks.OnFailureListener;
  import com.google.android.gms.tasks.OnSuccessListener;
  import com.google.android.gms.tasks.Task;
  import com.google.firebase.ml.vision.FirebaseVision;
  import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcode;
  import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcodeDetector;
  import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcodeDetectorOptions;
  import com.google.firebase.ml.vision.common.FirebaseVisionImage;
  import com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata;
  
  import java.util.List;
  
  public class ImageAnalyzer implements ImageAnalysis.Analyzer {
      private static final String TAG =  ImageAnalyzer.class.getSimpleName();
      private FirebaseVisionBarcodeDetectorOptions barcodeOptions;
  
      public ImageAnalyzer() {
          barcodeOptions =
              new FirebaseVisionBarcodeDetectorOptions.Builder()
                      .setBarcodeFormats(FirebaseVisionBarcode.FORMAT_CODE_39)
                      .build();
  
      }
  
      private int degreesToFirebaseRotation(int degrees) {
          switch (degrees) {
              case 0:
                  return FirebaseVisionImageMetadata.ROTATION_0;
              case 90:
                  return FirebaseVisionImageMetadata.ROTATION_90;
              case 180:
                  return FirebaseVisionImageMetadata.ROTATION_180;
              case 270:
                  return FirebaseVisionImageMetadata.ROTATION_270;
              default:
                  throw new IllegalArgumentException(
                          "Rotation must be 0, 90, 180, or 270.");
          }
      }
  
      @Override
      @androidx.camera.core.ExperimentalGetImage
      public void analyze(ImageProxy imageProxy) {
          if (imageProxy == null || imageProxy.getImage() == null) {
              return;
          }
          Image mediaImage = imageProxy.getImage();
          int rotation = degreesToFirebaseRotation(imageProxy.getImageInfo().getRotationDegrees());
          FirebaseVisionImage image =
                  FirebaseVisionImage.fromMediaImage(mediaImage, rotation);
  
          FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance()
                  .getVisionBarcodeDetector(barcodeOptions);
  
          Task&lt;List&lt;FirebaseVisionBarcode&gt;&gt; result = detector.detectInImage(image)
                  .addOnSuccessListener(new OnSuccessListener&lt;List&lt;FirebaseVisionBarcode&gt;&gt;() {
                      @Override
                      public void onSuccess(List&lt;FirebaseVisionBarcode&gt; barcodes) {
                          // Task completed successfully
                          // ...
                          for (FirebaseVisionBarcode barcode: barcodes) {
                              String asset = barcode.getRawValue();
                              Log.d(TAG, "Barcode is: " + asset);
                          }
                      }
                  })
                  .addOnFailureListener(new OnFailureListener() {
                      @Override
                      public void onFailure(@NonNull Exception e) {
                          Log.d(TAG, "Detect failed.");
                      }
                  });
      }
  }
  
  </code></pre>