2

I am working on a project which uses CameraX to show the camera preview on a TextureView. The code hails from this codelab. I have used the setOnPreviewOutputUpdateListener() method to update the TextureView.

private fun startCamera() {
    val previewConfig = PreviewConfig.Builder().apply {
        setTargetAspectRatio( Rational( 1 , 1 ) )
        setTargetResolution( Size( 640 , 640 ) )
    }.build()
    val preview = Preview( previewConfig )
    preview.setOnPreviewOutputUpdateListener {
        val parent = cameraTextureView?.parent as ViewGroup
        parent.removeView( cameraTextureView )
        cameraTextureView?.surfaceTexture = it.surfaceTexture
        parent.addView( cameraTextureView , 0)
        updateTransform()
    }
    preview.removePreviewOutputListener()
    CameraX.bindToLifecycle( this , preview )
}

My question is how do I get the raw byte[] object from preview frames using CameraX. I expect something which is similar to onPreviewFrame(byte[] data, Camera camera) method.

Basically, I want the realtime frames ( in byte[] or Bitmap ) from the preview using CameraX.

Shubham Panchal
  • 4,061
  • 2
  • 11
  • 36

1 Answers1

8

You will use ImageAnalysis. The preview frames will be passed to your Analyzer in YUV_420_888 format.

If necessary, you can request non-blocking analysis mode. I would recommend to set a Handler for your Analyzer, so that the camera callbacks not arrive on the UI thread.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Thanks @Alex Cohn's for the answer. – Shubham Panchal Sep 27 '19 at 00:36
  • 4
    What if you also use `VideoCapture` together with the `Preview`? In that case, you get an exception by trying to "bind too many use cases". See my question here: https://stackoverflow.com/q/61524671/1680301 – Georgios Apr 30 '20 at 15:34
  • 1
    In camerax,can use only one image use case and only one video use case。so ImageAnaysis and VideoCapture together is fine。 Use the ImageAnaysis's callback to get preview frame image,then render the image by GPUImageView to Preview – ProBlc May 27 '21 at 03:06
  • @Alex Cohn I hope You have any examples to implement it. – newbie Jul 16 '22 at 09:54
  • @newbie I have not touched this field for a while, so I cannot show working examples at this time. – Alex Cohn Jul 19 '22 at 11:21
  • Don't worry. take your time. – newbie Jul 19 '22 at 12:14
  • "render the image by GPUImageView to Preview" How? @ProBlc – Astha Garg Jan 03 '23 at 09:43