So, I wanted to explore new Google's Camera API - CameraX
.
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.
I read the documentation on Camera X
Image Analyzer:
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.
..which basically is what I need. So, I implemented this image analyzer like this:
imageAnalysis.setAnalyzer { image: ImageProxy, _: Int ->
viewModel.onAnalyzeImage(image)
}
What I get is image: ImageProxy
. How can I transfer this ImageProxy
to Bitmap
?
I tried to solve it like this:
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)
}
But it returns null
- because decodeByteArray
does not receive valid (?) bitmap bytes. Any ideas?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…