如何解决CameraX PreviewView 显示所有但仅记录一个区域
我想要做的是在我的 PreviewView
上显示一个矩形,即使 PreviewView 显示整个相机,也只记录该矩形内的区域。
我只想将白色矩形内的内容保存到文件中,但我希望预览显示所有内容:
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))
}
我尝试过 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()
关于 CameraX 的文档太混乱了,从 1.0.0 到 1.1.0 有很多重大变化,我真的很难在这里找到我要找的东西。
如果我不设置 ViewPort,那么记录整个摄像机就可以正常工作,只是记录我正在努力处理的那个区域。
谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。