Ema*_*nho 5 video camera android android-camerax
我想要做的是在我的上方显示一个矩形PreviewView,并且仅记录该矩形内部的区域,即使 PreviewView 显示整个相机也是如此。
我只想将白色矩形内的内容保存到文件中,但我希望预览显示所有内容:

活动.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>
Run Code Online (Sandbox Code Playgroud)
活动.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))
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过 ViewPort,但这也改变了预览,这不是我的最终目标,我想记录白色矩形所在的确切 X,Y,但似乎我无法在视口上指定它?
val viewPort = ViewPort.Builder(Rational(width, height), rotation).build()
val useCaseGroup = UseCaseGroup.Builder()
.addUseCase(preview!!) //your preview
.addUseCase(videoCapture!!)
.setViewPort(viewPort)
.build()
Run Code Online (Sandbox Code Playgroud)
关于 CameraX 的文档太混乱了,从 1.0.0 到 1.1.0 有很多重大变化,我真的很难在这里找到我要找的东西。
如果我不设置 ViewPort,则记录整个相机效果很好,只是记录我正在努力处理的区域。
谢谢。