I am trying to turn on and off camera flashlight using the following code, but every time I call those functions it slows down my whole app. Which is kinda annoying. Is there any way I can do it in separate thread or perhaps in a different way? Thanks in advance.
private void turnOnFlash() {
if (!isFlashOn && hasFlash) {
if (camera == null || params == null) {
return;
}
isFlashOn = true;
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
}
}
private void turnOffFlash() {
if (isFlashOn && hasFlash) {
if (camera == null || params == null) {
return;
}
isFlashOn = false;
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
}
}
and im calling it from another function like
if (intensity[3] < 0.5f) {
turnOnFlash();
} else {
turnOffFlash();
}