Reputation: 95
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);
}
Upvotes: 2
Views: 2917
Reputation: 988
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()
Upvotes: 1
Reputation: 20128
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.
Upvotes: 2