Register now for Android Dev Summit 2019!

Analyze images

The image analysis use case provides your app with a CPU-accessible image to perform image processing, computer vision, or machine learning inference on. The application implements an Analyzer method that is run on each frame.

Implementation

Images are processed by passing a method that takes an ImageProxy parameter and a rotation parameter to the setAnalyzer() method.

The following code example shows how to do this, as well as how to bind the image analysis use case and a preview use case to the LifecycleOwner. To read about creating preview use cases, see Implement a preview.

Upon return from this method, the image reference is closed. Therefore, the method should complete analysis or make a copy instead of passing the image reference beyond the analysis method.

Kotlin

val imageAnalysisConfig = ImageAnalysisConfig.Builder()
    .setTargetResolution(Size(1280, 720))
    .build()
val imageAnalysis = ImageAnalysis(imageAnalysisConfig)

imageAnalysis.setAnalyzer({ image: ImageProxy, rotationDegrees: Int ->
    // insert your code here.
})

CameraX.bindToLifecycle(this as LifecycleOwner, imageAnalysis, preview)

Java

ImageAnalysisConfig config =
    new ImageAnalysisConfig.Builder()
        .setTargetResolution(new Size(1280, 720))
        .build();

ImageAnalysis imageAnalysis = new ImageAnalysis(config);

imageAnalysis.setAnalyzer(
    new ImageAnalysis.Analyzer() {
        @Override
        public void analyze(ImageProxy image, int rotationDegrees) {
            // insert your code here.
        }
});

CameraX.bindToLifecycle((LifecycleOwner) this, imageAnalysis, preview);

CameraX produces images in YUV_422_888 format.

Additional resources

To learn more about CameraX, consult the following additional resources.

Codelab

  • Getting Started with CameraX
  • Code sample

  • Official CameraX sample app