2

I am currently working on a camera activity. I managed to access my device's back camera flash light and to hide the flash toggle button when I switch to the front camera automatically. However, I was wondering if there was a way to check for secondary flash lights since many smartphone models come with front camera flash light and that would also help when using this application from a tablet without back camera flash light. My idea is checking for the front and back facing camera flash lights separately with two independent booleans and if the flash light is not available, setting the toggle button invisible. I really dislike the idea of showing or hiding the flash button without making sure the device has flash light or not in any of its cameras. This is what I have so far. Any ideas?

private boolean hasFlash(Context Context) {
    if (Context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
        return true;
    } else {
        return false;
    }
}

_

 if (!hasFlash(Context)) {
            ImageButton FlashButton = (ImageButton) findViewById(R.id.frnxcameraflashbutton);
                            FlashButton.setVisibility(View.INVISIBLE);
                            FlashButton.setImageResource(R.mipmap.cameraflashoffbutton);
        }
Matej Varga
  • 95
  • 2
  • 14

2 Answers2

2

If you are able to use the new Camera2 APIs (mostly 21+ I think), the CameraCharacteristics key-value map available for each camera should indicate whether or not each camera has a corresponding flash. For example, you could probably just check for the FLASH_STATE_UNAVAILABLE flag per-camera to achieve your goal.

stkent
  • 19,772
  • 14
  • 85
  • 111
  • Thank you for your answer. I was using the deprecated Camera for this activity so most APIs could use this app. Is it possible to include Camera2 as well to the same activity and get the same result? – Matej Varga Sep 27 '15 at 22:25
  • You can use the `Camera2` APIs on Lollipop and newer, and fall back to your existing implementation on older devices. See e.g. [this answer](http://stackoverflow.com/a/28417458/2911458) for more details. – stkent Sep 27 '15 at 22:28
  • It is certainly a great idea, but I would have to write the Camera2 analogue activities to be able to achieve this new future, right? Anyway, thanks so much for this information, your answer is just perfect. – Matej Varga Sep 27 '15 at 22:37
  • Yeah, you'd need to maintain Camera1 and Camera2 stuff side-by-side and would only see the benefit on newer devices. – stkent Sep 27 '15 at 22:41
1

If we are using CameraX, we can do it as shown below.

var imageCapture: ImageCapture = ImageCapture.Builder()
                .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
                .setFlashMode(flashMode)
                .setTargetRotation(Surface.ROTATION_0)
                .build()




Below code can be used to check if flash unit is available on the device.

val cameraProviderFuture: ListenableFuture<ProcessCameraProvider> = ProcessCameraProvider.getInstance(requireContext())

//Get camera provider
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()

//Get camera object after binding to lifecycle
var camera: Camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview, imageCapture)

Once we have camera object, we can use it to get CameraInfo and check if flash unit is available.

camera?.cameraInfo?.hasFlashUnit()
arungiri_10
  • 988
  • 1
  • 11
  • 23