Android 在CameraX中访问图像数据缓冲区
我使用新的CameraX API拍摄这样的照片:Android 在CameraX中访问图像数据缓冲区,android,image,pixel,android-camerax,Android,Image,Pixel,Android Camerax,我使用新的CameraX API拍摄这样的照片: imageButton.setOnClickListener{ val file = File(externalMediaDirs.first(), "${System.currentTimeMillis()}.jpg") imageCapture.takePicture(executor, object : ImageCapture
imageButton.setOnClickListener{
val file = File(externalMediaDirs.first(),
"${System.currentTimeMillis()}.jpg")
imageCapture.takePicture(executor, object :
ImageCapture.OnImageCapturedListener() {
override fun onCaptureSuccess(
image: ImageProxy,
rotationDegrees: Int)
{
// DO I NEED TO USE 'image' TO ACCESS THE IMAGE DATA FOR MANIPULATION (e.g. image.planes)
}
override fun onError(
imageCaptureError: ImageCapture.ImageCaptureError,
message: String,
cause: Throwable?
) {
val msg = "Photo capture failed: $message"
Log.e("CameraXApp", msg, cause)
}
})
imageCapture.takePicture(file, executor,
object : ImageCapture.OnImageSavedListener {
override fun onError(
imageCaptureError: ImageCapture.ImageCaptureError,
message: String,
exc: Throwable?
) {
val msg = "Photo capture failed: $message"
Log.e("CameraXApp", msg, exc)
}
override fun onImageSaved(file: File) {
val msg = "Photo capture succeeded: ${file.absolutePath}"
Log.d("CameraXApp", msg)
}
}
}
我想在捕获图像时使用Renderscript应用一些图像处理。但我不知道如何访问所捕获图像的像素。
有人能提供解决方案吗?
我在onCaptureSucture()回调中使用了image.planes
(参见注释)
我必须承认,我对这一点还不熟悉,也不知道飞机到底是什么。在此之前,我只使用位图(使用Renderscript对位图进行一些图像处理)。有没有办法将帧/图像转换为位图,并在保存为文件之前“动态”对其应用某种排序图像处理?如果是,如何进行?官方的CameraX指南对此没有多大帮助。这可能是很晚的答案,但答案在
ImageCapture
回调中
你得到了一张照片
val image = imageProxy.image
请注意,只有当您拥有YUV格式的图像时,此功能才起作用,以便配置您可以使用的相机
imageCapture = ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
.setBufferFormat(ImageFormat.YUV_420_888)
...
.build()
现在可以获得image.planes
默认图像格式为JPEG
您可以使用
buffer: ByteBuffer = imageProxy.image.planes[0].getBuffer()
如果你打算把它设为YUV
val image = imageProxy.image
val yBuffer = image.planes[0].buffer // Y
val uBuffer = image.planes[1].buffer // U
val vBuffer = image.planes[2].buffer // V