Untitled

mail@pastecode.io avatar
unknown
kotlin
a year ago
1.4 kB
1
Indexable
    external fun processMat(mat: Mat, maskSettings: MaskSettings): Any

    data class ResultRect(
        var p1: Point,
        var p2: Point,
        var p3: Point,
        var p4: Point,
        var width: Float,
        var height: Float,
        var angle: Float
    )

    data class MaskSettings(
        val colorSpace: Int,
        val lowerBound: Scalar,
        val upperBound: Scalar,
    )

    @androidx.annotation.OptIn(androidx.camera.core.ExperimentalGetImage::class)
    fun analyzeImage(bitmap: Bitmap, imageView: ImageView): Pair<Bitmap, ResultRect>? {

        val bitmap1 = bitmap.copy(bitmap.config, false)

        val maskSettings = listOf(
            MaskSettings(Imgproc.COLOR_RGB2Lab, Scalar(0.0, 107.0, 132.0), Scalar(236.0, 126.0, 196.0)),
            MaskSettings(Imgproc.COLOR_RGB2Lab, Scalar(13.0, 46.0, 35.0), Scalar(57.0, 255.0, 255.0)),
        )

        for (settings in maskSettings) {
            val srcImage = Mat()
            Utils.bitmapToMat(bitmap, srcImage)
            val result = processMat(srcImage, settings) as ResultRect?
            if (result != null) {
                Utils.matToBitmap(srcImage, bitmap1)
                CoroutineScope(Dispatchers.Main).launch {
                    imageView.setImageBitmap(bitmap1)
                }
                return Pair(bitmap1, result)
            }
        }

        return null
    }