Camerax Take Picture image.planes всегда пустые - PullRequest
3 голосов
/ 05 мая 2020

Я пытаюсь сделать изображение для использования памяти, но 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.")
        }
    })
}

Высота и ширина захваченного изображения кажутся правильными.

1 Ответ

2 голосов
/ 20 мая 2020

Прежде всего, не работайте на image.image!!. Попробуйте перемотать ByteBuffer потом проверьте remaining() Попробуйте:

val buffer = image.planes[0].buffer
buffer.rewind()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...