Please, can someone provide good example of using CameraX library with GLSurfaceView, can't find any information.
The documentation for using CameraX with Custom surface / TextureView is non-existent. After the beta release of CameraX many API have changed so the existing examples online would work only with alpha versions. Link for using TextureView/GLSUrfaceView with alpha-xx versions: https://github.com/android/camera-samples/tree/master/CameraXBasic
However these samples wont work for the latest beta-02 (as of today) versions.
Please refer the below sample that I used to create a TextureView with CameraX API.
Gradle dependencies:
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"
...
}
Create a Activity for Preview: (Kotlin Example but similar in 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)
}
Now Create a SurfaceProvider Class.
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")
}})
}
}
Create the view 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>
Hope this helps.