The Android Developer Challenge is back! Submit your idea before December 2.

Implement a preview

The preview use case produces a SurfaceTexture which streams the camera input. It also provides additional information for a View to crop, scale, or rotate for proper display.

The image preview is streamed to this SurfaceTexture when the camera becomes active. The SurfaceTexture can be connected to a TextureView or a GLSurfaceView.


The following sample shows how to use PreviewOutput:


val previewConfig = PreviewConfig.Builder().build()
val preview = Preview(previewConfig)

preview.setOnPreviewOutputUpdateListener {
    previewOutput: Preview.PreviewOutput? ->
        // Your code here. For example, use previewOutput?.getSurfaceTexture()
        // and post to a GL renderer.

CameraX.bindToLifecycle(this as LifecycleOwner, preview)


PreviewConfig config = new PreviewConfig.Builder().build();
Preview preview = new Preview(config);

    new Preview.OnPreviewOutputUpdateListener() {
        public void onUpdated(Preview.PreviewOutput previewOutput) {
            // Your code here. For example, use previewOutput.getSurfaceTexture()
            // and post to a GL renderer.

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

Use CameraView

CameraX provides a CameraView class that uses a Preview to implement a more convenient View API. The View API automatically scales, crops, and rotates the image data.

Torch mode, focus, and zoom controls are implemented in this preview. Unlike use cases, where configuration is set at creation, you can set these controls dynamically, while the use case is in operation.

Additional resources

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


  • Getting Started with CameraX
  • Code sample

  • Official CameraX sample app