Android 使用CameraX analyze时如何选择图像的一部分

Android 使用CameraX analyze时如何选择图像的一部分,android,kotlin,firebase-mlkit,android-camerax,Android,Kotlin,Firebase Mlkit,Android Camerax,我正在使用CameraX和FirebaseVision从图像中读取一些文本。当我分析图像时,我想选择图像的一部分,而不是整个图像,就像使用条形码扫描仪一样 class Analyzer : ImageAnalysis.Analyzer { override fun analyze(imageProxy: ImageProxy?, rotationDegrees: Int) { // how to crop the image in here? val

我正在使用CameraX和FirebaseVision从图像中读取一些文本。当我分析图像时,我想选择图像的一部分,而不是整个图像,就像使用条形码扫描仪一样

class Analyzer : ImageAnalysis.Analyzer {
    override fun analyze(imageProxy: ImageProxy?, rotationDegrees: Int) {

        // how to crop the image in here?

        val image = imageProxy.image
        val imageRotation = degreesToFirebaseRotation(degrees)
        if (image != null) {
        val visionImage = FirebaseVisionImage.fromMediaImage(image, imageRotation)
        val textRecognizer = FirebaseVision.getInstance().onDeviceTextRecognizer
        textRecognizer.processImage(visionImage)
    }
}

我想知道,有没有办法裁剪图像?

你的问题正是我两个月前解决的

object YuvNV21Util {

    fun yuv420toNV21(image: Image): ByteArray {
        val crop = image.cropRect
        val format = image.format
        val width = crop.width()
        val height = crop.height()
        val planes = image.planes
        val data =
            ByteArray(width * height * ImageFormat.getBitsPerPixel(format) / 8)
        val rowData = ByteArray(planes[0].rowStride)
        var channelOffset = 0
        var outputStride = 1
        for (i in planes.indices) {
            when (i) {
                0 -> {
                    channelOffset = 0
                    outputStride = 1
                }
                1 -> {
                    channelOffset = width * height + 1
                    outputStride = 2
                }
                2 -> {
                    channelOffset = width * height
                    outputStride = 2
                }
            }
            val buffer = planes[i].buffer
            val rowStride = planes[i].rowStride
            val pixelStride = planes[i].pixelStride
            val shift = if (i == 0) 0 else 1
            val w = width shr shift
            val h = height shr shift
            buffer.position(rowStride * (crop.top shr shift) + pixelStride * (crop.left shr shift))
            for (row in 0 until h) {
                var length: Int
                if (pixelStride == 1 && outputStride == 1) {
                    length = w
                    buffer[data, channelOffset, length]
                    channelOffset += length
                } else {
                    length = (w - 1) * pixelStride + 1
                    buffer[rowData, 0, length]
                    for (col in 0 until w) {
                        data[channelOffset] = rowData[col * pixelStride]
                        channelOffset += outputStride
                    }
                }
                if (row < h - 1) {
                    buffer.position(buffer.position() + rowStride - length)
                }
            }
        }
        return data
    }
}

请看一看我的开源项目,我构建了一个演示应用程序,其中图像代理的一部分在ml kit处理之前被裁剪。

这方面有什么进展吗?我在同一点上被绊倒了。我尝试了
imageProxy.setCropRect
,但是图像没有被裁剪。虽然这个链接可以回答这个问题,但最好在这里包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能无效。-问题是关于种植。在您发布的代码中,这是在哪里实现的?签出YuvNV21Util,它从图像代理获取图像并裁剪croprect类的部分,字节数组输出可以转换为位图,这是下面的代码片段。
object BitmapUtil {
    fun getBitmap(data: ByteArray, metadata: FrameMetadata): Bitmap {

        val image = YuvImage(
            data, ImageFormat.NV21, metadata.width, metadata.height, null
        )
        val stream = ByteArrayOutputStream()
        image.compressToJpeg(
            Rect(0, 0, metadata.width, metadata.height),
            80,
            stream
        )
        val bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size())
        stream.close()
        return rotateBitmap(bmp, metadata.rotation, false, false)
    }

    private fun rotateBitmap(
        bitmap: Bitmap, rotationDegrees: Int, flipX: Boolean, flipY: Boolean
    ): Bitmap {
        val matrix = Matrix()

        // Rotate the image back to straight.
        matrix.postRotate(rotationDegrees.toFloat())

        // Mirror the image along the X or Y axis.
        matrix.postScale(if (flipX) -1.0f else 1.0f, if (flipY) -1.0f else 1.0f)
        val rotatedBitmap =
            Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)

        // Recycle the old bitmap if it has changed.
        if (rotatedBitmap != bitmap) {
            bitmap.recycle()
        }
        return rotatedBitmap
    }
}