简体   繁体   中英

How to get CameraX event when PreviewView is ready

I'm trying to achieve effect with animation, but only after preview view is active. One of the reasons is also to avoid flickering after camera is binded.

I've managed to do it using reflection (androidx.camera.core.UseCase), but on some devices onUseCaseActive is called multiple times in strange order. It's first in ACTIVE state, then it switches to INACTIVE and it is called multiple times and at the end is ACTIVE state.

So my actual question: Is there any way to get CameraX callbacks so I can manage my state differently accordingly to state of camera?

If you need to know when the camera preview first displays on PreviewView , you can use PreviewView.getPreviewStreamState() , it's a LiveData instance that emits either STREAMING or IDLE when the preview is showing or not respectively.

As the documentation states, depending on PreviewView 's implementation mode, the accuracy of the signal for when the preview starts differs. For COMPATIBLE mode, receiving the stream state STREAMING accurately implies that the preview has started, PERFORMANCE mode is less accurate.

Toward the end of your question, you generalized it to include other CameraX use cases as well. So for ImageAnalysis , you know it's active once you receive the first camera frame through its attached Analyzer . As for ImageCapture , you receive a success/failure callback after taking a picture through one of its capture callbacks OnImageCapturedCallback and OnImageSavedCallback .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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