How to crop imageProxy

119 Views Asked by At

I want to crop ImageProxy, In order to analyze data only in preview bounds. I try so many things, And here is what I found, I just create custom view for overlay, that contains only ConstraintLayout and ImageView:

class ScannerOverlay @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {

    private var binding: ScannerOverlayBinding

    init {
        inflate(context, R.layout.scanner_overlay, this)
        binding = ScannerOverlayBinding.inflate(LayoutInflater.from(context))
    }

    val size: Size
        get() = Size(width, height)

    fun scanRect(): RectF {
        // binding.scannerOverlay
        // Here I want to return Rect by preview image
        // return RectF()
    }
}

and I got xml layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/scannerOverlay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:contentDescription="@null"
        android:src="@drawable/ic_scanner_target"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

In case of dimensions it's way simple, But I could not find any usefull example of ImageView case

1

There are 1 best solutions below

0
Mohan Raj On

I've also faced this kind of requirement. That time this blog was found helpful. https://medium.com/@sdptd20/exploring-ocr-capabilities-of-ml-kit-using-camera-x-9949633af0fe

Drop a comment if this was useful. Thank you.