Android Turn On/off Camera Flash Programmatically With Camera2
Solution 1:
I'll be addressing you to the Android Dev. documentation about the CameraManager since more code will be required but this is the basic code to activate the Flash on the camera in API's above 21.
CameraManagercamManager= (CameraManager) getSystemService(Context.CAMERA_SERVICE);
StringcameraId= camManager.getCameraIdList()[0]; // Usually front camera is at 0 position.
camManager.setTorchMode(cameraId, true);
Android CameraManager documentation.
Android.hardware.camera2 documentation.
very important thing to remember that you will need to use try/catch to discard possible errors and ofcourse check that no other higher priority application is using the camera at the moment.
Solution 2:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
try {
for (String camID : mCameraManager.getCameraIdList()) {
CameraCharacteristics cameraCharacteristics = mCameraManager.getCameraCharacteristics(camID);
int lensFacing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
if (lensFacing == CameraCharacteristics.LENS_FACING_FRONT && cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) {
mCameraId = camID;
break;
} elseif (lensFacing == CameraCharacteristics.LENS_FACING_BACK && cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) {
mCameraId = camID;
}
}
if (mCameraId != null) {
mCameraManager.setTorchMode(mCameraId, true);
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
mCameraId will preferably turn on front camera flash by priority and if there is no front flash then back camera flash will be used. If device has no flash then mCameraId will be null and setTorchMode will not be called in the code above.
Solution 3:
Try this. Is working fine on me for android Pie and above. There is a toggle button that turns on and off the flashlight
ToggleButtonFlB= (ToggleButton) findViewById(R.id.FlashBt);
finalboolean[] IsTurnedOn = {false};
finalCameraManagercamManager= (CameraManager) getSystemService(Context.CAMERA_SERVICE);
StringcameraId=null; // Usually front camera is at 0 position.try {
cameraId = camManager.getCameraIdList()[0];
} catch (CameraAccessException e) {
e.printStackTrace();
}
finalStringfinalCameraId= cameraId;
FlB.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
if (!IsTurnedOn[0]){
try {
camManager.setTorchMode(finalCameraId, true);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
else {
try {
camManager.setTorchMode(finalCameraId, false);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
IsTurnedOn[0] =!IsTurnedOn[0];
}
});
Solution 4:
Simplest and most banal way to do a flashlight app
// xml code
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.android.torcia.MainActivity"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="ON"android:id="@+id/flash_button"android:layout_centerVertical="true"android:layout_centerHorizontal="true"android:onClick="turnOnOff"/></RelativeLayout>
//java code
publicclassMainActivityextendsAppCompatActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button flashButton;
String buttonText;
publicvoidcheckState(String buttonText) {
switch (buttonText) {
case"ON":
flashButton.setText("OFF");
flashOn();
break;
case"OFF":
flashButton.setText("ON");
flashOff();
break;
}
}
publicvoidturnOnOff(View view) {
flashButton = (Button) findViewById(R.id.flash_button);
buttonText = flashButton.getText().toString();
checkState(this.buttonText);
}
publicvoidflashOn() {
Camera camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
}
publicvoidflashOff() {
Camera camera2 = Camera.open();
Camera.Parameters parameters2 = camera2.getParameters();
parameters2.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera2.setParameters(parameters2);
camera2.stopPreview();
}
}
// AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<uses-permissionandroid:name="android.permission.CAMERA"/><uses-permissionandroid:name="android.permission.FLASHLIGHT"/><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activityandroid:name=".MainActivity"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application>
Post a Comment for "Android Turn On/off Camera Flash Programmatically With Camera2"