通过事件通道将 Android 图像发送到 Dart

问题描述 投票:0回答:1

在 Flutter 插件中,我想通过事件通道将 Android 图像发送到 Dart,然后将其转换为 Dart 图像。以下实验代码有效,可以视为起点。

        val imageAnalysis = ImageAnalysis.Builder()
            // .setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888)
            // Default is OUTPUT_IMAGE_FORMAT_YUV_420_888
            // .setTargetResolution(Size(1280, 720))
            .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
            .build()
        imageAnalysis.setAnalyzer(executor, ImageAnalysis.Analyzer { imageProxy ->
            val rotationDegrees = imageProxy.imageInfo.rotationDegrees
            val mediaImage = imageProxy.image
            if (mediaImage != null) {
                val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
                sink?.success(image.width)
                sink?.success(image.height)
                sink?.success(image.planes!![0].rowStride)
                sink?.success(image.planes!![0].pixelStride)
                sink?.success(image.planes!![0].buffer.asShortBuffer()[0])
            }
            imageProxy.close()
        })
:
cameraProvider.bindToLifecycle(owner, cameraSelector, preview, imageCapture, imageAnalysis)

我真正想做的是sink?.success(image),但这不起作用——你不能通过事件通道发送对象。

有人做过吗?有任何想法吗?某处有工作代码吗?

android flutter image kotlin event-channel
1个回答
0
投票

这有效。我得到的图像是 RGBA888 而不是 YUV_420_888。

val analyzer = ImageAnalysis.Analyzer { image ->
    if (image.format == PixelFormat.RGBA_8888) {
        val planes = image.image!!.planes
        val plane0 = planes!![0]
        val byteBuffer: ByteBuffer = plane0.buffer
        val byteArray = ByteArray(byteBuffer.capacity())
        byteBuffer.get(byteArray)
        sink?.success(byteArray)
        // Thread.sleep(500)
    }
    image.close()
}
© www.soinside.com 2019 - 2024. All rights reserved.