I am developing an app where I want when a user presses a button front flashlight is on. I know how to open a back camera flash light.
But I don't know how to open the front camera flashlight within the same application. Anyone will guide me how to do this?
I don't think there is such thing as "Front Flash Light". The only thing I can think of at this stage is the Notification LED light at the front. Even looking at my Samsung S6 Edge device, I cannot find any front flash light on my camera.
The following code only applies to the rear flash light. I believe there are three things you will need to do.
1 - Check to see if there is availability for flash on the device
2 - Set the corresponding permission in your manifest
3 - Implement the code to operate the flash functions
This code will return a boolean based on the availability for flash feature
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
Next are your permissions in the manifest file
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
The first permission is for the camera, second permission is for the flashlight where you don't have to activate the camera hardware
And finally, the code to turn on the flash
Camera myCamera = Camera.open();
Parameters myParameters = myCamera .getParameters();
myParameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
myCamera.setParameters(p);
myCamera.startPreview();
To turn the service off
myCamera.stopPreview();
myCamera.release();
Additional function to test
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, true);
flashLightStatus = true;
imageFlashlight.setImageResource(R.drawable.btn_switch_on);
} catch (CameraAccessException e) {
}
This may assist in getting the front camera accessibility from getCameraIdList function. Usually, the front camera is at the first position.
Related
I'm using Camera Package for developing android app to turn on flash light, like this:
Camera mCamera = Camera.open();
List<String> flashModes = mCamera.getParameters().getSupportedFlashModes();
if(flashModes != null && flashModes.contains(Parameters.FLASH_MODE_TORCH)){
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(parameters);
} else if (flashModes != null && flashModes.contains(Parameters.FLASH_MODE_ON)){
parameters.setFlashMode(Parameters.FLASH_MODE_ON);
mCamera.setParameters(parameters);
}
if(!previewing){
mCamera.startPreview();
previewing = true;
}
It check supporting camera flash modes, and adjust it. It works in Samsung Android devices, and some others, but doesn't works in LG V20, LG V30 and Mi Android. I searched for this and tried:
mCamera.autoFocus(Camera.AutoFocusCallback)
mCamera.setPreviewTexture(SurfaceTexture)
But nothing works. So I used Camera2 package for api>23, like:
CameraManager mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
mCameraManager.setTorchMode("0", true);
And it works, but in Samsung device, an notification show up "Flashlight turned on" and it's very annoying.
So, I need the way to turn on flashlight for all devices with Camera package, or not to show up "Flashlight turned on" notification using Camera2 package.
I look forward to your reply, thanks.
The answer in below link saves my life!
The SurfaceView is essential for some devices.
LED flashlight on Galaxy Nexus controllable by what API?
How to turn off Flash light of Camera through intents in Android Programatically?
I use the below code for activating Camera application and then taking the picture and get back to my application.
String packageName = takePictureIntent.resolveActivity(packageManager).getPackageName();
if (mPhotoFileUri != null)
getActivity().grantUriPermission(packageName, mPhotoFileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoFileUri);
startActivityForResult(takePictureIntent, TAKE_PICTURE);
I want to turn off Camera flash when its open by default.How its possible?
Suggestions will be appreciated:-)
to turn on/off the phone camera led or flashlight in Android, you can use following :
Camera camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
And, not forgot to put following permission on AndroidManifest.xml.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
Edit-1:
Unfortunately, when using the camera with Intent, the only extra parameter you can set is
MediaStore.EXTRA_OUTPUT
I'm guess you can't change via Intent. Reference from this question
For this you should do like :
1) Check whether flash light is available or not ?
2) If yes then Turn Off/On
3) If no then you can do whatever according to your app. needs
For Checking availability of flash in device:
You can use the following
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
which will return true if a flash is available, false if not.
See link for more information.
For turning on/off flashlight :
I googled out and got this about android.permission.FLASHLIGHT. Android manifests' permission looks promising:
<!-- Allows access to the flashlight -->
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal"
android:label="#string/permlab_flashlight"
android:description="#string/permdesc_flashlight" />
Then make use of Camera and set Camera.Parameters. The main parameter used here is FLASH_MODE_TORCH.
eg.
Code Snippet to turn on camera flash light.
Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
Code snippet to turn off camera led light.
cam.stopPreview();
cam.release();
Issues :
There are also some problems while turning On/Off flashlight. eg. for the devices not having FLASH_MODE_TORCH or even if it has, then flashlight doesnot turn ON etc.
Typically Samsung creates alot of problems.
You can refer about problems in the given below list:
1) Use camera flashlight in Android
2) Turn ON/OFF Camera LED/flash light in Samsung Galaxy Ace 2.2.1 & Galaxy Tab
REFERENCE LINK :
How to turn on camera flash light programmatically in Android?
I want to turn on only camera flash light (not with camera preview) programmatically in Android. I googled for it but the help I found was not working on my Samsung Galaxy ACE.
By using this code able to turn on for few seconds after that it goes to off automatically with out doing anything on the UI. If I try to turn on again getting force close.
private void turnOn() {
clicked = true;
mCamera = Camera.open();
Parameters params = mCamera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
mCamera.setParameters(params);
mCamera.startPreview();
mCamera.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
}
});
}
For turn OFF
private void turnOff() {
clicked = false;
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
}
}
Declared these manifest permissions
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
Can any body tell me How to turn on the flash light for a long time upto the use click on OFF button. Is it requires any third party libraries. I saw this app in the Google play Tiny flash how they did this app..
From my after turn on it goes to off in 2 sec I did not get what's the problem in this.
Try this
//Turn on
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
//Turn off
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
#RajaReddy PolamReddy,
I am using Samsung S3 LTE (4G). All you need change is to change Parameters.FLASH_MODE_ON to Parameters.FLASH_MODE_TORCH.
It works for me.
Bad news:
According to documentation android camera needs intialized surface view to start preview
Good news:
it must not be full screen or visible
My expirience so far that starting preview is necessary to activate flash ( but I can not speak for all devices )
If you need some examples how to activate preview and possibly hide it behind overlay,
look into javaocr library (see demos - there are 2 android apps, and also camera management library in separate project - you can just grab it)
https://sourceforge.net/projects/javaocr/
I am trying to build a custom camera.
I have a button that starts/stops the flash. I set it to flash mode TORCH when it is on.
On some devices I was told that the flash stops just before the picture is taken. This happens on some android 2.3 devices.
Did anyone else run into this problem? Any ideas why?
Yes I have found the same problem especially on HTC phones.
I have placed a condition to use FLASH_MODE_ON instead of FLASH_MODE_TORCH
List<String> pList = mCamera.getParameters().getSupportedFlashModes();
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
if (pList.contains(Parameters.FLASH_MODE_TORCH) && (!ManufacturerName.contains("htc"))) {
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
}
else
if (pList.contains(Parameters.FLASH_MODE_ON)) {
parameters.setFlashMode(Parameters.FLASH_MODE_ON);
}
}
I am using LG Optimus 3D model, I am able to turn on the torch light when I switch the camera to video recording mode. In normal capture mode, I am only able to turn on flash light.
I have tried several way by adding different parameters into the Camera object, but none of them work. My code is as below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash_light);
PackageManager pm = getPackageManager();
if (! pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
Toast.makeText(this, "Your phone does not have flash light support.", Toast.LENGTH_LONG).show();
finish();
}
camera = Camera.open();
cParameters = camera.getParameters();
cParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(cParameters);
}
No point I cannot turn it on programmatically, I have checked as below to ensure my phone support torch mode, in fact, I am really able to turn on for my device.
List<String> abc = cParameters.getSupportedFlashModes();
for (String a : abc){
Log.d("mode: ", a);
}
Please help, anyway to do that?
* The firmware is 2.2
use the following
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
to see if a flash is available or not. It will will return true or false.
More here on how to actually implement the code.
How to turn on the Android Flashlight
Use camera flashlight in Android
How to turn on camera flash light programmatically in Android?