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?
Related
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.
I've been working on an app that needs to turn the flashlight on and off on an Android phone. I need a way to turn on the flashlight across ALL versions of Android above API 15. The problem is, on older phones (API < 21), I have to use the traditional way (below). But on newer phones, like my Nexus 6P, I have to use the Camera2 interface (below the below). Is there one way to have the flashlight torch work on ALL versions of Android with one set of code?
Below:
camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
// camera.release(); used later when app closes
Below the Below:
CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, state); // state is either true or false
A possible solution is to segment different sdk versions during runtime to determine which method to choose:
private final int sdkVersion = Build.VERSION.SDK_INT;
if (sdkVersion < Build.VERSION_CODES.LOLLIPOP) {
//do old way
} else {
//do new way
}
More information here: How to support multiple android version in your code?
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?
I am writing an app that sets the flash mode to torch. I have been testing the application on my Droid X, and the LED light does not come on. I tried it on a Droid Incredible and it worked fine. I can't figure out what the problem is. Here is part of my code for turning on torch mode.
Camera mCamera = Camera.open();
Camera.Parameters params = mCamera.getParameters();
if(params.getFlashMode() != null){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(params);
I have added mCamera.startPreview(); because I read that should make a difference, but it doesn't. I also made a list of available flash modes and displayed them to the screen to make sure that my Droid X does have torch mode, and it was in the list. I even created a new application from code I found online that turns the LED flash on and off with a button. Again it worked fine on the Droid Incredible but not the Droid X. Is there something I am missing to get this to run on the Droid X, or could it be something with Gingerbread? The Droid X is running Gingerbread and the Droid Incredible is running FroYo.
There are quite a few quirks when setting FLASH_MODE_TORCH.
Often you need to start a camera preview:
Camera mCamera = Camera.open();
mCamera.startPreview();
Camera.Parameters params = mCamera.getParameters();
if(params.getFlashMode() != null){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(params);
That may resolve it on some phones, other phones also require the preview to be drawn to a SurfaceView. This can be done by implementing SurfaceHolder.Callback interface in your activity.
See an example here.
It could be that the Droid X doesn't support Torch Mode. Try something like this:
List<String> pList = camera.getParameters().getSupportedFlashModes();
if (pList.contains(Parameters.FLASH_MODE_TORCH))
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
Refer to Issue 191453:
SurfaceTexture mDummy = new SurfaceTexture(1); // any int argument will do
camera.setPreviewTexture(mDummy);
camera.startPreview();
The only thing I found that works on the Droid X is the code presented by Siddhpura Amit part way down the page in this answer Use camera flashlight in Android. He checks the manufacturer and checks to see if it contains the string "motorola." If so, he has special code that can switch the camera Flash LED on or off. I can verify that it does work as I have a Motorola Droid X.