请,有人可以提供使用带有 GLSurfaceView 的 CameraX 库的好例子,找不到任何信息。
问问题
1073 次
2 回答
2
不存在将 CameraX 与自定义表面/TextureView 一起使用的文档。在 CameraX 的 beta 版本发布后,许多 API 发生了变化,因此现有的在线示例仅适用于 alpha 版本。使用带有 alpha-xx 版本的 TextureView/GLSUrfaceView 的链接: https ://github.com/android/camera-samples/tree/master/CameraXBasic
但是,这些示例不适用于最新的 beta-02(截至今天)版本。
请参考下面我使用 CameraX API 创建 TextureView 的示例。
摇篮依赖:
dependencies{
def camerax_version = "1.0.0-beta02"
.....
implementation "androidx.camera:camera-core:$camerax_version"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:$camerax_version"
implementation "androidx.camera:camera-view:1.0.0-alpha09"
...
}
为预览创建一个活动:(Kotlin 示例,但在 Java 中类似)
class Capture : AppCompatActivity(), CameraXConfig.Provider {
// ur code
private lateinit var cameraProviderFuture : ListenableFuture<ProcessCameraProvider>
private lateinit var viewFinder: TextureView // Created in view file
// ur code
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_capture)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
// CameraX related Code
cameraProviderFuture = ProcessCameraProvider.getInstance(this)
viewFinder = findViewById(R.id.view_finder)
cameraProviderFuture.addListener(Runnable {
val cameraProvider = cameraProviderFuture.get()
bindPreview(cameraProvider)
}, ContextCompat.getMainExecutor(this))
// ur code here
}
}
private fun bindPreview(cameraProvider : ProcessCameraProvider) {
var preview : Preview = Preview.Builder()
.build()
var cameraSelector : CameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()
var camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview)
val surfaceTexture = (view_finder as TextureView).surfaceTexture
val surface = Surface(surfaceTexture)
val executor = Executors.newSingleThreadExecutor()
val previewSurfaceProvider = PreviewSurfaceProvider(surface, executor)
preview.setSurfaceProvider(executor,previewSurfaceProvider)
cameraProvider.bindToLifecycle(this, cameraSelector, preview)
}
现在创建一个 SurfaceProvider 类。
import android.view.Surface
import androidx.camera.core.Preview
import androidx.camera.core.SurfaceRequest
import androidx.core.util.Consumer
import java.util.concurrent.Executor
import java.util.concurrent.Executors
class PreviewSurfaceProvider (private val surface: Surface, private val executor: Executor): Preview.SurfaceProvider {
override fun onSurfaceRequested(request: SurfaceRequest) {
request.provideSurface(surface, executor, Consumer { result: SurfaceRequest.Result -> {
TODO("Not yet implemented")
}})
}
}
创建视图 xml activity_capture.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
// Can be replaced with GLSurfaceView
<TextureView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_finder"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
希望这可以帮助。
于 2020-04-06T18:31:59.017 回答
0
于 2020-08-21T21:31:51.593 回答