private boolean isFlashSupported(PackageManager packageManager) {
return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
private boolean isCameraSupported(PackageManager packageManager) {
return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA);
}
public void setTorchState(boolean torchState) {
Log.v(LOG_TAG, "setTorchState - " + torchState);
PackageManager packageManager = getPackageManager();
if (isCameraSupported(packageManager) && isFlashSupported(packageManager)) {
if (camera == null)
try {
camera = Camera.open();
} catch (Exception e) {
e.printStackTrace();
flashOn = false;
}
if (torchState) {
if (camera != null) {
try {
Parameters param = camera.getParameters();
param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(param);
sleep(300);
camera.startPreview();
flashOn = true;
} catch (Exception e) {
e.printStackTrace();
flashOn = false;
}
}
} else {
if (camera != null) {
try {
Parameters param = camera.getParameters();
param.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(param);
camera.stopPreview();
camera.release();
camera = null;
flashOn = false;
} catch (Exception e) {
e.printStackTrace();
}
}
}
sendBroadcast(new Intent(FLASHLIGHT_CHANGED)); // receive in custom receiver
}
}
Thursday, November 23, 2017
Android - Turn flashlight on and off
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment