Converting ImageProxy to Bitmap : <p>So, I wanted to explore new Google's Camera API - <code>CameraX</code>.
What I want to do, is take an image from camera feed every second and then pass it into a function that accepts bitmap for machine learning purposes. </p>

<p>I read the documentation on <code>Camera X</code> Image Analyzer:</p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>..which basically is what I need. So, I implemented this image analyzer like this:</p>

<pre><code>imageAnalysis.setAnalyzer { image: ImageProxy, _: Int -&gt;
    viewModel.onAnalyzeImage(image)
}
</code></pre>

<p>What I get is <code>image: ImageProxy</code>. How can I transfer this <code>ImageProxy</code> to <code>Bitmap</code>?</p>

<p>I tried to solve it like this:</p>

<pre><code>fun decodeBitmap(image: ImageProxy): Bitmap? {
    val buffer = image.planes[0].buffer
    val bytes = ByteArray(buffer.capacity()).also { buffer.get(it) }
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
}
</code></pre>

<p>But it returns <code>null</code> - because <code>decodeByteArray</code> does not receive valid (?) bitmap bytes. Any ideas?</p>
