Reputation: 151
I try to take an image for memory usage but imageProxy.planes[0].remaining() is always 0. So I can not get an bitmap from the result. Is this a bug or what am I doing wrong?
Here is how I bind my camera:
private fun bindPreview(cameraProvider: ProcessCameraProvider) {
val cameraSelector: CameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()
imageCapture = ImageCapture.Builder()
.setCaptureMode((ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY))
.setTargetAspectRatio(aspectRatio)
.setTargetRotation(rotation)
.build()
imageAnalyzer = ImageAnalysis.Builder()
.setTargetAspectRatio(aspectRatio)
.setTargetRotation(rotation)
.build()
imageAnalyzer?.setAnalyzer(cameraExecutor, ImageAnalysis.Analyzer { image ->
viewModel.analyzeImage(image)
})
preview = Preview.Builder()
.setTargetAspectRatio(aspectRatio)
.setTargetRotation(rotation)
.build()
cameraProvider.unbindAll()
try {
camera = cameraProvider.bindToLifecycle(
this as LifecycleOwner,
cameraSelector,
preview,
imageCapture,
imageAnalyzer
)
preview?.setSurfaceProvider(binding.previewView.createSurfaceProvider(camera?.cameraInfo))
} catch (e: Exception) {
Timber.e(e, "Could not bind camera.")
}
}
And here is the code how I try to capture the image:
imageCapture?.takePicture(cameraExecutor, object : ImageCapture.OnImageCapturedCallback() {
override fun onCaptureSuccess(image: ImageProxy) {
Log.d(
"test",
"image -> height: ${image.height} | width: ${image.width} | format: ${image.format}"
)
Log.d(
"test",
"image -> planes.size ${image.planes.size} | planes[0].buffer.remaining ${image.planes[0].buffer.remaining()}"
)
Log.d(
"test",
"image -> image.image?.planes.size ${image.image?.planes?.size} | image.image?.planes[0].buffer.remaining ${image.image!!.planes[0].buffer.remaining()}"
)
// some code here
image.close()
}
override fun onError(exception: ImageCaptureException) {
super.onError(exception)
Timber.e(exception.cause, "Could not capture image.")
}
})
}
The height and width of the captured image seems to be right.
Upvotes: 3
Views: 694
Reputation: 698
First of all, do not operate on image.image!!
.
Try to rewind ByteBuffer
then check remaining()
Try:
val buffer = image.planes[0].buffer
buffer.rewind()
Upvotes: 2