title: Android CameraX analyse image stream
body: <p>I want to analyse a stream of image frames and do some computation on it. However, with CameraX, the ImageAnalysis implementation class seems to get called only once - on camera bind. </p>
  
  <p>My question is: How do I run the analysis on a continuous stream of images - conceptually akin to a video stream?</p>
  
  <p>Following is my camera, preview and analysis setup code:</p>
  
  <pre><code>private void setPreview() {
      ListenableFuture&lt;ProcessCameraProvider&gt; instance = ProcessCameraProvider.getInstance(this);
  
      Activity self = this;
      instance.addListener(() -&gt; {
  
          try {
              ProcessCameraProvider cameraProvider = instance.get();
              Preview preview = new Preview.Builder().build();
  
  
              ImageAnalysis imageAnalysis = new ImageAnalysis.Builder().build();
              imageAnalysis.setAnalyzer(Executors.newFixedThreadPool(1),
                                                      new ImageAnalyser(new CameraLogListener() {
                                                          @Override
                                                          public void log(String msg) {
                                                              Log.e("Camera log", msg);
                                                          }
                                                      }));
  
              CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
  
              cameraProvider.unbindAll();
              Camera camera = cameraProvider.bindToLifecycle(((LifecycleOwner)self), cameraSelector, preview, imageAnalysis);
  
              preview.setSurfaceProvider(
                      ((PreviewView)findViewById(R.id.cameraTextureView)).createSurfaceProvider(camera.getCameraInfo()));
  
          } catch (ExecutionException e) {
              e.printStackTrace();
              Log.e("TAG", "Use case binding failed", e);
          } catch (InterruptedException e) {
              e.printStackTrace();
              Log.e("TAG", "Use case binding failed", e);
          }
  
      }, ContextCompat.getMainExecutor(this));
  
  }
  </code></pre>
  
  <p>Following is my ImageAnalysis implementation class:</p>
  
  <pre><code>private class ImageAnalyser implements ImageAnalysis.Analyzer {
  
      CameraLogListener listener;
      public ImageAnalyser(CameraLogListener listener) {
          this.listener = listener;
      }
  
      @Override
      public void analyze(@NonNull ImageProxy image) {
          ByteBuffer imageBuffer = image.getPlanes()[0].getBuffer();
          StringBuilder sb = new StringBuilder();
          sb.append("format:" + image.getFormat()).append("\n")
              .append(image.getWidth() + " x " + image.getHeight()).append("\n\n");
  
          for (int i=0; i&lt;image.getPlanes().length; i++) {
              sb.append("pixel stride:").append(image.getPlanes()[i].getPixelStride())
                  .append("\nrow stride:").append(image.getPlanes()[i].getRowStride())
                  .append("\n");
          }
  
          listener.log(sb.toString());
  
      }
  }
  </code></pre>