Midnight Guest
Midnight Guest

Reputation: 1910

androidx.camera.view.CameraView how to detect if camera has flash/torch?

Im using androidx.camera:camera-view:1.0.0-alpha05 in my project to take photos from front and back camera of the phone. I don't need all the advanced features which are present in examples here https://github.com/android/camera-samples/tree/master/CameraXBasic also at the present moment the examples are outdated and do not work with latest libs. So all the features are fine in androidx.camera:camera-view, except that I can't detect if flash/torch is present for front or back camera, and there is no way to get this from CameraView. While there are methods like hasCameraWithLensFacing to check if specified camera is present and it also possible set the flash mode via flash property of CameraView. Also there is no way to Get CameraInfo which is referenced in the android documentation for androidx.camera:camera-core.

So how can I detect if flash is present using only androidx.camera:camera-view:1.0.0 which is -alpha05 currently?

Upvotes: 1

Views: 794

Answers (1)

samtun
samtun

Reputation: 444

Check Googles "Getting Started with CameraX": https://codelabs.developers.google.com/codelabs/camerax-getting-started/#3

They use a PreviewView which needs some more initialisation work and offers some less fine control over the camera itself than the CameraView but enables you to do the following things:

  • Detecting if the camera has a flash unit

    _cameraHasFlashUnit = _camera.CameraInfo.HasFlashUnit;

  • Setting the FlashMode (via simple int values)

    _imageCapture.FlashMode = _flashMode;

    • The int values are however not well documented but I found theses:
      • 0 = Auto
      • 1 = Always
      • 2 = Off
  • Setting the torch to on or off

    _camera.CameraControl.EnableTorch(_torchIsActive);

Samples are taken from code using androidx-beta01.1.

My code samples are written in C# because I am currently developing in Xamarin Native but they should be easily transferable to Kotlin if you need to.

Upvotes: 1

Related Questions