gpt4 book ai didi

android - Camerax 拍照 image.planes 总是空的

转载 作者:行者123 更新时间:2023-12-04 15:29:14 28 4
gpt4 key购买 nike

我尝试为内存使用拍摄图像,但 imageProxy.planes[0].remaining() 始终为 0。因此我无法从结果中获取位图。这是错误还是我做错了什么?

这是我绑定(bind)相机的方法:

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.")
}
}

下面是我 try catch 图像的代码:

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.")
}
})
}

截取图片的高度和宽度似乎是正确的。

最佳答案

首先,不要对image.image!!进行操作。尝试倒回 ByteBuffer 然后检查 remaining()尝试:

val buffer = image.planes[0].buffer
buffer.rewind()

关于android - Camerax 拍照 image.planes 总是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61610143/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com