简体   繁体   English

CameraX 的自定义生命周期

[英]Custom Lifecycle for CameraX

I am trying to add a camera fragment using CameraX.but when i use this with viewPager(tablayout) i need camera to close when onPause() function called (as when i switch tab the camera should close ).but with default lifecycle of fragment it closes when fragment gets destroyed.我正在尝试使用 CameraX 添加一个相机片段。但是当我将它与 viewPager(tablayout) 一起使用时,我需要在调用 onPause() 函数时关闭相机(因为当我切换选项卡时,相机应该关闭)但具有片段的默认生命周期当片段被销毁时它会关闭。

Therefore i tried to make a custom lifecycle (code shown below).因此,我尝试制作自定义生命周期(代码如下所示)。 but it is not working camera doesn't even open但它不起作用相机甚至没有打开

class CustomLifecycle : LifecycleOwner {
    private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)

    init {
        lifecycleRegistry.currentState = Lifecycle.State.CREATED
    }

    fun doOnResume() {
        lifecycleRegistry.currentState = Lifecycle.State.RESUMED
    }

    fun doOnDestroy() {
        lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
    }
    override fun getLifecycle(): Lifecycle {
        return lifecycleRegistry
    }
}

binding cameraProvider to lifecycle将 cameraProvider 绑定到生命周期

camera = cameraProvider.bindToLifecycle(
                CustomLifecycle(), cameraSelector, preview, imageCapture, imageAnalyzer
            )

I don't have any idea how to make it work我不知道如何使它工作

any suggestion will be appreciated任何建议将不胜感激

The ON_START and ON_STOP lifecycle events control when a bound use case becomes active and inactive. ON_STARTON_STOP生命周期事件控制绑定用例何时变为活动和非活动状态。 When a use case is active, it expects to start receiving data (frames) from the camera it's attached to.当用例处于活动状态时,它希望开始从它所连接的相机接收数据(帧)。 When a use case is inactive, it no longer expects data from the camera it's attached to.当用例处于非活动状态时,它不再期望来自它所连接的相机的数据。

When you first bind your use cases to the custom lifecycle, you should immediately move its state to ON_START (or even ON_RESUME ).当您第一次将用例绑定到自定义生命周期时,您应该立即将其状态移动到ON_START (甚至ON_RESUME )。 This will start the preview, the analyzer will begin receiving camera frames, and you'll be able to capture images.这将开始预览,分析器将开始接收相机帧,您将能够捕获图像。 When the fragment's onPause() callback is invoked, you can move the custom lifecycle's state to ON_STOP .当片段的onPause()回调被调用时,您可以将自定义生命周期的状态移动到ON_STOP When the fragment is finally destroyed, make sure to move the lifecycle's state to ON_DESTROY in order to free up camera resources.当片段最终被销毁时,确保将生命周期的状态移动到ON_DESTROY以释放相机资源。

In terms of code, you can take a look at a custom lifecycle owner used inside CameraX.在代码方面,您可以查看 CameraX 内部使用的自定义生命周期所有者 You can use it as follows for your use case.您可以按如下方式将其用于您的用例。

class CameraFragment: Fragment() {

    private val customLifecycleOwner = CustomLifecycleOwner()

    override fun onViewCreated(view, savedState) {
        super.onViewCreated(view, savedState)
        // Initialize CameraX
        // Build use cases
        cameraProvider.bindToLifecycle(customLifecycleOwner, selector, useCases)
        customLifecycleOwner.startAndResume()
    }

    override fun onPause() {
        super.onPause()
        customLifecycleOwner.pauseAndStop()
    }

    override fun onDestroy() {
        super.onDestroy()
        customLifecycleOwner.destroy()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM