What I want to do is show a rectangle over my PreviewView
and only record the area inside of that rectangle even if PreviewView shows the whole camera.
I just want to save to a file what's inside the white rectangle, but I want the preview to show everything:
activity.xml:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.camera.view.PreviewView
android:id="@+id/textureView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/crop"
android:layout_width="350dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="@drawable/background_drawable"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity.kt
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
cameraProvider = cameraProviderFuture.get()
// The display information
val metrics = DisplayMetrics().also { textureView.display.getRealMetrics(it) }
// The ratio for the output image and preview
val aspectRatio = aspectRatio(metrics.widthPixels, metrics.heightPixels)
// The display rotation
val rotation = textureView.display.rotation
val localCameraProvider = cameraProvider
?: throw IllegalStateException("Camera initialization failed.")
// The Configuration of camera preview
preview = Preview.Builder()
.setTargetAspectRatio(aspectRatio) // set the camera aspect ratio
.setTargetRotation(rotation) // set the camera rotation
.build()
// The Configuration of video capture
videoCapture = VideoCapture.Builder().build()
localCameraProvider.unbindAll() // unbind the use-cases before rebinding them
try {
// Bind all use cases to the camera with lifecycle
camera = localCameraProvider.bindToLifecycle(
this, // current lifecycle owner
lensFacing, // either front or back facing
preview, // camera preview use case
videoCapture, // video capture use case
)
// Attach the viewfinder's surface provider to preview use case
preview?.setSurfaceProvider(textureView.surfaceProvider)
} catch (e: Exception) {
Log.e(tag, "Failed to bind use cases", e)
}
}, ContextCompat.getMainExecutor(this))
}
I have tried ViewPort but that changes the preview too and that's not my end goal here and I want to record that exact X,Y where the white rectangle is and it seems like I can't specify that on the viewport?
val viewPort = ViewPort.Builder(Rational(width, height), rotation).build()
val useCaseGroup = UseCaseGroup.Builder()
.addUseCase(preview!!) //your preview
.addUseCase(videoCapture!!)
.setViewPort(viewPort)
.build()
Documentation around CameraX is too messy with a lot of breaking changes from 1.0.0 to 1.1.0 I'm really struggling to find what I'm looking for here.
It works fine to record the whole camera if I don't set ViewPort, is just recording that area only that I'm struggling with.
Thanks.