问题描述
我想要做的是在我的 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 (将#修改为@)