我尝试获取内存使用情况的图像,但是imageProxy.planes [0] .remaining()始终为0。因此,我无法从结果中获取位图。这是一个错误还是我的操作有误?
这是我如何绑定相机的:
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.")
}
}
这里是我尝试捕获图像的代码:
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.")
}
})
}
拍摄的图像高度和宽度似乎是正确的。