简体   繁体   中英

Android - Camera X - How to check if device has a Front Camera CameraX.LensFacing.FRONT

I'm working with Camera X for the first time and I can't find a way to check if a device has a front or rear camera in runtime...

I only need to use the preview I'm not capturing images so i can't use a button for it..

private var lensFacing = CameraX.LensFacing.FRONT

 val viewFinderConfig = PreviewConfig.Builder().apply {
            setLensFacing(lensFacing)
            setTargetAspectRatio(screenAspectRatio)
            setTargetRotation(viewFinder.display.rotation)
        }.build()

How can I make sure that the app won't crash if the user device has no Front camera? Thanks in advance!

Check if the device supports at least one camera with the specified lens facing:

version 1.0.0-alpha06:

val hasFrontCamera = CameraX.hasCameraWithLensFacing(CameraX.LensFacing.FRONT)

EDIT :

version >= 1.0.0-alpha07:

From https://developer.android.com/jetpack/androidx/releases/camera :

hasCamera() previously provided by the CameraX class call are now available via the ProcessCameraProvider

override fun onCreate(savedInstanceState: Bundle?) {
    cameraProviderFuture = ProcessCameraProvider.getInstance(this);
}

cameraProviderFuture.addListener(Runnable {
    val cameraProvider = cameraProviderFuture.get()
    try {
        var hasCamera = cameraProvider.hasCamera(CameraSelector.DEFAULT_FRONT_CAMERA)
    } catch (e: CameraInfoUnavailableException) {
        e.printStackTrace()
    }
}, ContextCompat.getMainExecutor(this))

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